Skip to content
Open
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
37 changes: 36 additions & 1 deletion src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
use PHPStan\Type\OperatorTypeSpecifyingExtensionRegistry;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringSet;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -1185,12 +1186,22 @@ public function resolveEqualType(Type $leftType, Type $rightType, ?PhpVersions $
return $this->resolveIdenticalType($leftType, $rightType);
}

$versions = $phpVersions !== null ? $phpVersions->getLooseComparisonVersions() : [$this->phpVersion];

// same-type sides cannot coerce - loose equals strict, except for
// two numeric strings, which compare as numbers
if (
($leftType->isString()->yes() && $rightType->isString()->yes() && (self::isNonNumericString($leftType, $versions) || self::isNonNumericString($rightType, $versions)))
|| ($leftType->isInteger()->yes() && $rightType->isInteger()->yes())
) {
return $this->resolveIdenticalType($leftType, $rightType);
}

if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) {
return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): TypeResult => $this->resolveEqualType($leftValueType, $rightValueType, $phpVersions));
}

// constant only when every PHP version agrees
$versions = $phpVersions !== null ? $phpVersions->getLooseComparisonVersions() : [$this->phpVersion];
$result = $leftType->looseCompare($rightType, $versions[0]);
foreach (array_slice($versions, 1) as $version) {
if (!$result->equals($leftType->looseCompare($rightType, $version))) {
Expand Down Expand Up @@ -1227,6 +1238,30 @@ public function resolveEqualType(Type $leftType, Type $rightType, ?PhpVersions $
return new TypeResult($result, []);
}

/**
* Whether the strings are numeric in none of the PHP versions. A constant
* string's isNumericString() follows the PHP version running PHPStan,
* while '1 ' is numeric since PHP 8.0 only.
*
* @param non-empty-list<PhpVersion> $phpVersions
*/
private static function isNonNumericString(Type $type, array $phpVersions): bool
{
if (!$type->isNumericString()->no()) {
return false;
}

foreach ($type->getConstantStrings() as $constantString) {
foreach ($phpVersions as $phpVersion) {
if (StringSet::parseNumericString($constantString->getValue(), $phpVersion) !== null) {
return false;
}
}
}

return true;
}

/**
* @param callable(Type, Type): TypeResult<BooleanType> $valueComparisonCallback
* @return TypeResult<BooleanType>
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/nsrt/loose-comparisons.php
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,16 @@ public function sayIntersection(
}
}

/**
* @param 'abc'|true $stringOrTrue
* @param int<1, 5> $oneToFive
* @param int<10, 20>|true $tenToTwentyOrTrue
*/
public function sameTypeOnOneSideOnly($stringOrTrue, $oneToFive, $tenToTwentyOrTrue): void
{
assertType('true', 'abc' == $stringOrTrue);
assertType('true', $stringOrTrue == 'abc');
assertType('bool', $oneToFive == $tenToTwentyOrTrue);
}

}
Loading
Loading