Skip to content

Commit a6f947b

Browse files
committed
more aggressive refactor
1 parent a95e43d commit a6f947b

2 files changed

Lines changed: 45 additions & 85 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ parameters:
456456
-
457457
rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.'
458458
identifier: phpstanApi.instanceofType
459-
count: 22
459+
count: 18
460460
path: src/Reflection/InitializerExprTypeResolver.php
461461

462462
-
@@ -474,7 +474,7 @@ parameters:
474474
-
475475
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
476476
identifier: phpstanApi.instanceofType
477-
count: 10
477+
count: 6
478478
path: src/Reflection/InitializerExprTypeResolver.php
479479

480480
-

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -976,39 +976,10 @@ public function getBitwiseAndTypeFromTypes(Type $leftType, Type $rightType): Typ
976976
return $this->getNeverType($leftType, $rightType);
977977
}
978978

979-
$leftTypes = $this->getFiniteOrConstantScalarTypes($leftType);
980-
$rightTypes = $this->getFiniteOrConstantScalarTypes($rightType);
981-
982-
$leftTypesCount = count($leftTypes);
983-
$rightTypesCount = count($rightTypes);
984-
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
985-
$resultTypes = [];
986-
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
987-
if (!$generalize) {
988-
foreach ($leftTypes as $leftTypeInner) {
989-
foreach ($rightTypes as $rightTypeInner) {
990-
if ($leftTypeInner instanceof ConstantStringType && $rightTypeInner instanceof ConstantStringType) {
991-
$resultType = $this->getTypeFromValue($leftTypeInner->getValue() & $rightTypeInner->getValue());
992-
} else {
993-
$leftNumberType = $leftTypeInner->toNumber();
994-
$rightNumberType = $rightTypeInner->toNumber();
995-
996-
if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
997-
return new ErrorType();
998-
}
999-
1000-
if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) {
1001-
throw new ShouldNotHappenException();
1002-
}
1003-
1004-
$resultType = $this->getTypeFromValue($leftNumberType->getValue() & $rightNumberType->getValue());
1005-
}
1006-
$resultTypes[] = $resultType;
1007-
}
1008-
}
1009-
return TypeCombinator::union(...$resultTypes);
1010-
}
1011-
979+
$result = $this->getFiniteOrConstantScalarTypes($leftType, $rightType, static fn ($a, $b) => $a & $b);
980+
if ($result instanceof Type) {
981+
return $result;
982+
} elseif ($result === self::IS_SCALAR_TYPE) {
1012983
$leftType = $this->optimizeScalarType($leftType);
1013984
$rightType = $this->optimizeScalarType($rightType);
1014985
}
@@ -1063,39 +1034,10 @@ public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type
10631034
return $this->getNeverType($leftType, $rightType);
10641035
}
10651036

1066-
$leftTypes = $this->getFiniteOrConstantScalarTypes($leftType);
1067-
$rightTypes = $this->getFiniteOrConstantScalarTypes($rightType);
1068-
1069-
$leftTypesCount = count($leftTypes);
1070-
$rightTypesCount = count($rightTypes);
1071-
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
1072-
$resultTypes = [];
1073-
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
1074-
if (!$generalize) {
1075-
foreach ($leftTypes as $leftTypeInner) {
1076-
foreach ($rightTypes as $rightTypeInner) {
1077-
if ($leftTypeInner instanceof ConstantStringType && $rightTypeInner instanceof ConstantStringType) {
1078-
$resultType = $this->getTypeFromValue($leftTypeInner->getValue() | $rightTypeInner->getValue());
1079-
} else {
1080-
$leftNumberType = $leftTypeInner->toNumber();
1081-
$rightNumberType = $rightTypeInner->toNumber();
1082-
1083-
if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
1084-
return new ErrorType();
1085-
}
1086-
1087-
if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) {
1088-
throw new ShouldNotHappenException();
1089-
}
1090-
1091-
$resultType = $this->getTypeFromValue($leftNumberType->getValue() | $rightNumberType->getValue());
1092-
}
1093-
$resultTypes[] = $resultType;
1094-
}
1095-
}
1096-
return TypeCombinator::union(...$resultTypes);
1097-
}
1098-
1037+
$result = $this->getFiniteOrConstantScalarTypes($leftType, $rightType, static fn ($a, $b) => $a | $b);
1038+
if ($result instanceof Type) {
1039+
return $result;
1040+
} elseif ($result === self::IS_SCALAR_TYPE) {
10991041
$leftType = $this->optimizeScalarType($leftType);
11001042
$rightType = $this->optimizeScalarType($rightType);
11011043
}
@@ -1134,27 +1076,27 @@ public function getBitwiseXorType(Expr $left, Expr $right, callable $getTypeCall
11341076
return $this->getBitwiseXorTypeFromTypes($leftType, $rightType);
11351077
}
11361078

1079+
private const IS_SCALAR_TYPE = 1;
1080+
private const IS_UNKNOWN = 2;
1081+
11371082
/**
1138-
* @return array<Type>
1083+
* @param callable(bool|float|int|string|null, bool|float|int|string|null):string $operationCallable
1084+
*
1085+
* @return self::IS_UNKNOWN|self::IS_SCALAR_TYPE|Type
11391086
*/
1140-
private function getFiniteOrConstantScalarTypes(Type $type): array
1087+
private function getFiniteOrConstantScalarTypes(Type $leftType, Type $rightType, callable $operationCallable): int|Type
11411088
{
1142-
if ($type instanceof IntegerRangeType) {
1143-
return $type->getFiniteTypes();
1089+
if ($leftType instanceof IntegerRangeType) {
1090+
$leftTypes = $leftType->getFiniteTypes();
1091+
} else {
1092+
$leftTypes = $leftType->getConstantScalarTypes();
11441093
}
1145-
1146-
return $type->getConstantScalarTypes();
1147-
}
1148-
1149-
public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Type
1150-
{
1151-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1152-
return $this->getNeverType($leftType, $rightType);
1094+
if ($rightType instanceof IntegerRangeType) {
1095+
$rightTypes = $rightType->getFiniteTypes();
1096+
} else {
1097+
$rightTypes = $rightType->getConstantScalarTypes();
11531098
}
11541099

1155-
$leftTypes = $this->getFiniteOrConstantScalarTypes($leftType);
1156-
$rightTypes = $this->getFiniteOrConstantScalarTypes($rightType);
1157-
11581100
$leftTypesCount = count($leftTypes);
11591101
$rightTypesCount = count($rightTypes);
11601102
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
@@ -1164,7 +1106,8 @@ public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Typ
11641106
foreach ($leftTypes as $leftTypeInner) {
11651107
foreach ($rightTypes as $rightTypeInner) {
11661108
if ($leftTypeInner instanceof ConstantStringType && $rightTypeInner instanceof ConstantStringType) {
1167-
$resultType = $this->getTypeFromValue($leftTypeInner->getValue() ^ $rightTypeInner->getValue());
1109+
$resultValue = $operationCallable($leftTypeInner->getValue(), $rightTypeInner->getValue());
1110+
$resultType = $this->getTypeFromValue($resultValue);
11681111
} else {
11691112
$leftNumberType = $leftTypeInner->toNumber();
11701113
$rightNumberType = $rightTypeInner->toNumber();
@@ -1177,14 +1120,31 @@ public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Typ
11771120
throw new ShouldNotHappenException();
11781121
}
11791122

1180-
$resultType = $this->getTypeFromValue($leftNumberType->getValue() ^ $rightNumberType->getValue());
1123+
$resultValue = $operationCallable($leftNumberType->getValue(), $rightNumberType->getValue());
1124+
$resultType = $this->getTypeFromValue($resultValue);
11811125
}
11821126
$resultTypes[] = $resultType;
11831127
}
11841128
}
11851129
return TypeCombinator::union(...$resultTypes);
11861130
}
11871131

1132+
return self::IS_SCALAR_TYPE;
1133+
}
1134+
1135+
return self::IS_UNKNOWN;
1136+
}
1137+
1138+
public function getBitwiseXorTypeFromTypes(Type $leftType, Type $rightType): Type
1139+
{
1140+
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1141+
return $this->getNeverType($leftType, $rightType);
1142+
}
1143+
1144+
$result = $this->getFiniteOrConstantScalarTypes($leftType, $rightType, static fn ($a, $b) => $a ^ $b);
1145+
if ($result instanceof Type) {
1146+
return $result;
1147+
} elseif ($result === self::IS_SCALAR_TYPE) {
11881148
$leftType = $this->optimizeScalarType($leftType);
11891149
$rightType = $this->optimizeScalarType($rightType);
11901150
}

0 commit comments

Comments
 (0)