Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Feb 5, 2023
1 parent 59eafed commit 0c903fc
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Php/PhpVersion.php
Expand Up @@ -216,4 +216,10 @@ public function arrayFunctionsReturnNullWithNonArray(): bool
return $this->versionId < 80000;
}

// see https://www.php.net/manual/en/migration80.incompatible.php#migration80.incompatible.core.string-number-comparision
public function castsNumbersToStringsOnLooseComparison(): bool
{
return $this->versionId >= 80000;
}

}
22 changes: 20 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Reflection;

use Hoa\Stream\Test\Unit\IStream\In;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
Expand Down Expand Up @@ -1346,8 +1347,7 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
}

if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) {
// @phpstan-ignore-next-line
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore
return $this->looseCompareConstantScalars($leftType, $rightType);
}

if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) {
Expand All @@ -1357,6 +1357,24 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
return new BooleanType();
}

private function looseCompareConstantScalars(ConstantScalarType $leftType, ConstantScalarType $rightType): BooleanType
{
$isNumber = new UnionType([
new IntegerType(),
new FloatType()
]);

if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $isNumber->isSuperTypeOf($rightType)->yes()) {
return new ConstantBooleanType(!$this->phpVersion->castsNumbersToStringsOnLooseComparison());
}
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $isNumber->isSuperTypeOf($leftType)->yes()) {
return new ConstantBooleanType(!$this->phpVersion->castsNumbersToStringsOnLooseComparison());
}

// @phpstan-ignore-next-line
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore
}

/**
* @param callable(Type, Type): BooleanType $valueComparisonCallback
*/
Expand Down
1 change: 0 additions & 1 deletion tests/PHPStan/Analyser/LooseConstComparisonPhp7Test.php
Expand Up @@ -31,7 +31,6 @@ public function testFileAsserts(
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../conf/bleedingEdge.neon',
__DIR__ . '/looseConstComparisonPhp7.neon',
];
}
Expand Down
1 change: 0 additions & 1 deletion tests/PHPStan/Analyser/LooseConstComparisonPhp8Test.php
Expand Up @@ -31,7 +31,6 @@ public function testFileAsserts(
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../conf/bleedingEdge.neon',
__DIR__ . '/looseConstComparisonPhp8.neon',
];
}
Expand Down
Expand Up @@ -13,4 +13,5 @@ function doFoo() {
assertType('true', 42 == "42foo");

assertType('true', 0.0 == "");
assertType('true', 42.0 == "42foo");
}
Expand Up @@ -13,4 +13,5 @@ function doFoo() {
assertType('false', 42 == "42foo");

assertType('false', 0.0 == "");
assertType('false', 42.0 == "42foo");
}

0 comments on commit 0c903fc

Please sign in to comment.