Skip to content

Commit d1d03a5

Browse files
Adds support for modern PHP syntax analysis
Improves the analysis of PHP classes by adding support for modern syntax features (PHP 8.1+), including enums, readonly properties, attributes and interface analysis within classes. The update ensures accurate identification of class dependencies and correct flagging of abstract/interface status when modern syntax is used.
1 parent 4c9f2e8 commit d1d03a5

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

app/Infrastructure/Analyze/Adapters/PhpParser/DependencyCollectorVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public function enterNode(Node $node): void
2020
if ($node instanceof Class_) {
2121
$this->fqcn = $node->namespacedName?->toString();
2222
$this->isAbstract = $node->isAbstract();
23+
$this->isInterface = false;
2324
}
2425

25-
if ($node instanceof Interface_) {
26+
if ($node instanceof Interface_ && $this->fqcn === null) {
2627
$this->fqcn = $node->namespacedName?->toString();
2728
$this->isInterface = true;
2829
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Php85;
4+
5+
use Attribute;
6+
use DateTimeInterface;
7+
use IteratorAggregate;
8+
9+
#[Attribute]
10+
class CustomAttribute {}
11+
12+
interface Contract {}
13+
14+
abstract class AbstractBase {}
15+
16+
enum Status: string {
17+
case Active = 'active';
18+
}
19+
20+
final class ModernClass extends AbstractBase implements Contract, IteratorAggregate
21+
{
22+
public function __construct(
23+
private readonly DateTimeInterface $clock,
24+
) {}
25+
26+
#[CustomAttribute]
27+
public function handle(Status|Contract|null $value): ?DateTimeInterface
28+
{
29+
return $this->clock;
30+
}
31+
32+
public function getIterator(): \Traversable
33+
{
34+
return new \ArrayIterator([]);
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use App\Infrastructure\Analyze\Adapters\PhpParser\PhpAstClassDependenciesParser;
4+
5+
it('detects dependencies from modern PHP syntax (8.1+) ', function () {
6+
$parser = app(PhpAstClassDependenciesParser::class);
7+
8+
$analysis = $parser->parse(__DIR__ . '/../../../Fixtures/Php85/ModernClass.php');
9+
10+
expect($analysis->fqcn())->toBe('Tests\\Fixtures\\Php85\\ModernClass');
11+
12+
expect($analysis->dependencies())->toContain(
13+
'Tests\\Fixtures\\Php85\\AbstractBase',
14+
'Tests\\Fixtures\\Php85\\Contract',
15+
'IteratorAggregate',
16+
'DateTimeInterface',
17+
'Tests\\Fixtures\\Php85\\Status',
18+
'Tests\\Fixtures\\Php85\\CustomAttribute',
19+
'ArrayIterator',
20+
'Traversable',
21+
);
22+
});
23+
24+
it('marks interface and abstract correctly', function () {
25+
$parser = app(PhpAstClassDependenciesParser::class);
26+
27+
$analysis = $parser->parse(__DIR__ . '/../../../Fixtures/Php85/ModernClass.php');
28+
29+
expect($analysis->isAbstract())->toBeFalse();
30+
expect($analysis->isInterface())->toBeFalse();
31+
});

0 commit comments

Comments
 (0)