Skip to content

Commit 414a487

Browse files
mitelgstaabm
authored andcommitted
feat: Discourage assert(Not)Empty if "empty" usage is disallowed (#325)
1 parent 3320043 commit 414a487

6 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ jobs:
2828
- script: |
2929
cd e2e/composer-version
3030
composer install
31-
OUTPUT=$(../bashunit -a exit_code "1" "vendor/bin/phpstan analyze test.php --error-format=raw")
31+
OUTPUT=$(../bashunit assert exit_code "1" "vendor/bin/phpstan analyze test.php --error-format=raw")
3232
echo "$OUTPUT"
33-
../bashunit -a contains 'test.php:12:Version requirement <=8.0.0 does not match 8.1.0...8.5.99.' "$OUTPUT"
34-
../bashunit -a contains 'test.php:32:Version requirement ^11.0.0 does not match 12.5.0...12.5.99.' "$OUTPUT"
33+
../bashunit assert contains 'test.php:12:Version requirement <=8.0.0 does not match 8.1.0...8.6.99.' "$OUTPUT"
34+
../bashunit assert contains 'test.php:32:Version requirement ^11.0.0 does not match 12.5.0...12.5.99.' "$OUTPUT"
3535
3636
steps:
3737
- name: Harden the runner (Audit all outbound calls)

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ It also contains this strict framework-specific rules (can be enabled separately
2424
* Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead.
2525
* Check that you are not using `assertEquals()` with same types (`assertSame()` should be used)
2626
* Check that you are not using `assertNotEquals()` with same types (`assertNotSame()` should be used)
27+
* If PHPStan Strict Rules are enabled, PHPUnit's `assertEmpty()` and `assertNotEmpty()` assertions are disallowed as well.
2728

2829
## How to document mock objects in phpDocs?
2930

‎rules.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ rules:
1212
conditionalTags:
1313
PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule:
1414
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]
15+
PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule:
16+
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]
1517

1618
PHPStan\Rules\PHPUnit\DataProviderDataRule:
1719
phpstan.rules.rule: %featureToggles.bleedingEdge%
@@ -39,5 +41,8 @@ services:
3941
-
4042
class: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule
4143

44+
-
45+
class: PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule
46+
4247
-
4348
class: PHPStan\Rules\PHPUnit\DataProviderDataRule
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\CallLike;
7+
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Expr\StaticCall;
9+
use PhpParser\Node\Identifier;
10+
use PHPStan\Analyser\Scope;
11+
use PHPStan\Rules\Rule;
12+
use PHPStan\Rules\RuleErrorBuilder;
13+
use function count;
14+
use function in_array;
15+
use function sprintf;
16+
17+
/**
18+
* @implements Rule<CallLike>
19+
*/
20+
class AssertEmptyIsDiscouragedRule implements Rule
21+
{
22+
23+
public function getNodeType(): string
24+
{
25+
return CallLike::class;
26+
}
27+
28+
public function processNode(Node $node, Scope $scope): array
29+
{
30+
if (!($node instanceof MethodCall) && !($node instanceof StaticCall)) {
31+
return [];
32+
}
33+
34+
if ($node->isFirstClassCallable() || count($node->getArgs()) < 1) {
35+
return [];
36+
}
37+
38+
if (!$node->name instanceof Identifier || !in_array($node->name->toLowerString(), ['assertempty', 'assertnotempty'], true)) {
39+
return [];
40+
}
41+
42+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
43+
return [];
44+
}
45+
46+
return [
47+
RuleErrorBuilder::message(sprintf('%s() is not allowed. Use more strict assertion.', $node->name->toString()))
48+
->identifier('empty.notAllowed')
49+
->build(),
50+
];
51+
}
52+
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<AssertEmptyIsDiscouragedRule>
10+
*/
11+
final class AssertEmptyIsDiscouragedRuleTest extends RuleTestCase
12+
{
13+
14+
public function testRule(): void
15+
{
16+
$this->analyse([__DIR__ . '/data/assert-empty-is-discouraged.php'], [
17+
['assertEmpty() is not allowed. Use more strict assertion.', 13],
18+
['assertNotEmpty() is not allowed. Use more strict assertion.', 14],
19+
['assertEmpty() is not allowed. Use more strict assertion.', 15],
20+
['assertNotEmpty() is not allowed. Use more strict assertion.', 16],
21+
]);
22+
}
23+
24+
protected function getRule(): Rule
25+
{
26+
return new AssertEmptyIsDiscouragedRule();
27+
}
28+
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertEmptyIsDiscouragedTest;
4+
5+
use PHPUnit\Framework\Assert;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class AssertEmptyTest extends TestCase
9+
{
10+
11+
public function test(string $bar): void
12+
{
13+
$this->assertEmpty([]);
14+
$this->assertNotEmpty([1]);
15+
Assert::assertEmpty([]);
16+
static::assertNotEmpty([1]);
17+
static::assertSame('foo', $bar);
18+
static::assertEquals(1, '1');
19+
}
20+
21+
}

0 commit comments

Comments
 (0)