Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Feb 6, 2023
1 parent 3772b73 commit 39766bf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
39 changes: 30 additions & 9 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -1358,16 +1358,37 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType

private function looseCompareConstantScalars(ConstantScalarType $leftType, ConstantScalarType $rightType): BooleanType
{
$isNumber = new UnionType([
new IntegerType(),
new FloatType(),
]);
if ($this->phpVersion->castsNumbersToStringsOnLooseComparison()) {
$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());
if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $isNumber->isSuperTypeOf($rightType)->yes()) {
$stringValue = (string) $rightType->getValue();
return new ConstantBooleanType($stringValue === $leftType->getValue());
}
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $isNumber->isSuperTypeOf($leftType)->yes()) {
$stringValue = (string) $leftType->getValue();
return new ConstantBooleanType($stringValue === $rightType->getValue());
}
} else {
if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $rightType->isFloat()->yes()) {
$numericPart = (float) $leftType->getValue();
return new ConstantBooleanType($numericPart === $rightType->getValue());
}
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $leftType->isFloat()->yes()) {
$numericPart = (float) $rightType->getValue();
return new ConstantBooleanType($numericPart === $leftType->getValue());
}
if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $rightType->isInteger()->yes()) {
$numericPart = (int) $leftType->getValue();
return new ConstantBooleanType($numericPart === $rightType->getValue());
}
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $leftType->isInteger()->yes()) {
$numericPart = (int) $rightType->getValue();
return new ConstantBooleanType($numericPart === $leftType->getValue());
}
}

// @phpstan-ignore-next-line
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/PHPStanTestCase.php
Expand Up @@ -175,7 +175,7 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
$container->getByType(PhpVersion::class),
$container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class),
new OversizedArrayBuilder(),
$container->getParameter('usePathConstantsAsConstantString')
$container->getParameter('usePathConstantsAsConstantString'),
),
$container->getByType(DynamicReturnTypeExtensionRegistryProvider::class),
$container->getByType(ExprPrinter::class),
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/LooseConstComparisonPhp7Test.php
Expand Up @@ -12,6 +12,8 @@ class LooseConstComparisonPhp7Test extends TypeInferenceTestCase
*/
public function dataFileAsserts(): iterable
{
// compares constants according to the php-version phpstan configuration,
// _NOT_ the current php runtime version
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-const-comparison-php7.php');
}

Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/LooseConstComparisonPhp8Test.php
Expand Up @@ -12,6 +12,8 @@ class LooseConstComparisonPhp8Test extends TypeInferenceTestCase
*/
public function dataFileAsserts(): iterable
{
// compares constants according to the php-version phpstan configuration,
// _NOT_ the current php runtime version
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-const-comparison-php8.php');
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/data/loose-const-comparison-php7.php
Expand Up @@ -14,4 +14,7 @@ function doFoo() {

assertType('true', 0.0 == "");
assertType('true', 42.0 == "42foo");
assertType('true', 42 == "42.0foo");
assertType('false', 42.1 == "42.0foo");
assertType('true', 42.0 == "42.0foo");
}
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/data/loose-const-comparison-php8.php
Expand Up @@ -14,4 +14,7 @@ function doFoo() {

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

0 comments on commit 39766bf

Please sign in to comment.