Skip to content

Commit 3219c6e

Browse files
authored
Merge pull request #192 from maMykola/fix-closure-with-use-statment
Fix ContextFactory::createForNamespace with closure and use statement
2 parents 07100e6 + 4ec06f8 commit 3219c6e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Types/ContextFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public function createForNamespace(string $namespace, string $fileContents): Con
181181
$currentNamespace = $this->parseNamespace($tokens);
182182
break;
183183
case T_CLASS:
184+
case T_TRAIT:
184185
// Fast-forward the iterator through the class so that any
185186
// T_USE tokens found within are skipped - these are not
186187
// valid namespace use statements so should be ignored.

tests/unit/Types/ContextFactoryTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,35 @@ public function bar()
139139
$this->assertSame([], $context->getNamespaceAliases());
140140
}
141141

142+
/**
143+
* @covers ::createForNamespace
144+
* @uses phpDocumentor\Reflection\Types\Context
145+
*/
146+
public function testTraitContainsClosureWithUseStatement() : void
147+
{
148+
$php = '<?php declare(strict_types=1);
149+
namespace Foo;
150+
151+
trait FooTrait {
152+
protected function check(array $data, string $key) : void
153+
{
154+
array_walk($data, function(&$item) use ($key) {
155+
// update item based on the key
156+
});
157+
}
158+
}
159+
160+
class FooClass {
161+
use FooTrait;
162+
}
163+
';
164+
165+
$fixture = new ContextFactory();
166+
$context = $fixture->createForNamespace('Foo', $php);
167+
168+
$this->assertSame([], $context->getNamespaceAliases());
169+
}
170+
142171
/**
143172
* @covers ::createFromReflector
144173
*/

0 commit comments

Comments
 (0)