diff --git a/conf/config.level4.neon b/conf/config.level4.neon index 56d89f6237..e7645930ed 100644 --- a/conf/config.level4.neon +++ b/conf/config.level4.neon @@ -155,6 +155,7 @@ services: class: PHPStan\Rules\Comparison\ConstantLooseComparisonRule arguments: checkAlwaysTrueLooseComparison: %checkAlwaysTrueLooseComparison% + treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition% - diff --git a/src/Rules/Comparison/ConstantLooseComparisonRule.php b/src/Rules/Comparison/ConstantLooseComparisonRule.php index d8744aa664..f0348ba23d 100644 --- a/src/Rules/Comparison/ConstantLooseComparisonRule.php +++ b/src/Rules/Comparison/ConstantLooseComparisonRule.php @@ -19,6 +19,7 @@ class ConstantLooseComparisonRule implements Rule public function __construct( private bool $checkAlwaysTrueLooseComparison, + private bool $treatPhpDocTypesAsCertain, private bool $reportAlwaysTrueInLastCondition, ) { @@ -35,7 +36,7 @@ public function processNode(Node $node, Scope $scope): array return []; } - $nodeType = $scope->getType($node); + $nodeType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node); if (!$nodeType instanceof ConstantBooleanType) { return []; } diff --git a/tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php b/tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php index 5078c0ce71..4d9054ca81 100644 --- a/tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php @@ -14,11 +14,13 @@ class ConstantLooseComparisonRuleTest extends RuleTestCase private bool $checkAlwaysTrueStrictComparison; + private bool $treatPhpDocTypesAsCertain = true; + private bool $reportAlwaysTrueInLastCondition = false; protected function getRule(): Rule { - return new ConstantLooseComparisonRule($this->checkAlwaysTrueStrictComparison, $this->reportAlwaysTrueInLastCondition); + return new ConstantLooseComparisonRule($this->checkAlwaysTrueStrictComparison, $this->treatPhpDocTypesAsCertain, $this->reportAlwaysTrueInLastCondition); } public function testRule(): void @@ -131,4 +133,26 @@ public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLast $this->analyse([__DIR__ . '/data/loose-comparison-report-always-true-last-condition.php'], $expectedErrors); } + public function dataTreatPhpDocTypesAsCertain(): iterable + { + yield [false, []]; + yield [true, [ + [ + 'Loose comparison using == between 3 and 3 will always evaluate to true.', + 14, + ], + ]]; + } + + /** + * @dataProvider dataTreatPhpDocTypesAsCertain + * @param list $expectedErrors + */ + public function testTreatPhpDocTypesAsCertain(bool $treatPhpDocTypesAsCertain, array $expectedErrors): void + { + $this->checkAlwaysTrueStrictComparison = true; + $this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain; + $this->analyse([__DIR__ . '/data/loose-comparison-treat-phpdoc-types.php'], $expectedErrors); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/loose-comparison-treat-phpdoc-types.php b/tests/PHPStan/Rules/Comparison/data/loose-comparison-treat-phpdoc-types.php new file mode 100644 index 0000000000..08c8838848 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/loose-comparison-treat-phpdoc-types.php @@ -0,0 +1,19 @@ +