-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AssertEqualsIsDiscouragedRule (#216)
- Loading branch information
Showing
6 changed files
with
148 additions
and
1 deletion.
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
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,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\CallLike; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\GeneralizePrecision; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements Rule<CallLike> | ||
*/ | ||
class AssertEqualsIsDiscouragedRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return CallLike::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { | ||
return []; | ||
} | ||
|
||
if (count($node->getArgs()) < 2) { | ||
return []; | ||
} | ||
if (!$node->name instanceof Node\Identifier || strtolower($node->name->name) !== 'assertequals') { | ||
return []; | ||
} | ||
|
||
$leftType = TypeCombinator::removeNull($scope->getType($node->getArgs()[0]->value)); | ||
$rightType = TypeCombinator::removeNull($scope->getType($node->getArgs()[1]->value)); | ||
|
||
if ($leftType->isConstantScalarValue()->yes()) { | ||
$leftType = $leftType->generalize(GeneralizePrecision::lessSpecific()); | ||
} | ||
if ($rightType->isConstantScalarValue()->yes()) { | ||
$rightType = $rightType->generalize(GeneralizePrecision::lessSpecific()); | ||
} | ||
|
||
if ( | ||
($leftType->isScalar()->yes() && $rightType->isScalar()->yes()) | ||
&& ($leftType->isSuperTypeOf($rightType)->yes()) | ||
&& ($rightType->isSuperTypeOf($leftType)->yes()) | ||
) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type', | ||
)->identifier('phpunit.assertEquals')->build(), | ||
]; | ||
} | ||
|
||
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,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<AssertEqualsIsDiscouragedRule> | ||
*/ | ||
final class AssertEqualsIsDiscouragedRuleTest extends RuleTestCase | ||
{ | ||
|
||
private const ERROR_MESSAGE = 'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type'; | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/assert-equals-is-discouraged.php'], [ | ||
[self::ERROR_MESSAGE, 19], | ||
[self::ERROR_MESSAGE, 22], | ||
[self::ERROR_MESSAGE, 23], | ||
[self::ERROR_MESSAGE, 24], | ||
[self::ERROR_MESSAGE, 25], | ||
[self::ERROR_MESSAGE, 26], | ||
[self::ERROR_MESSAGE, 27], | ||
[self::ERROR_MESSAGE, 28], | ||
[self::ERROR_MESSAGE, 29], | ||
[self::ERROR_MESSAGE, 30], | ||
[self::ERROR_MESSAGE, 32], | ||
]); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new AssertEqualsIsDiscouragedRule(); | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SameOverEqualsTest; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AssertSameOverAssertEqualsRule extends TestCase | ||
{ | ||
public function dummyTest(string $string, int $integer, bool $boolean, float $float, ?string $nullableString): void | ||
{ | ||
$null = null; | ||
|
||
$this->assertSame(5, $integer); | ||
static::assertSame(5, $integer); | ||
|
||
$this->assertEquals('', $string); | ||
$this->assertEquals(null, $string); | ||
static::assertEquals(null, $string); | ||
static::assertEquals($nullableString, $string); | ||
$this->assertEquals(2, $integer); | ||
$this->assertEquals(2.2, $float); | ||
static::assertEquals((int) '2', (int) $string); | ||
$this->assertEquals(true, $boolean); | ||
$this->assertEquals($string, $string); | ||
$this->assertEquals($integer, $integer); | ||
$this->assertEquals($boolean, $boolean); | ||
$this->assertEquals($float, $float); | ||
$this->assertEquals($null, $null); | ||
$this->assertEquals((string) new Exception(), (string) new Exception()); | ||
$this->assertEquals([], []); | ||
$this->assertEquals(new Exception(), new Exception()); | ||
static::assertEquals(new Exception(), new Exception()); | ||
} | ||
} |