Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UnionType->looseCompare() #2908

Draft
wants to merge 4 commits into
base: 1.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Type/UnionType.php
Expand Up @@ -601,7 +601,21 @@ public function isScalar(): TrinaryLogic

public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
{
return new BooleanType();
$lastResult = null;
foreach ($this->types as $innerType) {
$result = $innerType->looseCompare($type, $phpVersion);
if ($lastResult === null) {
$lastResult = $result;
continue;
}
if ($lastResult->equals($result)) {
continue;
}

return new BooleanType();
}

return $lastResult ?? new BooleanType();
}

public function isOffsetAccessible(): TrinaryLogic
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Analyser/data/loose-comparisons.php
Expand Up @@ -519,4 +519,37 @@ public function sayEmptyStr(
assertType('false', $emptyStr == $phpStr);
assertType('true', $emptyStr == $emptyStr);
}

/**
* @param true|1|"1" $looseOne
* @param false|0|"0" $looseZero
* @param false|1 $constMix
*/
public function sayConstUnion(
$looseOne,
$looseZero,
$constMix
): void
{
assertType('true', $looseOne == 1);
assertType('false', $looseOne == 0);
assertType('true', $looseOne == true);
assertType('false', $looseOne == false);
assertType('true', $looseOne == "1");
assertType('false', $looseOne == "0");

assertType('false', $looseZero == 1);
assertType('true', $looseZero == 0);
assertType('false', $looseZero == true);
assertType('true', $looseZero == false);
assertType('false', $looseZero == "1");
assertType('true', $looseZero == "0");

assertType('bool', $constMix == 0);
assertType('bool', $constMix == 1);
assertType('bool', $constMix == true);
assertType('bool', $constMix == false);
assertType('bool', $constMix == "1");
assertType('bool', $constMix == "0");
}
}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php
Expand Up @@ -166,4 +166,16 @@ public function testTreatPhpDocTypesAsCertain(bool $treatPhpDocTypesAsCertain, a
$this->analyse([__DIR__ . '/data/loose-comparison-treat-phpdoc-types.php'], $expectedErrors);
}

public function testLooseUnion(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/loose-comparison-union.php'], [
[
'Loose comparison using == between 0|1|false and 2 will always evaluate to false.',
9,
],
]);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/loose-comparison-union.php
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace LooseCompareUnion;

class HelloWorld
{
public function sayHello(string $s): void
{
var_dump(preg_match('{[A-Z]}', $s) == 2);
}
}