Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add consideration of Node\Attribute to FileVisitor #444

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
9 changes: 9 additions & 0 deletions src/Analyzer/FileVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ public function enterNode(Node $node): void
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
}
}

if ($node instanceof Node\Attribute) {
$nodeName = $node->name;

if ($nodeName instanceof Node\Name\FullyQualified) {
$this->classDescriptionBuilder
->addDependency(new ClassDependency(implode('\\', $nodeName->getParts()), $node->getLine()));
}
}
}

public function getClassDescriptions(): array
Expand Down
35 changes: 35 additions & 0 deletions tests/E2E/PHPUnit/CheckAttributeDependencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Arkitect\Tests\E2E\PHPUnit;

use Arkitect\ClassSet;
use Arkitect\Expression\ForClasses\NotDependsOnTheseNamespaces;
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
use Arkitect\Rules\Rule;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

class CheckAttributeDependencyTest extends TestCase
{
/**
* @requires PHP >= 8.0
*/
public function test_assertion_should_fail_on_invalid_dependency(): void
{
$set = ClassSet::fromDir(__DIR__.'/../_fixtures/attributes');

$rule = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Service'))
->should(new NotDependsOnTheseNamespaces('App\Service\Invalid'))
->because('i said so');

$expectedExceptionMessage = 'depends on App\Service\Invalid\Attr, but should not depend on these namespaces: App\Service\Invalid because i said so';

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

ArchRuleTestCase::assertArchRule($rule, $set);
}
}
13 changes: 13 additions & 0 deletions tests/E2E/_fixtures/attributes/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Service;

use App\Service\Invalid\Attr as InvalidAttr;
use App\Service\Valid\Attr as ValidAttr;

#[ValidAttr, InvalidAttr]
class Foo
{
}
12 changes: 12 additions & 0 deletions tests/E2E/_fixtures/attributes/Invalid/Attr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Service\Invalid;

use Attribute;

#[Attribute]
class Attr
{
}
12 changes: 12 additions & 0 deletions tests/E2E/_fixtures/attributes/Valid/Attr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Service\Valid;

use Attribute;

#[Attribute]
class Attr
{
}