Skip to content

Commit 92dad93

Browse files
Adds appliesTo to verify if a given rule should be applied (#454)
1 parent 3adbcec commit 92dad93

32 files changed

+647
-245
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ phparkitect.phar
1111
composer.lock
1212
.php-version
1313
composer.phar
14-
.phpunit.cache/
14+
.phpunit.cache/
15+
.vscode

.php-cs-fixer.dist.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
->exclude('vendor/')
66
->notPath('tests/E2E/_fixtures/parse_error/Services/CartService.php');
77

8-
98
return (new PhpCsFixer\Config())
109
->setFinder($finder)
1110
->setRiskyAllowed(true)
@@ -24,7 +23,7 @@
2423
'modernize_types_casting' => true, // Replaces intval, floatval, doubleval, strval and boolval function calls with according type casting operator.
2524
'multiline_whitespace_before_semicolons' => true, // Forbid multi-line whitespace before the closing semicolon or move the semicolon to the new line for chained calls.
2625
'no_unreachable_default_argument_value' => true, // In function arguments there must not be arguments with default values before non-default ones.
27-
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],// To avoid problems of compatibility with the old php-cs-fixer version used on PHP 7.3
26+
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true], // To avoid problems of compatibility with the old php-cs-fixer version used on PHP 7.3
2827
'no_useless_else' => true,
2928
'no_useless_return' => true,
3029
'ordered_class_elements' => true, // Orders the elements of classes/interfaces/traits.

src/Analyzer/FileVisitor.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,6 @@ 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-
}
236227
}
237228

238229
public function getClassDescriptions(): array

src/CLI/PhpArkitectApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Arkitect\CLI;

src/Expression/Expression.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Arkitect\Expression;
@@ -20,6 +21,16 @@ interface Expression
2021
*/
2122
public function describe(ClassDescription $theClass, string $because): Description;
2223

24+
/**
25+
* Checks if the expression applies to the class passed as parameter.
26+
* If the current expression does not apply to the class, this method should return false.
27+
*
28+
* eg: IsAbstract does not applies to interfaces, traits, readonly classes
29+
*
30+
* Not included directly in the interface to allow incremental implementation of it in the rules.
31+
*/
32+
//public function appliesTo(ClassDescription $theClass): bool;
33+
2334
/**
2435
* Evaluates the expression for the class passed as parameter.
2536
* It should adds violations if rule is violated.

src/Expression/ForClasses/IsAbstract.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ public function describe(ClassDescription $theClass, string $because): Descripti
1818
return new Description("{$theClass->getName()} should be abstract", $because);
1919
}
2020

21+
public function appliesTo(ClassDescription $theClass): bool
22+
{
23+
return !($theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum() || $theClass->isFinal());
24+
}
25+
2126
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
2227
{
23-
if ($theClass->isAbstract() || $theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum()
24-
|| $theClass->isFinal()) {
28+
if ($theClass->isAbstract()) {
2529
return;
2630
}
2731

src/Expression/ForClasses/IsFinal.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ public function describe(ClassDescription $theClass, string $because): Descripti
1818
return new Description("{$theClass->getName()} should be final", $because);
1919
}
2020

21+
public function appliesTo(ClassDescription $theClass): bool
22+
{
23+
return !($theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum() || $theClass->isAbstract());
24+
}
25+
2126
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
2227
{
23-
if ($theClass->isAbstract() || $theClass->isInterface() || $theClass->isFinal() || $theClass->isTrait()
24-
|| $theClass->isEnum()) {
28+
if ($theClass->isFinal()) {
2529
return;
2630
}
2731

src/Expression/ForClasses/IsNotAbstract.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function describe(ClassDescription $theClass, string $because): Descripti
1818
return new Description("{$theClass->getName()} should not be abstract", $because);
1919
}
2020

21+
public function appliesTo(ClassDescription $theClass): bool
22+
{
23+
return !($theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum() || $theClass->isFinal());
24+
}
25+
2126
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
2227
{
2328
if (!$theClass->isAbstract()) {

src/Expression/ForClasses/IsNotFinal.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function describe(ClassDescription $theClass, string $because): Descripti
1818
return new Description("{$theClass->getName()} should not be final", $because);
1919
}
2020

21+
public function appliesTo(ClassDescription $theClass): bool
22+
{
23+
return !($theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum() || $theClass->isAbstract());
24+
}
25+
2126
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
2227
{
2328
if (!$theClass->isFinal()) {

src/Expression/ForClasses/IsNotReadonly.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function describe(ClassDescription $theClass, string $because): Descripti
1818
return new Description("{$theClass->getName()} should not be readonly", $because);
1919
}
2020

21+
public function appliesTo(ClassDescription $theClass): bool
22+
{
23+
return !($theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum());
24+
}
25+
2126
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
2227
{
2328
if (!$theClass->isReadonly()) {

0 commit comments

Comments
 (0)