diff --git a/src/Analyzer/FileVisitor.php b/src/Analyzer/FileVisitor.php index 827e6e48..db2ffcd7 100644 --- a/src/Analyzer/FileVisitor.php +++ b/src/Analyzer/FileVisitor.php @@ -29,6 +29,8 @@ public function enterNode(Node $node): void { $this->handleClassNode($node); + $this->handleAnonClassNode($node); + $this->handleEnumNode($node); $this->handleStaticClassConstantNode($node); @@ -95,7 +97,11 @@ private function handleClassNode(Node $node): void return; } - if (!$node->isAnonymous() && null !== $node->namespacedName) { + if ($node->isAnonymous()) { + return; + } + + if (null !== $node->namespacedName) { $this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString()); } @@ -104,21 +110,36 @@ private function handleClassNode(Node $node): void ->addInterface($interface->toString(), $interface->getLine()); } - if (!$node->isAnonymous() && null !== $node->extends) { + if (null !== $node->extends) { $this->classDescriptionBuilder ->addExtends($node->extends->toString(), $node->getLine()); } - if (!$node->isAnonymous()) { - $this->classDescriptionBuilder->setFinal($node->isFinal()); + $this->classDescriptionBuilder->setFinal($node->isFinal()); + + $this->classDescriptionBuilder->setReadonly($node->isReadonly()); + + $this->classDescriptionBuilder->setAbstract($node->isAbstract()); + } + + private function handleAnonClassNode(Node $node): void + { + if (!($node instanceof Node\Stmt\Class_)) { + return; } if (!$node->isAnonymous()) { - $this->classDescriptionBuilder->setReadonly($node->isReadonly()); + return; } - if (!$node->isAnonymous()) { - $this->classDescriptionBuilder->setAbstract($node->isAbstract()); + foreach ($node->implements as $interface) { + $this->classDescriptionBuilder + ->addDependency(new ClassDependency($interface->toString(), $interface->getLine())); + } + + if (null !== $node->extends) { + $this->classDescriptionBuilder + ->addDependency(new ClassDependency($node->extends->toString(), $node->getLine())); } } diff --git a/tests/Unit/Analyzer/FileParser/CanParseClassTest.php b/tests/Unit/Analyzer/FileParser/CanParseClassTest.php index 9946df43..6e48c009 100644 --- a/tests/Unit/Analyzer/FileParser/CanParseClassTest.php +++ b/tests/Unit/Analyzer/FileParser/CanParseClassTest.php @@ -132,6 +132,47 @@ class Cat implements AnInterface self::assertEquals($expectedInterfaces, $cd[0]->getDependencies()); } + public function test_should_parse_anonymous_class_extends(): void + { + $code = <<< 'EOF' + parse($code, 'relativePathName'); + $cd = $fp->getClassDescriptions(); + + self::assertCount(2, $cd); + self::assertInstanceOf(ClassDescription::class, $cd[0]); + self::assertInstanceOf(ClassDescription::class, $cd[1]); + + $expectedInterfaces = [ + new ClassDependency('Root\Namespace1\AnInterface', 7), + new ClassDependency('Root\Namespace1\InterfaceTwo', 7), + new ClassDependency('Root\Namespace1\Another\ForbiddenExtend', 11), + ]; + + self::assertEquals($expectedInterfaces, $cd[0]->getDependencies()); + } + public function test_it_should_parse_extends_class(): void { $code = <<< 'EOF'