Skip to content

Commit af7b9b8

Browse files
committed
Added test to check T_CURLY_OPEN and T_DOLLAR_OPEN_CURLY_BRACES are counted as equal to ensure we end at the correct point
1 parent 1c2a09c commit af7b9b8

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Types/ContextFactory.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public function createForNamespace($namespace, $fileContents)
7777
$braceLevel = 0;
7878
$firstBraceFound = false;
7979
while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) {
80-
if ($tokens->current() === '{') {
80+
if ($tokens->current() === '{'
81+
|| $tokens->current()[0] === T_CURLY_OPEN
82+
|| $tokens->current()[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
8183
if (!$firstBraceFound) {
8284
$firstBraceFound = true;
8385
}

tests/unit/Types/ContextFactoryTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ class FooClass {
110110

111111
$this->assertSame([], $context->getNamespaceAliases());
112112
}
113+
114+
/**
115+
* @covers ::createForNamespace
116+
* @uses phpDocumentor\Reflection\Types\Context
117+
*/
118+
public function testAllOpeningBracesAreCheckedWhenSearchingForEndOfClass()
119+
{
120+
$php = '<?php
121+
namespace Foo;
122+
123+
trait FooTrait {}
124+
trait BarTrait {}
125+
126+
class FooClass {
127+
use FooTrait;
128+
129+
public function bar()
130+
{
131+
echo "{$baz}";
132+
echo "${baz}";
133+
}
134+
}
135+
136+
class BarClass {
137+
use BarTrait;
138+
139+
public function bar()
140+
{
141+
echo "{$baz}";
142+
echo "${baz}";
143+
}
144+
}
145+
';
146+
147+
$fixture = new ContextFactory();
148+
$context = $fixture->createForNamespace('Foo', $php);
149+
150+
$this->assertSame([], $context->getNamespaceAliases());
151+
}
113152
}
114153
}
115154

0 commit comments

Comments
 (0)