Skip to content
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

Implement AssertEqualsIsDiscouragedRule #216

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ It also contains this strict framework-specific rules (can be enabled separately
* Check that you are not using `assertSame()` with `false` as expected value. `assertFalse()` should be used instead.
* Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead.
* 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)

## How to document mock objects in phpDocs?

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^2.0"
"phpstan/phpstan": "^2.0.4"
},
"conflict": {
"phpunit/phpunit": "<7.0"
Expand Down
7 changes: 7 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ services:
deprecationRulesInstalled: %deprecationRulesInstalled%
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule
arguments:
strictRulesInstalled: %strictRulesInstalled%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather use conditionalTags instead of passing the parameter in the rule.

Also: we can do it only on bleeding edge. Conditional tags can do and condition based on multiple parameters since phpstan/phpstan-src#1697 (see the tests).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, thanks

tags:
- phpstan.rules.rule
77 changes: 77 additions & 0 deletions src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PhpParser\Node;
use PhpParser\NodeAbstract;
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<NodeAbstract>
*/
class AssertEqualsIsDiscouragedRule implements Rule
{

private bool $strictRulesInstalled;

public function __construct(
bool $strictRulesInstalled
)
{
$this->strictRulesInstalled = $strictRulesInstalled;
}

public function getNodeType(): string
{
return NodeAbstract::class;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like they have a more specific class parent than just NodeAbstract. Feel free to check out and optimize other similar rules as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed here and will do for more rules in a separate PR with a 1.4.x target in #217

}

public function processNode(Node $node, Scope $scope): array
{
if (!$this->strictRulesInstalled) {
return [];
}

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 [];
}

}
38 changes: 38 additions & 0 deletions tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php
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(true);
}

}
37 changes: 37 additions & 0 deletions tests/Rules/PHPUnit/data/assert-equals-is-discouraged.php
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());
}
}
Loading