Skip to content

Commit 8dc2d6c

Browse files
committed
Add operand types to DisallowedLooseComparisonRule error message
1 parent f6f5b3e commit 8dc2d6c

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
12+
use PHPStan\Type\VerbosityLevel;
13+
use function sprintf;
1214

1315
/**
1416
* @implements Rule<BinaryOp>
@@ -26,7 +28,11 @@ public function processNode(Node $node, Scope $scope): array
2628
if ($node instanceof Equal) {
2729
return [
2830
RuleErrorBuilder::message(
29-
'Loose comparison via "==" is not allowed.',
31+
sprintf(
32+
'Loose comparison via "==" between %s and %s is not allowed.',
33+
$scope->getType($node->left)->describe(VerbosityLevel::typeOnly()),
34+
$scope->getType($node->right)->describe(VerbosityLevel::typeOnly()),
35+
),
3036
)->tip('Use strict comparison via "===" instead.')
3137
->identifier('equal.notAllowed')
3238
->build(),
@@ -35,7 +41,11 @@ public function processNode(Node $node, Scope $scope): array
3541
if ($node instanceof NotEqual) {
3642
return [
3743
RuleErrorBuilder::message(
38-
'Loose comparison via "!=" is not allowed.',
44+
sprintf(
45+
'Loose comparison via "!=" between %s and %s is not allowed.',
46+
$scope->getType($node->left)->describe(VerbosityLevel::typeOnly()),
47+
$scope->getType($node->right)->describe(VerbosityLevel::typeOnly()),
48+
),
3949
)->tip('Use strict comparison via "!==" instead.')
4050
->identifier('notEqual.notAllowed')
4151
->build(),

tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,45 @@ public function testRule(): void
2020
{
2121
$this->analyse([__DIR__ . '/data/weak-comparison.php'], [
2222
[
23-
'Loose comparison via "==" is not allowed.',
23+
'Loose comparison via "==" between int and int is not allowed.',
2424
3,
2525
'Use strict comparison via "===" instead.',
2626
],
2727
[
28-
'Loose comparison via "!=" is not allowed.',
28+
'Loose comparison via "!=" between int and int is not allowed.',
2929
5,
3030
'Use strict comparison via "!==" instead.',
3131
],
32+
[
33+
'Loose comparison via "==" between DateTime and float is not allowed.',
34+
8,
35+
'Use strict comparison via "===" instead.',
36+
],
37+
[
38+
'Loose comparison via "!=" between float and DateTime is not allowed.',
39+
10,
40+
'Use strict comparison via "!==" instead.',
41+
],
42+
[
43+
'Loose comparison via "==" between DateTime and null is not allowed.',
44+
13,
45+
'Use strict comparison via "===" instead.',
46+
],
47+
[
48+
'Loose comparison via "!=" between null and DateTime is not allowed.',
49+
15,
50+
'Use strict comparison via "!==" instead.',
51+
],
52+
[
53+
'Loose comparison via "==" between DateTime and DateTime is not allowed.',
54+
18,
55+
'Use strict comparison via "===" instead.',
56+
],
57+
[
58+
'Loose comparison via "!=" between DateTime and DateTime is not allowed.',
59+
20,
60+
'Use strict comparison via "!==" instead.',
61+
],
3262
]);
3363
}
3464

tests/Rules/DisallowedConstructs/data/weak-comparison.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,18 @@
44
$bool2 = 123 === 456;
55
$bool3 = 123 != 456;
66
$bool4 = 123 !== 456;
7+
8+
$bool5 = new DateTime('now') == 1.23;
9+
$bool6 = new DateTime('now') === 1.23;
10+
$bool7 = 1.23 != new DateTime('now');
11+
$bool8 = 1.23 !== new DateTime('now');
12+
13+
$bool9 = new DateTime('now') == null;
14+
$bool10 = new DateTime('now') === null;
15+
$bool11 = null != new DateTime('now');
16+
$bool12 = null !== new DateTime('now');
17+
18+
$bool13 = new DateTime('now') == new DateTime('now');
19+
$bool14 = new DateTime('now') === new DateTime('now');
20+
$bool15 = new DateTime('now') != new DateTime('now');
21+
$bool16 = new DateTime('now') !== new DateTime('now');

0 commit comments

Comments
 (0)