Skip to content

Fix ContextFactory::createForNamespace with closure and use statement #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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