Skip to content
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
35 changes: 28 additions & 7 deletions src/Analyzer/FileVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function enterNode(Node $node): void
{
$this->handleClassNode($node);

$this->handleAnonClassNode($node);

$this->handleEnumNode($node);

$this->handleStaticClassConstantNode($node);
Expand Down Expand Up @@ -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());
}

Expand All @@ -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()));
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Analyzer/FileParser/CanParseClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php

namespace Root\Namespace1;

use Root\Namespace2\D;

class Dog implements AnInterface, InterfaceTwo
{
public function foo()
{
$projector2 = new class() extends Another\ForbiddenExtend {};

}
}

class Cat implements AnInterface
{

}
EOF;

$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
$fp->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'
Expand Down