Skip to content

Commit bbf7df7

Browse files
committed
Do not use instanceof *Type
1 parent 5f784e4 commit bbf7df7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/Type/BeberleiAssert/AssertHelper.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
use PHPStan\Analyser\TypeSpecifierContext;
2020
use PHPStan\ShouldNotHappenException;
2121
use PHPStan\Type\ArrayType;
22-
use PHPStan\Type\Constant\ConstantArrayType;
2322
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
24-
use PHPStan\Type\Constant\ConstantStringType;
2523
use PHPStan\Type\IterableType;
2624
use PHPStan\Type\MixedType;
2725
use PHPStan\Type\NeverType;
@@ -139,11 +137,12 @@ static function (Type $type): Type {
139137

140138
if ($assertName === 'notIsInstanceOf') {
141139
$classType = $scope->getType($args[1]->value);
142-
if (!$classType instanceof ConstantStringType) {
140+
$constantStrings = $classType->getConstantStrings();
141+
if (count($constantStrings) !== 1) {
143142
return new SpecifiedTypes([], []);
144143
}
145144

146-
$objectType = new ObjectType($classType->getValue());
145+
$objectType = new ObjectType($constantStrings[0]->getValue());
147146
return self::allArrayOrIterable(
148147
$typeSpecifier,
149148
$scope,
@@ -181,14 +180,15 @@ private static function allArrayOrIterable(
181180
if (count($arrayTypes) > 0) {
182181
$newArrayTypes = [];
183182
foreach ($arrayTypes as $arrayType) {
184-
if ($arrayType instanceof ConstantArrayType) {
183+
$constantArrays = $arrayType->getConstantArrays();
184+
if (count($constantArrays) === 1) {
185185
$builder = ConstantArrayTypeBuilder::createEmpty();
186-
foreach ($arrayType->getKeyTypes() as $i => $keyType) {
187-
$valueType = $typeCallback($arrayType->getValueTypes()[$i]);
186+
foreach ($constantArrays[0]->getKeyTypes() as $i => $keyType) {
187+
$valueType = $typeCallback($constantArrays[0]->getValueTypes()[$i]);
188188
if ($valueType instanceof NeverType) {
189189
continue 2;
190190
}
191-
$builder->setOffsetValueType($keyType, $valueType, $arrayType->isOptionalKey($i));
191+
$builder->setOffsetValueType($keyType, $valueType, $constantArrays[0]->isOptionalKey($i));
192192
}
193193
$newArrayTypes[] = $builder->getArray();
194194
} else {
@@ -308,25 +308,27 @@ private static function getExpressionResolvers(): array
308308
},
309309
'isInstanceOf' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr {
310310
$classType = $scope->getType($class->value);
311-
if (!$classType instanceof ConstantStringType) {
311+
$constantStrings = $classType->getConstantStrings();
312+
if (count($constantStrings) !== 1) {
312313
return null;
313314
}
314315

315316
return new Instanceof_(
316317
$expr->value,
317-
new Name($classType->getValue())
318+
new Name($constantStrings[0]->getValue())
318319
);
319320
},
320321
'notIsInstanceOf' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr {
321322
$classType = $scope->getType($class->value);
322-
if (!$classType instanceof ConstantStringType) {
323+
$constantStrings = $classType->getConstantStrings();
324+
if (count($constantStrings) !== 1) {
323325
return null;
324326
}
325327

326328
return new BooleanNot(
327329
new Instanceof_(
328330
$expr->value,
329-
new Name($classType->getValue())
331+
new Name($constantStrings[0]->getValue())
330332
)
331333
);
332334
},

0 commit comments

Comments
 (0)