Skip to content

Commit 1c4477f

Browse files
committed
Resolve loose comparison of two strings or two integers as identical
Two strings of which one is not numeric, and two integers, cannot coerce, so their loose comparison is the identical one. resolveEqualType() now answers it through resolveIdenticalType() like the narrowing in IdenticalNarrowingHelper already does, instead of computing the values each side can loosely equal for every PHP version in the configured range. A switch over a string with many case labels or a long chain of `$string == 'constant'` conditions spent most of its time there. Amp-Thread-ID: https://ampcode.com/threads/T-01a1b250-12b7-7206-9d98-c1f38e89010e
1 parent 317da9a commit 1c4477f

3 files changed

Lines changed: 4058 additions & 0 deletions

File tree

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,15 @@ public function resolveEqualType(Type $leftType, Type $rightType, ?PhpVersions $
11851185
return $this->resolveIdenticalType($leftType, $rightType);
11861186
}
11871187

1188+
// same-type sides cannot coerce - loose equals strict, except for
1189+
// two numeric strings, which compare as numbers
1190+
if (
1191+
($leftType->isString()->yes() && $rightType->isString()->yes() && ($leftType->isNumericString()->no() || $rightType->isNumericString()->no()))
1192+
|| ($leftType->isInteger()->yes() && $rightType->isInteger()->yes())
1193+
) {
1194+
return $this->resolveIdenticalType($leftType, $rightType);
1195+
}
1196+
11881197
if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) {
11891198
return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): TypeResult => $this->resolveEqualType($leftValueType, $rightValueType, $phpVersions));
11901199
}

0 commit comments

Comments
 (0)