From 44299db3c574935bc7939a777cb46dd024623965 Mon Sep 17 00:00:00 2001 From: Mikko Pesari Date: Thu, 15 Jan 2026 20:28:12 +0200 Subject: [PATCH] Add operand types to DisallowedLooseComparisonRule error message --- .../DisallowedLooseComparisonRule.php | 14 ++++++-- .../DisallowedLooseComparisonRuleTest.php | 34 +++++++++++++++++-- .../data/weak-comparison.php | 15 ++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php b/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php index 70b8514f..a9f20f2c 100644 --- a/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php +++ b/src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php @@ -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 @@ -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(), @@ -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(), diff --git a/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php b/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php index 9e56b583..99a33771 100644 --- a/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php +++ b/tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php @@ -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.', + ], ]); } diff --git a/tests/Rules/DisallowedConstructs/data/weak-comparison.php b/tests/Rules/DisallowedConstructs/data/weak-comparison.php index 2e7e3a97..0a6a1671 100644 --- a/tests/Rules/DisallowedConstructs/data/weak-comparison.php +++ b/tests/Rules/DisallowedConstructs/data/weak-comparison.php @@ -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');