Skip to content

Commit fdc9ca1

Browse files
committed
Get rid of instanceof *Type checks
1 parent a5aa2ce commit fdc9ca1

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,12 +1521,6 @@ parameters:
15211521
count: 2
15221522
path: src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php
15231523

1524-
-
1525-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
1526-
identifier: phpstanApi.instanceofType
1527-
count: 1
1528-
path: src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php
1529-
15301524
-
15311525
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
15321526
identifier: phpstanApi.instanceofType

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Type\Constant\ConstantBooleanType;
1616
use PHPStan\Type\Constant\ConstantIntegerType;
1717
use PHPStan\Type\Constant\ConstantStringType;
18+
use PHPStan\Type\ConstantScalarType;
1819
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1920
use PHPStan\Type\ErrorType;
2021
use PHPStan\Type\MixedType;
@@ -23,6 +24,8 @@
2324
use PHPStan\Type\TypeCombinator;
2425
use PHPStan\Type\UnionType;
2526
use function count;
27+
use function is_int;
28+
use function is_string;
2629

2730
#[AutowiredService]
2831
final class ArrayCombineFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -114,7 +117,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
114117
/**
115118
* @param array<int, Type> $types
116119
*
117-
* @return array<int, ConstantIntegerType|ConstantStringType>|null
120+
* @return list<ConstantScalarType>|null
118121
*/
119122
private function sanitizeConstantArrayKeyTypes(array $types): ?array
120123
{
@@ -125,14 +128,19 @@ private function sanitizeConstantArrayKeyTypes(array $types): ?array
125128
$type = $type->toString();
126129
}
127130

128-
if (
129-
!$type instanceof ConstantIntegerType
130-
&& !$type instanceof ConstantStringType
131-
) {
131+
$scalars = $type->getConstantScalarTypes();
132+
if (count($scalars) === 0) {
132133
return null;
133134
}
134135

135-
$sanitizedTypes[] = $type;
136+
foreach ($scalars as $scalar) {
137+
$value = $scalar->getValue();
138+
if (!is_int($value) && !is_string($value)) {
139+
return null;
140+
}
141+
142+
$sanitizedTypes[] = $scalar;
143+
}
136144
}
137145

138146
return $sanitizedTypes;

0 commit comments

Comments
 (0)