Skip to content

Commit 72472dd

Browse files
committed
ConstantLooseComparisonRule - respect treatPhpDocTypesAsCertain
1 parent 40400ae commit 72472dd

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

conf/config.level4.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ services:
155155
class: PHPStan\Rules\Comparison\ConstantLooseComparisonRule
156156
arguments:
157157
checkAlwaysTrueLooseComparison: %checkAlwaysTrueLooseComparison%
158+
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
158159
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
159160

160161
-

src/Rules/Comparison/ConstantLooseComparisonRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ConstantLooseComparisonRule implements Rule
1919

2020
public function __construct(
2121
private bool $checkAlwaysTrueLooseComparison,
22+
private bool $treatPhpDocTypesAsCertain,
2223
private bool $reportAlwaysTrueInLastCondition,
2324
)
2425
{
@@ -35,7 +36,7 @@ public function processNode(Node $node, Scope $scope): array
3536
return [];
3637
}
3738

38-
$nodeType = $scope->getType($node);
39+
$nodeType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
3940
if (!$nodeType instanceof ConstantBooleanType) {
4041
return [];
4142
}

tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ class ConstantLooseComparisonRuleTest extends RuleTestCase
1414

1515
private bool $checkAlwaysTrueStrictComparison;
1616

17+
private bool $treatPhpDocTypesAsCertain = true;
18+
1719
private bool $reportAlwaysTrueInLastCondition = false;
1820

1921
protected function getRule(): Rule
2022
{
21-
return new ConstantLooseComparisonRule($this->checkAlwaysTrueStrictComparison, $this->reportAlwaysTrueInLastCondition);
23+
return new ConstantLooseComparisonRule($this->checkAlwaysTrueStrictComparison, $this->treatPhpDocTypesAsCertain, $this->reportAlwaysTrueInLastCondition);
2224
}
2325

2426
public function testRule(): void
@@ -131,4 +133,26 @@ public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLast
131133
$this->analyse([__DIR__ . '/data/loose-comparison-report-always-true-last-condition.php'], $expectedErrors);
132134
}
133135

136+
public function dataTreatPhpDocTypesAsCertain(): iterable
137+
{
138+
yield [false, []];
139+
yield [true, [
140+
[
141+
'Loose comparison using == between 3 and 3 will always evaluate to true.',
142+
14,
143+
],
144+
]];
145+
}
146+
147+
/**
148+
* @dataProvider dataTreatPhpDocTypesAsCertain
149+
* @param list<array{0: string, 1: int, 2?: string}> $expectedErrors
150+
*/
151+
public function testTreatPhpDocTypesAsCertain(bool $treatPhpDocTypesAsCertain, array $expectedErrors): void
152+
{
153+
$this->checkAlwaysTrueStrictComparison = true;
154+
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
155+
$this->analyse([__DIR__ . '/data/loose-comparison-treat-phpdoc-types.php'], $expectedErrors);
156+
}
157+
134158
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace LooseComparisonTreatPhpDocTypes;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @param 3 $i
10+
* @return void
11+
*/
12+
public function doFoo(int $i): void
13+
{
14+
if ($i == 3) {
15+
16+
}
17+
}
18+
19+
}

0 commit comments

Comments
 (0)