From 168cfc8ad4fefc38d58ce277b883c7736e8f8366 Mon Sep 17 00:00:00 2001 From: Jordi Domenech Date: Mon, 19 Jun 2023 08:38:50 +0200 Subject: [PATCH] Don't parse anonymous classes extends --- src/Analyzer/FileVisitor.php | 2 +- tests/Unit/Analyzer/FileVisitorTest.php | 29 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Analyzer/FileVisitor.php b/src/Analyzer/FileVisitor.php index 222c4c87..42658a84 100644 --- a/src/Analyzer/FileVisitor.php +++ b/src/Analyzer/FileVisitor.php @@ -33,7 +33,7 @@ public function enterNode(Node $node): void ->addInterface($interface->toString(), $interface->getLine()); } - if (null !== $node->extends) { + if (!$node->isAnonymous() && null !== $node->extends) { $this->classDescriptionBuilder ->setExtends($node->extends->toString(), $node->getLine()); } diff --git a/tests/Unit/Analyzer/FileVisitorTest.php b/tests/Unit/Analyzer/FileVisitorTest.php index cca32226..40f92ac0 100644 --- a/tests/Unit/Analyzer/FileVisitorTest.php +++ b/tests/Unit/Analyzer/FileVisitorTest.php @@ -116,6 +116,35 @@ class Animal class Cat extends Animal { +} +EOF; + + /** @var FileParser $fp */ + $fp = FileParserFactory::createFileParser(TargetPhpVersion::create('7.1')); + $fp->parse($code, 'relativePathName'); + + $cd = $fp->getClassDescriptions()[1]; + + $this->assertEquals('Root\Animals\Animal', $cd->getExtends()->toString()); + } + + public function test_it_should_not_parse_extends_from_insider_anonymousclass(): void + { + $code = <<< 'EOF' +