Skip to content

Commit 1d76e96

Browse files
Improve
1 parent 77b49bf commit 1d76e96

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ public function unsetOffset(Type $offsetType): Type
741741
$k++;
742742
}
743743

744-
$newIsList = $this->isListAfterUnset(
744+
$newIsList = self::isListAfterUnset(
745745
$newKeyTypes,
746746
$newOptionalKeys,
747747
$this->isList,
@@ -758,6 +758,7 @@ public function unsetOffset(Type $offsetType): Type
758758
if (count($constantScalars) > 0) {
759759
$optionalKeys = $this->optionalKeys;
760760

761+
$arrayHasChanged = false;
761762
foreach ($constantScalars as $constantScalar) {
762763
$constantScalar = $constantScalar->toArrayKey();
763764
if (!$constantScalar instanceof ConstantIntegerType && !$constantScalar instanceof ConstantStringType) {
@@ -769,6 +770,7 @@ public function unsetOffset(Type $offsetType): Type
769770
continue;
770771
}
771772

773+
$arrayHasChanged = true;
772774
if (in_array($i, $optionalKeys, true)) {
773775
continue 2;
774776
}
@@ -777,7 +779,11 @@ public function unsetOffset(Type $offsetType): Type
777779
}
778780
}
779781

780-
$newIsList = $this->isListAfterUnset(
782+
if (!$arrayHasChanged) {
783+
return $this;
784+
}
785+
786+
$newIsList = self::isListAfterUnset(
781787
$this->keyTypes,
782788
$optionalKeys,
783789
$this->isList,
@@ -788,15 +794,21 @@ public function unsetOffset(Type $offsetType): Type
788794
}
789795

790796
$optionalKeys = $this->optionalKeys;
797+
$arrayHasChanged = false;
791798
foreach ($this->keyTypes as $i => $keyType) {
792799
if (!$offsetType->isSuperTypeOf($keyType)->yes()) {
793800
continue;
794801
}
802+
$arrayHasChanged = true;
795803
$optionalKeys[] = $i;
796804
}
797805
$optionalKeys = array_values(array_unique($optionalKeys));
798806

799-
$newIsList = $this->isListAfterUnset(
807+
if (!$arrayHasChanged) {
808+
return $this;
809+
}
810+
811+
$newIsList = self::isListAfterUnset(
800812
$this->keyTypes,
801813
$optionalKeys,
802814
$this->isList,
@@ -807,10 +819,10 @@ public function unsetOffset(Type $offsetType): Type
807819
}
808820

809821
/**
810-
* If we're unsetting something that might not be on the array, it might still be a list (with PHPStan definition)
811-
* because the nextAutoIndexes will not change.
822+
* When we're unsetting something not on the array, it will be untouched,
823+
* So the nextAutoIndexes won't change, and the array might still be a list even with PHPStan definition.
812824
*/
813-
private function isListAfterUnset(array $newKeyTypes, array $newOptionalKeys, TrinaryLogic $arrayIsList, bool $unsetOptionalKey): TrinaryLogic
825+
private static function isListAfterUnset(array $newKeyTypes, array $newOptionalKeys, TrinaryLogic $arrayIsList, bool $unsetOptionalKey): TrinaryLogic
814826
{
815827
if (!$unsetOptionalKey || $arrayIsList->no()) {
816828
return TrinaryLogic::createNo();

0 commit comments

Comments
 (0)