Skip to content

Commit 414ec7a

Browse files
philipp-yoummdayPhilipp Lohmann
andauthored
fix: Add consideration of Node\Attribute to FileVisitor (#444)
* fix: Add consideration of Node\Attribute to FileVisitor * Add test --------- Co-authored-by: Philipp Lohmann <[email protected]>
1 parent 766b5e2 commit 414ec7a

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

src/Analyzer/FileVisitor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ public function enterNode(Node $node): void
224224
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
225225
}
226226
}
227+
228+
if ($node instanceof Node\Attribute) {
229+
$nodeName = $node->name;
230+
231+
if ($nodeName instanceof Node\Name\FullyQualified) {
232+
$this->classDescriptionBuilder
233+
->addDependency(new ClassDependency(implode('\\', $nodeName->getParts()), $node->getLine()));
234+
}
235+
}
227236
}
228237

229238
public function getClassDescriptions(): array
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arkitect\Tests\E2E\PHPUnit;
6+
7+
use Arkitect\ClassSet;
8+
use Arkitect\Expression\ForClasses\NotDependsOnTheseNamespaces;
9+
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
10+
use Arkitect\Rules\Rule;
11+
use PHPUnit\Framework\ExpectationFailedException;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class CheckAttributeDependencyTest extends TestCase
15+
{
16+
/**
17+
* @requires PHP >= 8.0
18+
*/
19+
public function test_assertion_should_fail_on_invalid_dependency(): void
20+
{
21+
$set = ClassSet::fromDir(__DIR__.'/../_fixtures/attributes');
22+
23+
$rule = Rule::allClasses()
24+
->that(new ResideInOneOfTheseNamespaces('App\Service'))
25+
->should(new NotDependsOnTheseNamespaces('App\Service\Invalid'))
26+
->because('i said so');
27+
28+
$expectedExceptionMessage = 'depends on App\Service\Invalid\Attr, but should not depend on these namespaces: App\Service\Invalid because i said so';
29+
30+
$this->expectException(ExpectationFailedException::class);
31+
$this->expectExceptionMessage($expectedExceptionMessage);
32+
33+
ArchRuleTestCase::assertArchRule($rule, $set);
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service;
6+
7+
use App\Service\Invalid\Attr as InvalidAttr;
8+
use App\Service\Valid\Attr as ValidAttr;
9+
10+
#[ValidAttr, InvalidAttr]
11+
class Foo
12+
{
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service\Invalid;
6+
7+
use Attribute;
8+
9+
#[Attribute]
10+
class Attr
11+
{
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service\Valid;
6+
7+
use Attribute;
8+
9+
#[Attribute]
10+
class Attr
11+
{
12+
}

0 commit comments

Comments
 (0)