Skip to content

Commit dc67ec4

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

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
use PHPStan\Type\Constant\ConstantArrayType;
1414
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1515
use PHPStan\Type\Constant\ConstantBooleanType;
16-
use PHPStan\Type\Constant\ConstantIntegerType;
17-
use PHPStan\Type\Constant\ConstantStringType;
16+
use PHPStan\Type\ConstantScalarType;
1817
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1918
use PHPStan\Type\ErrorType;
2019
use PHPStan\Type\MixedType;
@@ -23,6 +22,8 @@
2322
use PHPStan\Type\TypeCombinator;
2423
use PHPStan\Type\UnionType;
2524
use function count;
25+
use function is_int;
26+
use function is_string;
2627

2728
#[AutowiredService]
2829
final class ArrayCombineFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -114,7 +115,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
114115
/**
115116
* @param array<int, Type> $types
116117
*
117-
* @return array<int, ConstantIntegerType|ConstantStringType>|null
118+
* @return list<ConstantScalarType>|null
118119
*/
119120
private function sanitizeConstantArrayKeyTypes(array $types): ?array
120121
{
@@ -125,14 +126,19 @@ private function sanitizeConstantArrayKeyTypes(array $types): ?array
125126
$type = $type->toString();
126127
}
127128

128-
if (
129-
!$type instanceof ConstantIntegerType
130-
&& !$type instanceof ConstantStringType
131-
) {
129+
$scalars = $type->getConstantScalarTypes();
130+
if (count($scalars) === 0) {
132131
return null;
133132
}
134133

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

138144
return $sanitizedTypes;

0 commit comments

Comments
 (0)