Skip to content

Commit ef39c54

Browse files
ging-devondrejmirtes
authored andcommitted
loop optimization
1 parent c013219 commit ef39c54

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/Type/TypeCombinator.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,16 +709,18 @@ private static function reduceArrays(array $constantArrays): array
709709
$arraysToProcess[] = $constantArray;
710710
}
711711

712-
for ($i = 0; $i < count($arraysToProcess); $i++) {
713-
for ($j = $i + 1; $j < count($arraysToProcess); $j++) {
712+
for ($i = 0, $arraysToProcessCount = count($arraysToProcess); $i < $arraysToProcessCount; $i++) {
713+
for ($j = $i + 1; $j < $arraysToProcessCount; $j++) {
714714
if ($arraysToProcess[$j]->isKeysSupersetOf($arraysToProcess[$i])) {
715715
$arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
716716
array_splice($arraysToProcess, $i--, 1);
717+
$arraysToProcessCount--;
717718
continue 2;
718719

719720
} elseif ($arraysToProcess[$i]->isKeysSupersetOf($arraysToProcess[$j])) {
720721
$arraysToProcess[$i] = $arraysToProcess[$i]->mergeWith($arraysToProcess[$j]);
721722
array_splice($arraysToProcess, $j--, 1);
723+
$arraysToProcessCount--;
722724
continue 1;
723725
}
724726
}
@@ -789,15 +791,17 @@ public static function intersect(Type ...$types): Type
789791

790792
return $union;
791793
}
794+
$typesCount = count($types);
792795

793796
// transform A & (B & C) to A & B & C
794-
for ($i = 0; $i < count($types); $i++) {
797+
for ($i = 0; $i < $typesCount; $i++) {
795798
$type = $types[$i];
796799
if (!($type instanceof IntersectionType)) {
797800
continue;
798801
}
799802

800803
array_splice($types, $i--, 1, $type->getTypes());
804+
$typesCount = count($types);
801805
}
802806

803807
// transform IntegerType & ConstantIntegerType to ConstantIntegerType
@@ -808,7 +812,6 @@ public static function intersect(Type ...$types): Type
808812
// transform callable & int to never
809813
// transform A & ~A to never
810814
// transform int & string to never
811-
$typesCount = count($types);
812815
for ($i = 0; $i < $typesCount; $i++) {
813816
for ($j = $i + 1; $j < $typesCount; $j++) {
814817
if ($types[$j] instanceof SubtractableType) {

0 commit comments

Comments
 (0)