diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 9e316934..50a9d9cd 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -28,10 +28,10 @@ jobs: - script: | cd e2e/composer-version composer install - OUTPUT=$(../bashunit -a exit_code "1" "vendor/bin/phpstan analyze test.php --error-format=raw") + OUTPUT=$(../bashunit assert exit_code "1" "vendor/bin/phpstan analyze test.php --error-format=raw") echo "$OUTPUT" - ../bashunit -a contains 'test.php:12:Version requirement <=8.0.0 does not match 8.1.0...8.5.99.' "$OUTPUT" - ../bashunit -a contains 'test.php:32:Version requirement ^11.0.0 does not match 12.5.0...12.5.99.' "$OUTPUT" + ../bashunit assert contains 'test.php:12:Version requirement <=8.0.0 does not match 8.1.0...8.6.99.' "$OUTPUT" + ../bashunit assert contains 'test.php:32:Version requirement ^11.0.0 does not match 12.5.0...12.5.99.' "$OUTPUT" steps: - name: Harden the runner (Audit all outbound calls) diff --git a/README.md b/README.md index c86df268..34f69785 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ It also contains this strict framework-specific rules (can be enabled separately * Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead. * Check that you are not using `assertEquals()` with same types (`assertSame()` should be used) * Check that you are not using `assertNotEquals()` with same types (`assertNotSame()` should be used) +* When PHPStan Strict Rules' `disallowedEmpty` rule is enabled, disallow PHPUnit's `assertEmpty()` and `assertNotEmpty()` assertions as well. ## How to document mock objects in phpDocs? diff --git a/rules.neon b/rules.neon index 9d2d9477..6d4788d7 100644 --- a/rules.neon +++ b/rules.neon @@ -12,6 +12,8 @@ rules: conditionalTags: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule: phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%] + PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule: + phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%] PHPStan\Rules\PHPUnit\DataProviderDataRule: phpstan.rules.rule: %featureToggles.bleedingEdge% @@ -39,5 +41,8 @@ services: - class: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule + - + class: PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule + - class: PHPStan\Rules\PHPUnit\DataProviderDataRule diff --git a/src/Rules/PHPUnit/AssertEmptyIsDiscouragedRule.php b/src/Rules/PHPUnit/AssertEmptyIsDiscouragedRule.php new file mode 100644 index 00000000..2aa91e0d --- /dev/null +++ b/src/Rules/PHPUnit/AssertEmptyIsDiscouragedRule.php @@ -0,0 +1,53 @@ + + */ +class AssertEmptyIsDiscouragedRule implements Rule +{ + + public function getNodeType(): string + { + return CallLike::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ($node->isFirstClassCallable() || count($node->getArgs()) < 1) { + return []; + } + + if (!($node instanceof MethodCall) && !($node instanceof StaticCall)) { + return []; + } + + if (!$node->name instanceof Identifier || !in_array($node->name->toLowerString(), ['assertempty', 'assertnotempty'], true)) { + return []; + } + + if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + return []; + } + + return [ + RuleErrorBuilder::message(sprintf('%s() is not allowed. Use more strict assertion.', $node->name->toString())) + ->identifier('empty.notAllowed') + ->build(), + ]; + } + +} diff --git a/tests/Rules/PHPUnit/AssertEmptyIsDiscouragedRuleTest.php b/tests/Rules/PHPUnit/AssertEmptyIsDiscouragedRuleTest.php new file mode 100644 index 00000000..156a6e8b --- /dev/null +++ b/tests/Rules/PHPUnit/AssertEmptyIsDiscouragedRuleTest.php @@ -0,0 +1,29 @@ + + */ +final class AssertEmptyIsDiscouragedRuleTest extends RuleTestCase +{ + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/assert-empty-is-discouraged.php'], [ + ['assertEmpty() is not allowed. Use more strict assertion.', 13], + ['assertNotEmpty() is not allowed. Use more strict assertion.', 14], + ['assertEmpty() is not allowed. Use more strict assertion.', 15], + ['assertNotEmpty() is not allowed. Use more strict assertion.', 16], + ]); + } + + protected function getRule(): Rule + { + return new AssertEmptyIsDiscouragedRule(); + } + +} diff --git a/tests/Rules/PHPUnit/data/assert-empty-is-discouraged.php b/tests/Rules/PHPUnit/data/assert-empty-is-discouraged.php new file mode 100644 index 00000000..c6b3924f --- /dev/null +++ b/tests/Rules/PHPUnit/data/assert-empty-is-discouraged.php @@ -0,0 +1,19 @@ +assertEmpty([]); + $this->assertNotEmpty([1]); + Assert::assertEmpty([]); + static::assertNotEmpty([1]); + } + +}