-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #885 from dbrumann/attribute_collector
Add new Attribute-Collector
- Loading branch information
Showing
6 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Layer\Collector; | ||
|
||
use LogicException; | ||
use Qossmic\Deptrac\Ast\AstMap\AstMap; | ||
use Qossmic\Deptrac\Ast\AstMap\ClassLike\ClassLikeReference; | ||
use Qossmic\Deptrac\Ast\AstMap\DependencyToken; | ||
use Qossmic\Deptrac\Ast\AstMap\File\FileReference; | ||
use Qossmic\Deptrac\Ast\AstMap\FunctionLike\FunctionLikeReference; | ||
use Qossmic\Deptrac\Ast\AstMap\TokenReferenceInterface; | ||
|
||
class AttributeCollector implements CollectorInterface | ||
{ | ||
public function satisfy(array $config, TokenReferenceInterface $reference, AstMap $astMap): bool | ||
{ | ||
if (!$reference instanceof FileReference | ||
&& !$reference instanceof ClassLikeReference | ||
&& !$reference instanceof FunctionLikeReference | ||
) { | ||
return false; | ||
} | ||
|
||
$match = $this->getSearchedSubstring($config); | ||
|
||
foreach ($reference->getDependencies() as $dependency) { | ||
if (DependencyToken::ATTRIBUTE !== $dependency->getType()) { | ||
continue; | ||
} | ||
|
||
$usedAttribute = $dependency->getToken()->toString(); | ||
|
||
if (str_contains($usedAttribute, $match)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param array<string, string|array<string, string>> $config | ||
*/ | ||
private function getSearchedSubstring(array $config): string | ||
{ | ||
if (!isset($config['value']) || !is_string($config['value'])) { | ||
throw new LogicException('AttributeCollector needs the attribute name as a string.'); | ||
} | ||
|
||
return $config['value']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Qossmic\Deptrac\Layer\Collector; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Qossmic\Deptrac\Ast\AstMap\AstMap; | ||
use Qossmic\Deptrac\Ast\AstMap\File\FileReferenceBuilder; | ||
use Qossmic\Deptrac\Layer\Collector\AttributeCollector; | ||
|
||
final class AttributeCollectorTest extends TestCase | ||
{ | ||
private AttributeCollector $collector; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->collector = new AttributeCollector(); | ||
} | ||
|
||
public function dataProviderSatisfy(): iterable | ||
{ | ||
yield 'matches usage of attribute with only partial name' => [ | ||
['value' => 'MyAttribute'], | ||
true, | ||
]; | ||
yield 'does not match unescaped fully qualified class name' => [ | ||
['value' => 'App\MyAttribute'], | ||
true, | ||
]; | ||
yield 'does not match other attributes' => [ | ||
['value' => 'OtherAttribute'], | ||
false, | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderSatisfy | ||
*/ | ||
public function testSatisfy(array $config, bool $expected): void | ||
{ | ||
$classLikeReference = FileReferenceBuilder::create('Foo.php') | ||
->newClass('App\Foo') | ||
->attribute('App\MyAttribute', 2) | ||
->attribute('MyAttribute', 3) | ||
->build(); | ||
$actual = $this->collector->satisfy($config, $classLikeReference, new AstMap([])); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
} |