diff --git a/CHANGELOG.md b/CHANGELOG.md index c720a145..16b6776a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - displays more detailed errors if json config format is not valid +- fixed fatal error when parsing trait usage with modified visibility (#44) ## [0.1.6] - 2017-09-24 ### Added diff --git a/src/ComposerRequireChecker/NodeVisitor/UsedSymbolCollector.php b/src/ComposerRequireChecker/NodeVisitor/UsedSymbolCollector.php index e690a734..cbb96b95 100644 --- a/src/ComposerRequireChecker/NodeVisitor/UsedSymbolCollector.php +++ b/src/ComposerRequireChecker/NodeVisitor/UsedSymbolCollector.php @@ -150,7 +150,9 @@ private function recordTraitUsage(Node $node) array_map([$this, 'recordUsageOf'], $node->traits); foreach ($node->adaptations as $adaptation) { - $this->recordUsageOf($adaptation->trait); + if (null !== $adaptation->trait) { + $this->recordUsageOf($adaptation->trait); + } if ($adaptation instanceof Node\Stmt\TraitUseAdaptation\Precedence) { array_map([$this, 'recordUsageOf'], $adaptation->insteadof); diff --git a/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorFunctionalTest.php b/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorFunctionalTest.php index 9d9c2254..3515e64f 100644 --- a/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorFunctionalTest.php +++ b/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorFunctionalTest.php @@ -95,6 +95,17 @@ public function testWillCollectNamespacedConstDefinition() ); } + public function testTraitAdaptionDefinition() + { + $this->traverseStringAST('namespace Foo; trait BarTrait { protected function test(){}} class UseTrait { use BarTrait {test as public;} }'); + + self::assertSameCollectedSymbols( + ['Foo\BarTrait', 'Foo\UseTrait'], + $this->collector->getDefinedSymbols() + ); + } + + private function traverseStringAST(string $phpSource): array { return $this->traverser->traverse($this->parser->parse('traverseStringAST('collector->getCollectedSymbols() + ); + } + private function traverseStringAST(string $stringAST) { return $this->traverser->traverse( diff --git a/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php b/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php index 07a3893d..da7c3213 100644 --- a/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php +++ b/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php @@ -19,6 +19,7 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\TraitUse; +use PhpParser\Node\Stmt\TraitUseAdaptation\Alias; use PHPUnit\Framework\TestCase; /** @@ -304,6 +305,18 @@ public function testTraits() $this->assertContains('Foo', $symbols); } + public function testTraitUseVisibilityAdaptation() + { + $traitUseAdaption = new Alias(null, 'testMethod', Class_::MODIFIER_PUBLIC, null); + $traitUse = new TraitUse([new Name('Foo')], [$traitUseAdaption]); + + $this->visitor->enterNode($traitUse); + + $symbols = $this->visitor->getCollectedSymbols(); + $this->assertCount(1, $symbols); + $this->assertContains('Foo', $symbols); + } + public function testBeforeTraverseResetsRecordedSymbols() { $node = new Class_('Foo');