Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
Expand Down Expand Up @@ -689,6 +690,15 @@ public function toPhpDocNode(): TypeNode
return new GenericTypeNode(new IdentifierTypeNode('int'), [$min, $max]);
}

public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
{
if ($this->isSmallerThan($type)->yes() || $this->isGreaterThan($type)->yes()) {
return new ConstantBooleanType(false);
}

return parent::looseCompare($type, $phpVersion);
}

/**
* @param mixed[] $properties
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
}

if ($type instanceof CompoundType) {
return $type->looseCompare($this, $phpVersion);
}

return new BooleanType();
}

Expand Down
4 changes: 4 additions & 0 deletions src/Type/Traits/ConstantScalarTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
}

if ($type instanceof CompoundType) {
return $type->looseCompare($this, $phpVersion);
}

return parent::looseCompare($type, $phpVersion);
}

Expand Down
67 changes: 67 additions & 0 deletions tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,71 @@ public function testTreatPhpDocTypesAsCertain(bool $treatPhpDocTypesAsCertain, a
$this->analyse([__DIR__ . '/data/loose-comparison-treat-phpdoc-types.php'], $expectedErrors);
}

public function testBug11694(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/bug-11694.php'], [
[
'Loose comparison using == between 3 and int<10, 20> will always evaluate to false.',
17,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between int<10, 20> and 3 will always evaluate to false.',
18,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between 23 and int<10, 20> will always evaluate to false.',
23,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between int<10, 20> and 23 will always evaluate to false.',
24,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between null and int<10, 20> will always evaluate to false.',
26,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between int<10, 20> and null will always evaluate to false.',
27,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between \' 3\' and int<10, 20> will always evaluate to false.',
32,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between int<10, 20> and \' 3\' will always evaluate to false.',
33,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between \' 23\' and int<10, 20> will always evaluate to false.',
38,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between int<10, 20> and \' 23\' will always evaluate to false.',
39,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between false and int<10, 20> will always evaluate to false.',
44,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Loose comparison using == between int<10, 20> and false will always evaluate to false.',
45,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}

}
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11694.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bug11694;

/**
* @param int $value
* @return int<10, 20>
*/
function test(int $value) : int {
if ($value > 5) {
return 10;
}

return 20;
}

if (3 == test(3)) {}
if (test(3) == 3) {}

if (13 == test(3)) {}
if (test(3) == 13) {}

if (23 == test(3)) {}
if (test(3) == 23) {}

if (null == test(3)) {}
if (test(3) == null) {}

if ('13foo' == test(3)) {}
if (test(3) == '13foo') {}

if (' 3' == test(3)) {}
if (test(3) == ' 3') {}

if (' 13' == test(3)) {}
if (test(3) == ' 13') {}

if (' 23' == test(3)) {}
if (test(3) == ' 23') {}

if (true == test(3)) {}
if (test(3) == true) {}

if (false == test(3)) {}
if (test(3) == false) {}
Loading