diff --git a/docs/documentation/assertions.md b/docs/documentation/assertions.md index e683ab40..4d3b6a1c 100644 --- a/docs/documentation/assertions.md +++ b/docs/documentation/assertions.md @@ -48,7 +48,7 @@ It asserts that the selected classes **do not depend** on the target classes. It asserts that the selected classes **do not use the constructor** of the target classes. ## shouldNotExist() -It asserts that the selected classes **should not exist**. +It asserts that the selected classes **do not exist**. ## shouldApplyAttribute() It asserts that the selected classes **apply** the target attributes. diff --git a/extension.neon b/extension.neon index 34dbea22..12a5292e 100644 --- a/extension.neon +++ b/extension.neon @@ -74,7 +74,7 @@ services: # ShouldNotExist rules - - class: PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExistRule + class: PHPat\Rule\Assertion\Declaration\ShouldNotExist\ExistsRule tags: - phpstan.rules.rule diff --git a/src/Rule/Assertion/Declaration/ShouldNotExist/ExistsRule.php b/src/Rule/Assertion/Declaration/ShouldNotExist/ExistsRule.php new file mode 100644 index 00000000..b00b3750 --- /dev/null +++ b/src/Rule/Assertion/Declaration/ShouldNotExist/ExistsRule.php @@ -0,0 +1,15 @@ + + */ +final class ExistsRule extends ShouldNotExist implements Rule +{ + use ExistsExtractor; +} diff --git a/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php index f25d49ed..892d0fe2 100644 --- a/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php +++ b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php @@ -6,7 +6,6 @@ use PHPat\Rule\Assertion\Declaration\DeclarationAssertion; use PHPat\Rule\Assertion\Declaration\ValidationTrait; use PHPat\Statement\Builder\StatementBuilderFactory; -use PHPStan\Node\InClassNode; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\FileTypeMapper; @@ -30,13 +29,16 @@ public function __construct( ); } - public function getNodeType(): string + protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array { - return InClassNode::class; + return $this->applyShouldNot($ruleName, $subject, $meetsDeclaration, $tips, $params); } - protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array + protected function getMessage(string $ruleName, string $subject, array $params = []): string { - return $this->applyShouldNot($ruleName, $subject, $meetsDeclaration, $tips, $params); + return $this->prepareMessage( + $ruleName, + sprintf('%s should not exist', $subject) + ); } } diff --git a/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php deleted file mode 100644 index c7bdf90f..00000000 --- a/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php +++ /dev/null @@ -1,27 +0,0 @@ - - */ -final class ShouldNotExistRule extends ShouldNotExist implements Rule -{ - protected function getMessage(string $ruleName, string $subject, array $params = []): string - { - return $this->prepareMessage( - $ruleName, - sprintf('%s should not exist', $subject) - ); - } - - protected function meetsDeclaration(Node $node, Scope $scope, array $params = []): bool - { - return true; - } -} diff --git a/src/Rule/Extractor/Declaration/ExistsExtractor.php b/src/Rule/Extractor/Declaration/ExistsExtractor.php new file mode 100644 index 00000000..8f83f522 --- /dev/null +++ b/src/Rule/Extractor/Declaration/ExistsExtractor.php @@ -0,0 +1,23 @@ +createReflectionProvider(),