Skip to content

Commit

Permalink
ConstantLooseComparisonRule - respect treatPhpDocTypesAsCertain
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 29, 2023
1 parent 40400ae commit 72472dd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions conf/config.level4.neon
Expand Up @@ -155,6 +155,7 @@ services:
class: PHPStan\Rules\Comparison\ConstantLooseComparisonRule
arguments:
checkAlwaysTrueLooseComparison: %checkAlwaysTrueLooseComparison%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%

-
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Comparison/ConstantLooseComparisonRule.php
Expand Up @@ -19,6 +19,7 @@ class ConstantLooseComparisonRule implements Rule

public function __construct(
private bool $checkAlwaysTrueLooseComparison,
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
)
{
Expand All @@ -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 [];
}
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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<array{0: string, 1: int, 2?: string}> $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);
}

}
@@ -0,0 +1,19 @@
<?php

namespace LooseComparisonTreatPhpDocTypes;

class Foo
{

/**
* @param 3 $i
* @return void
*/
public function doFoo(int $i): void
{
if ($i == 3) {

}
}

}

0 comments on commit 72472dd

Please sign in to comment.