-
Notifications
You must be signed in to change notification settings - Fork 48
AssertEqualsIsDiscouraged #6
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
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 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 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 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,94 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\FloatType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PhpParser\NodeAbstract> | ||
*/ | ||
class AssertEqualsIsDiscouragedRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return \PhpParser\NodeAbstract::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { | ||
return []; | ||
} | ||
|
||
/** @var \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node */ | ||
$node = $node; | ||
|
||
if (count($node->args) < 2) { | ||
return []; | ||
} | ||
if (!$node->name instanceof Node\Identifier || strtolower($node->name->name) !== 'assertequals') { | ||
return []; | ||
} | ||
|
||
$leftType = $scope->getType($node->args[0]->value); | ||
$rightType = $scope->getType($node->args[1]->value); | ||
|
||
if ( | ||
($leftType instanceof BooleanType && $rightType instanceof BooleanType) | ||
|| ($leftType instanceof IntegerType && $rightType instanceof IntegerType) | ||
|| ($leftType instanceof StringType && $rightType instanceof StringType) | ||
) { | ||
$typeDescription = $leftType->describe(VerbosityLevel::typeOnly()); | ||
if ($leftType instanceof BooleanType) { | ||
$typeDescription = 'bool'; | ||
} | ||
return [ | ||
sprintf( | ||
'You should use assertSame instead of assertEquals, because both values are of the same type "%s"', | ||
$typeDescription | ||
), | ||
]; | ||
} | ||
if ( | ||
($leftType instanceof FloatType && $rightType instanceof FloatType) | ||
&& count($node->args) < 4 // is not using delta for comparing floats | ||
) { | ||
return [ | ||
'You should use assertSame instead of assertEquals, because both values are of the same type "float" and you are not using $delta argument', | ||
]; | ||
} | ||
|
||
if (!$node->hasAttribute('comments')) { | ||
return [ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...', | ||
]; | ||
} | ||
|
||
/** @var \PhpParser\Comment[] $comments */ | ||
$comments = $node->getAttribute('comments'); | ||
$comment = $comments[count($comments) - 1]; | ||
|
||
// the comment should be on the line above the assertEquals() | ||
if ($comment->getLine() !== ($node->getLine() - 1)) { | ||
return [ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (The comment is not directly above the assertEquals)', | ||
]; | ||
} | ||
|
||
if (preg_match('~^//\s+assertEquals because(.*)~', $comment->getReformattedText()) === 0) { | ||
return [ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)', | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
This file contains 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,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<AssertEqualsIsDiscouragedRule> | ||
*/ | ||
class AssertEqualsIsDiscouragedRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new AssertEqualsIsDiscouragedRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/assert-equals-is-discouraged.php'], [ | ||
[ | ||
'You should use assertSame instead of assertEquals, because both values are of the same type "string"', | ||
11, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals, because both values are of the same type "int"', | ||
12, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals, because both values are of the same type "bool"', | ||
13, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals, because both values are of the same type "float" and you are not using $delta argument', | ||
16, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)', | ||
19, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...', | ||
21, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)', | ||
24, | ||
], | ||
[ | ||
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (The comment is not directly above the assertEquals)', | ||
28, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains 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,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ExampleTestCase; | ||
|
||
class AssertEqualsIsDiscouragedTestCase extends \PHPUnit\Framework\TestCase | ||
{ | ||
|
||
public function testAssertEqualsIsDiscouraged() | ||
{ | ||
// assertSame can be used as both are of same type | ||
$this->assertEquals('a', 'b'); | ||
$this->assertEquals(1, 2); | ||
$this->assertEquals(true, false); | ||
|
||
// comparing floats without delta | ||
$this->assertEquals(1.0, 2.0); | ||
|
||
// comparing floats with delta | ||
$this->assertEquals(1.0, 2.0, '', 0.01); | ||
|
||
$this->assertEquals(1, '1'); // assertEquals without comment on previous line | ||
|
||
// with incorrect comment | ||
$this->assertEquals(1, '1'); | ||
|
||
// assertEquals because I want it! But sadly, the comment is not just above the assert. | ||
|
||
$this->assertEquals(1, '1'); | ||
|
||
// assertEquals because I want it! | ||
$this->assertEquals(1, '1'); | ||
} | ||
|
||
} |
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.
You might be able to simplify this condition with
Type::isSupersetOf()
and a UnionType consisting of bool, int and string. cc @JanTvrdik might help you with that.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'm not sure if the union would mean the same thing here. Both (left and right) need to be of the same type (not in the union of
bool|int|string
)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.
Maybe union +
\get_class($leftType) === \get_class($rightType)
?