Skip to content

Commit 4432832

Browse files
authored
Faster ArrayType::hasOffsetValueType()
1 parent 0fbb507 commit 4432832

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Type/ArrayType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,12 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
268268

269269
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
270270
{
271-
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
272-
$offsetType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
271+
$offsetArrayKeyType = $offsetType->toArrayKey();
272+
if ($offsetArrayKeyType instanceof ErrorType) {
273+
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
274+
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
275+
}
276+
$offsetType = $offsetArrayKeyType;
273277

274278
if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
275279
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())

src/Type/Constant/ConstantArrayType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,11 @@ public function findTypeAndMethodNames(): array
582582

583583
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
584584
{
585-
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
586-
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
585+
$offsetArrayKeyType = $offsetType->toArrayKey();
586+
if ($offsetArrayKeyType instanceof ErrorType) {
587+
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
588+
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
589+
}
587590

588591
return $this->recursiveHasOffsetValueType($offsetArrayKeyType);
589592
}

0 commit comments

Comments
 (0)