Skip to content

Commit

Permalink
Merge pull request #192 from maMykola/fix-closure-with-use-statment
Browse files Browse the repository at this point in the history
Fix ContextFactory::createForNamespace with closure and use statement
  • Loading branch information
jaapio authored Aug 12, 2023
2 parents 07100e6 + 4ec06f8 commit 3219c6e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Types/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public function createForNamespace(string $namespace, string $fileContents): Con
$currentNamespace = $this->parseNamespace($tokens);
break;
case T_CLASS:
case T_TRAIT:
// Fast-forward the iterator through the class so that any
// T_USE tokens found within are skipped - these are not
// valid namespace use statements so should be ignored.
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/Types/ContextFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ public function bar()
$this->assertSame([], $context->getNamespaceAliases());
}

/**
* @covers ::createForNamespace
* @uses phpDocumentor\Reflection\Types\Context
*/
public function testTraitContainsClosureWithUseStatement() : void
{
$php = '<?php declare(strict_types=1);
namespace Foo;
trait FooTrait {
protected function check(array $data, string $key) : void
{
array_walk($data, function(&$item) use ($key) {
// update item based on the key
});
}
}
class FooClass {
use FooTrait;
}
';

$fixture = new ContextFactory();
$context = $fixture->createForNamespace('Foo', $php);

$this->assertSame([], $context->getNamespaceAliases());
}

/**
* @covers ::createFromReflector
*/
Expand Down

0 comments on commit 3219c6e

Please sign in to comment.