Skip to content

Commit f818ac5

Browse files
herndlmondrejmirtes
authored andcommitted
Get rid of unnecessary instanceof self in ConstantArrayType
1 parent 268309d commit f818ac5

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

Diff for: phpstan-baseline.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ parameters:
882882
-
883883
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantArrayType is error\-prone and deprecated\. Use Type\:\:getConstantArrays\(\) instead\.$#'
884884
identifier: phpstanApi.instanceofType
885-
count: 7
885+
count: 5
886886
path: src/Type/Constant/ConstantArrayType.php
887887

888888
-

Diff for: src/Type/Constant/ConstantArrayType.php

+16-39
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
use function array_merge;
5353
use function array_pop;
5454
use function array_push;
55-
use function array_reverse;
5655
use function array_slice;
5756
use function array_unique;
5857
use function array_values;
@@ -242,7 +241,7 @@ public function getAllArrays(): array
242241
}
243242

244243
$array = $builder->getArray();
245-
if (!$array instanceof ConstantArrayType) {
244+
if (!$array instanceof self) {
246245
throw new ShouldNotHappenException();
247246
}
248247

@@ -858,14 +857,16 @@ public function popArray(): Type
858857

859858
public function reverseArray(TrinaryLogic $preserveKeys): Type
860859
{
861-
$keyTypesReversed = array_reverse($this->keyTypes, true);
862-
$keyTypes = array_values($keyTypesReversed);
863-
$keyTypesReversedKeys = array_keys($keyTypesReversed);
864-
$optionalKeys = array_map(static fn (int $optionalKey): int => $keyTypesReversedKeys[$optionalKey], $this->optionalKeys);
860+
$builder = ConstantArrayTypeBuilder::createEmpty();
865861

866-
$reversed = new self($keyTypes, array_reverse($this->valueTypes), $this->nextAutoIndexes, $optionalKeys, TrinaryLogic::createNo());
862+
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
863+
$offsetType = $preserveKeys->yes() || $this->keyTypes[$i]->isInteger()->no()
864+
? $this->keyTypes[$i]
865+
: null;
866+
$builder->setOffsetValueType($offsetType, $this->valueTypes[$i], $this->isOptionalKey($i));
867+
}
867868

868-
return $preserveKeys->yes() ? $reversed : $reversed->reindex();
869+
return $builder->getArray();
869870
}
870871

871872
public function searchArray(Type $needleType): Type
@@ -994,15 +995,14 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
994995
$isOptional = true;
995996
}
996997

997-
$builder->setOffsetValueType($this->keyTypes[$i], $this->valueTypes[$i], $isOptional);
998-
}
998+
$offsetType = $preserveKeys->yes() || $this->keyTypes[$i]->isInteger()->no()
999+
? $this->keyTypes[$i]
1000+
: null;
9991001

1000-
$slice = $builder->getArray();
1001-
if (!$slice instanceof self) {
1002-
throw new ShouldNotHappenException();
1002+
$builder->setOffsetValueType($offsetType, $this->valueTypes[$i], $isOptional);
10031003
}
10041004

1005-
return $preserveKeys->yes() ? $slice : $slice->reindex();
1005+
return $builder->getArray();
10061006
}
10071007

10081008
public function isIterableAtLeastOnce(): TrinaryLogic
@@ -1148,7 +1148,7 @@ private function removeLastElements(int $length): self
11481148
}
11491149

11501150
/** @param positive-int $length */
1151-
private function removeFirstElements(int $length, bool $reindex = true): self
1151+
private function removeFirstElements(int $length, bool $reindex = true): Type
11521152
{
11531153
$builder = ConstantArrayTypeBuilder::createEmpty();
11541154

@@ -1175,30 +1175,7 @@ private function removeFirstElements(int $length, bool $reindex = true): self
11751175
$builder->setOffsetValueType($keyType, $valueType, $isOptional);
11761176
}
11771177

1178-
$array = $builder->getArray();
1179-
if (!$array instanceof self) {
1180-
throw new ShouldNotHappenException();
1181-
}
1182-
1183-
return $array;
1184-
}
1185-
1186-
private function reindex(): self
1187-
{
1188-
$keyTypes = [];
1189-
$autoIndex = 0;
1190-
1191-
foreach ($this->keyTypes as $keyType) {
1192-
if (!$keyType instanceof ConstantIntegerType) {
1193-
$keyTypes[] = $keyType;
1194-
continue;
1195-
}
1196-
1197-
$keyTypes[] = new ConstantIntegerType($autoIndex);
1198-
$autoIndex++;
1199-
}
1200-
1201-
return new self($keyTypes, $this->valueTypes, [$autoIndex], $this->optionalKeys, TrinaryLogic::createYes());
1178+
return $builder->getArray();
12021179
}
12031180

12041181
public function toBoolean(): BooleanType

0 commit comments

Comments
 (0)