-
Notifications
You must be signed in to change notification settings - Fork 57
feat: Discourage assert(Not)Empty if "empty" usage is disallowed #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mitelg
wants to merge
1
commit into
phpstan:2.0.x
Choose a base branch
from
mitelg:feat/dissallow-assert-empty
base: 2.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PHPUnit; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\CallLike; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Name; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use function count; | ||
| use function in_array; | ||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * @implements Rule<CallLike> | ||
| */ | ||
| 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) { | ||
| if (!$node->name instanceof Identifier || !in_array($node->name->toLowerString(), ['assertempty', 'assertnotempty'], true)) { | ||
| return []; | ||
| } | ||
| if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { | ||
| return []; | ||
| } | ||
| } elseif ($node instanceof FuncCall) { | ||
| if (!$node->name instanceof Name || !in_array(strtolower($scope->resolveName($node->name)), ['phpunit\\framework\\assertempty', 'phpunit\\framework\\assertnotempty'], true)) { | ||
| return []; | ||
| } | ||
| } else { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message(sprintf( | ||
| '%s() is not allowed. Use more strict assertion.', | ||
| $node instanceof FuncCall ? $node->name->getLast() : $node->name->toString(), | ||
| )) | ||
| ->identifier('empty.notAllowed') | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PHPUnit; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<AssertEmptyIsDiscouragedRule> | ||
| */ | ||
| 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.', 15], | ||
| ['assertNotEmpty() is not allowed. Use more strict assertion.', 16], | ||
| ['assertEmpty() is not allowed. Use more strict assertion.', 17], | ||
| ['assertNotEmpty() is not allowed. Use more strict assertion.', 18], | ||
| ['assertEmpty() is not allowed. Use more strict assertion.', 19], | ||
| ['assertNotEmpty() is not allowed. Use more strict assertion.', 20], | ||
| ]); | ||
| } | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new AssertEmptyIsDiscouragedRule(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace AssertEmptyIsDiscouragedTest; | ||
|
|
||
| use PHPUnit\Framework\Assert; | ||
| use PHPUnit\Framework\TestCase; | ||
| use function PHPUnit\Framework\assertEmpty; | ||
| use function PHPUnit\Framework\assertNotEmpty; | ||
|
|
||
| final class AssertEmptyTest extends TestCase | ||
| { | ||
|
|
||
| public function test(): void | ||
| { | ||
| $this->assertEmpty([]); | ||
| $this->assertNotEmpty([1]); | ||
| Assert::assertEmpty([]); | ||
| static::assertNotEmpty([1]); | ||
| assertEmpty([]); | ||
| assertNotEmpty([1]); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to bind it to the
%strictRules.disallowedEmpty%toggle as well, but seems not be possible 馃