Skip to content

Commit fbc5e97

Browse files
split FileVisitor::handleClassNode into handleClassNode and handleAnonClassNode (#511)
1 parent 89bfe5f commit fbc5e97

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

src/Analyzer/FileVisitor.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function enterNode(Node $node): void
2929
{
3030
$this->handleClassNode($node);
3131

32+
$this->handleAnonClassNode($node);
33+
3234
$this->handleEnumNode($node);
3335

3436
$this->handleStaticClassConstantNode($node);
@@ -95,7 +97,11 @@ private function handleClassNode(Node $node): void
9597
return;
9698
}
9799

98-
if (!$node->isAnonymous() && null !== $node->namespacedName) {
100+
if ($node->isAnonymous()) {
101+
return;
102+
}
103+
104+
if (null !== $node->namespacedName) {
99105
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
100106
}
101107

@@ -104,21 +110,36 @@ private function handleClassNode(Node $node): void
104110
->addInterface($interface->toString(), $interface->getLine());
105111
}
106112

107-
if (!$node->isAnonymous() && null !== $node->extends) {
113+
if (null !== $node->extends) {
108114
$this->classDescriptionBuilder
109115
->addExtends($node->extends->toString(), $node->getLine());
110116
}
111117

112-
if (!$node->isAnonymous()) {
113-
$this->classDescriptionBuilder->setFinal($node->isFinal());
118+
$this->classDescriptionBuilder->setFinal($node->isFinal());
119+
120+
$this->classDescriptionBuilder->setReadonly($node->isReadonly());
121+
122+
$this->classDescriptionBuilder->setAbstract($node->isAbstract());
123+
}
124+
125+
private function handleAnonClassNode(Node $node): void
126+
{
127+
if (!($node instanceof Node\Stmt\Class_)) {
128+
return;
114129
}
115130

116131
if (!$node->isAnonymous()) {
117-
$this->classDescriptionBuilder->setReadonly($node->isReadonly());
132+
return;
118133
}
119134

120-
if (!$node->isAnonymous()) {
121-
$this->classDescriptionBuilder->setAbstract($node->isAbstract());
135+
foreach ($node->implements as $interface) {
136+
$this->classDescriptionBuilder
137+
->addDependency(new ClassDependency($interface->toString(), $interface->getLine()));
138+
}
139+
140+
if (null !== $node->extends) {
141+
$this->classDescriptionBuilder
142+
->addDependency(new ClassDependency($node->extends->toString(), $node->getLine()));
122143
}
123144
}
124145

tests/Unit/Analyzer/FileParser/CanParseClassTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,47 @@ class Cat implements AnInterface
132132
self::assertEquals($expectedInterfaces, $cd[0]->getDependencies());
133133
}
134134

135+
public function test_should_parse_anonymous_class_extends(): void
136+
{
137+
$code = <<< 'EOF'
138+
<?php
139+
140+
namespace Root\Namespace1;
141+
142+
use Root\Namespace2\D;
143+
144+
class Dog implements AnInterface, InterfaceTwo
145+
{
146+
public function foo()
147+
{
148+
$projector2 = new class() extends Another\ForbiddenExtend {};
149+
150+
}
151+
}
152+
153+
class Cat implements AnInterface
154+
{
155+
156+
}
157+
EOF;
158+
159+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
160+
$fp->parse($code, 'relativePathName');
161+
$cd = $fp->getClassDescriptions();
162+
163+
self::assertCount(2, $cd);
164+
self::assertInstanceOf(ClassDescription::class, $cd[0]);
165+
self::assertInstanceOf(ClassDescription::class, $cd[1]);
166+
167+
$expectedInterfaces = [
168+
new ClassDependency('Root\Namespace1\AnInterface', 7),
169+
new ClassDependency('Root\Namespace1\InterfaceTwo', 7),
170+
new ClassDependency('Root\Namespace1\Another\ForbiddenExtend', 11),
171+
];
172+
173+
self::assertEquals($expectedInterfaces, $cd[0]->getDependencies());
174+
}
175+
135176
public function test_it_should_parse_extends_class(): void
136177
{
137178
$code = <<< 'EOF'

0 commit comments

Comments
 (0)