Skip to content

Commit 7690d57

Browse files
Improves parsing of attributes (#461)
relies on checking Node\Attribute instead o $node->attrGroups
1 parent c1205c4 commit 7690d57

File tree

2 files changed

+74
-32
lines changed

2 files changed

+74
-32
lines changed

src/Analyzer/FileVisitor.php

+12-32
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111

1212
class FileVisitor extends NodeVisitorAbstract
1313
{
14-
/** @var ClassDescriptionBuilder */
15-
private $classDescriptionBuilder;
14+
private ClassDescriptionBuilder $classDescriptionBuilder;
1615

17-
/** @var array */
18-
private $classDescriptions = [];
16+
/** @var array<ClassDescription> */
17+
private array $classDescriptions = [];
1918

2019
public function __construct(ClassDescriptionBuilder $classDescriptionBuilder)
2120
{
@@ -50,13 +49,6 @@ public function enterNode(Node $node): void
5049
if ($node->isAbstract()) {
5150
$this->classDescriptionBuilder->setAbstract(true);
5251
}
53-
54-
foreach ($node->attrGroups as $attributeGroup) {
55-
foreach ($attributeGroup->attrs as $attribute) {
56-
$this->classDescriptionBuilder
57-
->addAttribute($attribute->name->toString(), $attribute->getLine());
58-
}
59-
}
6052
}
6153

6254
if ($node instanceof Node\Stmt\Enum_ && null !== $node->namespacedName) {
@@ -67,13 +59,6 @@ public function enterNode(Node $node): void
6759
$this->classDescriptionBuilder
6860
->addInterface($interface->toString(), $interface->getLine());
6961
}
70-
71-
foreach ($node->attrGroups as $attributeGroup) {
72-
foreach ($attributeGroup->attrs as $attribute) {
73-
$this->classDescriptionBuilder
74-
->addAttribute($attribute->name->toString(), $attribute->getLine());
75-
}
76-
}
7762
}
7863

7964
/**
@@ -197,13 +182,6 @@ public function enterNode(Node $node): void
197182

198183
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
199184
$this->classDescriptionBuilder->setInterface(true);
200-
201-
foreach ($node->attrGroups as $attributeGroup) {
202-
foreach ($attributeGroup->attrs as $attribute) {
203-
$this->classDescriptionBuilder
204-
->addAttribute($attribute->name->toString(), $attribute->getLine());
205-
}
206-
}
207185
}
208186

209187
if ($node instanceof Node\Stmt\Trait_) {
@@ -213,13 +191,6 @@ public function enterNode(Node $node): void
213191

214192
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
215193
$this->classDescriptionBuilder->setTrait(true);
216-
217-
foreach ($node->attrGroups as $attributeGroup) {
218-
foreach ($attributeGroup->attrs as $attribute) {
219-
$this->classDescriptionBuilder
220-
->addAttribute($attribute->name->toString(), $attribute->getLine());
221-
}
222-
}
223194
}
224195

225196
if ($node instanceof Node\Stmt\ClassMethod) {
@@ -229,6 +200,15 @@ public function enterNode(Node $node): void
229200
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
230201
}
231202
}
203+
204+
if ($node instanceof Node\Attribute) {
205+
$nodeName = $node->name;
206+
207+
if ($nodeName instanceof Node\Name\FullyQualified) {
208+
$this->classDescriptionBuilder
209+
->addAttribute($node->name->toString(), $node->getLine());
210+
}
211+
}
232212
}
233213

234214
public function getClassDescriptions(): array

tests/Unit/Analyzer/FileVisitorTest.php

+62
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,68 @@ enum Enum
511511
);
512512
}
513513

514+
public function test_it_should_parse_interface_attributes(): void
515+
{
516+
$code = <<< 'EOF'
517+
<?php
518+
519+
namespace Root\Cars;
520+
521+
use Bar\FooAttr;
522+
523+
#[FooAttr('bar')]
524+
interface AnInterface
525+
{
526+
#[Baz]
527+
public function foo(): string;
528+
}
529+
EOF;
530+
531+
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('8.1'));
532+
$fp->parse($code, 'relativePathName');
533+
534+
$cd = $fp->getClassDescriptions();
535+
536+
self::assertEquals(
537+
[
538+
FullyQualifiedClassName::fromString('Bar\\FooAttr'),
539+
FullyQualifiedClassName::fromString('Root\\Cars\\Baz'),
540+
],
541+
$cd[0]->getAttributes()
542+
);
543+
}
544+
545+
public function test_it_should_parse_traits_attributes(): void
546+
{
547+
$code = <<< 'EOF'
548+
<?php
549+
550+
namespace Root\Cars;
551+
552+
use Bar\FooAttr;
553+
554+
#[FooAttr('bar')]
555+
trait ATrait
556+
{
557+
#[Baz]
558+
public function foo(): string { return 'foo'; }
559+
}
560+
EOF;
561+
562+
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('8.1'));
563+
$fp->parse($code, 'relativePathName');
564+
565+
$cd = $fp->getClassDescriptions();
566+
567+
self::assertEquals(
568+
[
569+
FullyQualifiedClassName::fromString('Bar\\FooAttr'),
570+
FullyQualifiedClassName::fromString('Root\\Cars\\Baz'),
571+
],
572+
$cd[0]->getAttributes()
573+
);
574+
}
575+
514576
public function test_it_parse_docblocks(): void
515577
{
516578
$code = <<< 'EOF'

0 commit comments

Comments
 (0)