From 0a514433d8c7fa85426067a3714508f717dfe881 Mon Sep 17 00:00:00 2001 From: Anna Damm Date: Fri, 28 Jun 2024 09:53:26 +0200 Subject: [PATCH 1/3] [should-not-exist-rule] add new should not exist rule --- docs/documentation/assertions.md | 3 ++ .../ShouldNotExist/ShouldNotExist.php | 42 ++++++++++++++++ .../ShouldNotExist/ShouldNotExistRule.php | 27 +++++++++++ src/Test/Builder/AssertionStep.php | 8 ++++ .../ShouldNotExist/ShouldNotExistTest.php | 48 +++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php create mode 100644 src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php create mode 100644 tests/unit/rules/ShouldNotExist/ShouldNotExistTest.php diff --git a/docs/documentation/assertions.md b/docs/documentation/assertions.md index 35868f93..e683ab40 100644 --- a/docs/documentation/assertions.md +++ b/docs/documentation/assertions.md @@ -47,6 +47,9 @@ It asserts that the selected classes **do not depend** on the target classes. ## shouldNotConstruct() 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**. + ## shouldApplyAttribute() It asserts that the selected classes **apply** the target attributes. diff --git a/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php new file mode 100644 index 00000000..f25d49ed --- /dev/null +++ b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php @@ -0,0 +1,42 @@ +applyShouldNot($ruleName, $subject, $meetsDeclaration, $tips, $params); + } +} diff --git a/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php new file mode 100644 index 00000000..c7bdf90f --- /dev/null +++ b/src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php @@ -0,0 +1,27 @@ + + */ +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/Test/Builder/AssertionStep.php b/src/Test/Builder/AssertionStep.php index 1d04cbf0..b133202c 100644 --- a/src/Test/Builder/AssertionStep.php +++ b/src/Test/Builder/AssertionStep.php @@ -11,6 +11,7 @@ use PHPat\Rule\Assertion\Declaration\ShouldNotBeAbstract\ShouldNotBeAbstract; use PHPat\Rule\Assertion\Declaration\ShouldNotBeFinal\ShouldNotBeFinal; use PHPat\Rule\Assertion\Declaration\ShouldNotBeReadonly\ShouldNotBeReadonly; +use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExist; use PHPat\Rule\Assertion\Relation\CanOnlyDepend\CanOnlyDepend; use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ShouldApplyAttribute; use PHPat\Rule\Assertion\Relation\ShouldExtend\ShouldExtend; @@ -157,4 +158,11 @@ public function shouldBeInterface(): TipOrBuildStep return new TipOrBuildStep($this->rule); } + + public function shouldNotExist(): TipOrBuildStep + { + $this->rule->assertion = ShouldNotExist::class; + + return new TipOrBuildStep($this->rule); + } } diff --git a/tests/unit/rules/ShouldNotExist/ShouldNotExistTest.php b/tests/unit/rules/ShouldNotExist/ShouldNotExistTest.php new file mode 100644 index 00000000..fbf92b29 --- /dev/null +++ b/tests/unit/rules/ShouldNotExist/ShouldNotExistTest.php @@ -0,0 +1,48 @@ + + * @internal + * @coversNothing + */ +class ShouldNotExistTest extends RuleTestCase +{ + public const RULE_NAME = 'test_FixtureClassShouldNotExist'; + + public function testRule(): void + { + $this->analyse(['tests/fixtures/FixtureClass.php'], [ + [sprintf('%s should not exist', FixtureClass::class), 29], + ]); + } + + protected function getRule(): Rule + { + $testParser = FakeTestParser::create( + self::RULE_NAME, + ShouldNotExist::class, + [new Classname(FixtureClass::class, false)], + [] + ); + + return new ShouldNotExistRule( + new StatementBuilderFactory($testParser), + new Configuration(false, true, false), + $this->createReflectionProvider(), + self::getContainer()->getByType(FileTypeMapper::class) + ); + } +} From 11dc221962cb2280e92273517e07c4e88689977b Mon Sep 17 00:00:00 2001 From: Anna Damm Date: Fri, 28 Jun 2024 11:38:58 +0200 Subject: [PATCH 2/3] [should-not-exist-rule] add ShouldNotExistRule to extension --- extension.neon | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/extension.neon b/extension.neon index c634651e..34dbea22 100644 --- a/extension.neon +++ b/extension.neon @@ -72,7 +72,13 @@ services: tags: - phpstan.rules.rule - # # # # # RELATION RULES # # # # # + # ShouldNotExist rules + - + class: PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExistRule + tags: + - phpstan.rules.rule + + # # # # # RELATION RULES # # # # # # ShouldImplement rules - From c2318660d79703245e806967598d89175ad1954e Mon Sep 17 00:00:00 2001 From: Anna Damm Date: Mon, 1 Jul 2024 09:48:06 +0200 Subject: [PATCH 3/3] [should-not-exist-rule] rename class and move functions to extractor trait --- docs/documentation/assertions.md | 2 +- extension.neon | 2 +- .../Declaration/ShouldNotExist/ExistsRule.php | 15 +++++++++++ .../ShouldNotExist/ShouldNotExist.php | 12 +++++---- .../ShouldNotExist/ShouldNotExistRule.php | 27 ------------------- .../Extractor/Declaration/ExistsExtractor.php | 23 ++++++++++++++++ .../ShouldNotExist/ShouldNotExistTest.php | 4 +-- 7 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 src/Rule/Assertion/Declaration/ShouldNotExist/ExistsRule.php delete mode 100644 src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExistRule.php create mode 100644 src/Rule/Extractor/Declaration/ExistsExtractor.php 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(),