Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 12 additions & 2 deletions src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<BinaryOp>
Expand All @@ -26,7 +28,11 @@ public function processNode(Node $node, Scope $scope): array
if ($node instanceof Equal) {
return [
RuleErrorBuilder::message(
'Loose comparison via "==" is not allowed.',
sprintf(
'Loose comparison via "==" between %s and %s is not allowed.',
$scope->getType($node->left)->describe(VerbosityLevel::typeOnly()),
$scope->getType($node->right)->describe(VerbosityLevel::typeOnly()),
),
)->tip('Use strict comparison via "===" instead.')
->identifier('equal.notAllowed')
->build(),
Expand All @@ -35,7 +41,11 @@ public function processNode(Node $node, Scope $scope): array
if ($node instanceof NotEqual) {
return [
RuleErrorBuilder::message(
'Loose comparison via "!=" is not allowed.',
sprintf(
'Loose comparison via "!=" between %s and %s is not allowed.',
$scope->getType($node->left)->describe(VerbosityLevel::typeOnly()),
$scope->getType($node->right)->describe(VerbosityLevel::typeOnly()),
),
)->tip('Use strict comparison via "!==" instead.')
->identifier('notEqual.notAllowed')
->build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,45 @@ public function testRule(): void
{
$this->analyse([__DIR__ . '/data/weak-comparison.php'], [
[
'Loose comparison via "==" is not allowed.',
'Loose comparison via "==" between int and int is not allowed.',
3,
'Use strict comparison via "===" instead.',
],
[
'Loose comparison via "!=" is not allowed.',
'Loose comparison via "!=" between int and int is not allowed.',
5,
'Use strict comparison via "!==" instead.',
],
[
'Loose comparison via "==" between DateTime and float is not allowed.',
8,
'Use strict comparison via "===" instead.',
],
[
'Loose comparison via "!=" between float and DateTime is not allowed.',
10,
'Use strict comparison via "!==" instead.',
],
[
'Loose comparison via "==" between DateTime and null is not allowed.',
13,
'Use strict comparison via "===" instead.',
],
[
'Loose comparison via "!=" between null and DateTime is not allowed.',
15,
'Use strict comparison via "!==" instead.',
],
[
'Loose comparison via "==" between DateTime and DateTime is not allowed.',
18,
'Use strict comparison via "===" instead.',
],
[
'Loose comparison via "!=" between DateTime and DateTime is not allowed.',
20,
'Use strict comparison via "!==" instead.',
],
]);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Rules/DisallowedConstructs/data/weak-comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,18 @@
$bool2 = 123 === 456;
$bool3 = 123 != 456;
$bool4 = 123 !== 456;

$bool5 = new DateTime('now') == 1.23;
$bool6 = new DateTime('now') === 1.23;
$bool7 = 1.23 != new DateTime('now');
$bool8 = 1.23 !== new DateTime('now');

$bool9 = new DateTime('now') == null;
$bool10 = new DateTime('now') === null;
$bool11 = null != new DateTime('now');
$bool12 = null !== new DateTime('now');

$bool13 = new DateTime('now') == new DateTime('now');
$bool14 = new DateTime('now') === new DateTime('now');
$bool15 = new DateTime('now') != new DateTime('now');
$bool16 = new DateTime('now') !== new DateTime('now');