Skip to content

Commit 9d19cd0

Browse files
herndlmondrejmirtes
authored andcommitted
Get rid of unnecessary instanceof self in ConstantArrayType
1 parent 14a3b36 commit 9d19cd0

File tree

2 files changed

+9
-33
lines changed

2 files changed

+9
-33
lines changed

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
-

src/Type/Constant/ConstantArrayType.php

+8-32
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function getAllArrays(): array
241241
}
242242

243243
$array = $builder->getArray();
244-
if (!$array instanceof ConstantArrayType) {
244+
if (!$array instanceof self) {
245245
throw new ShouldNotHappenException();
246246
}
247247

@@ -995,15 +995,14 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
995995
$isOptional = true;
996996
}
997997

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

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

1006-
return $preserveKeys->yes() ? $slice : $slice->reindex();
1005+
return $builder->getArray();
10071006
}
10081007

10091008
public function isIterableAtLeastOnce(): TrinaryLogic
@@ -1149,7 +1148,7 @@ private function removeLastElements(int $length): self
11491148
}
11501149

11511150
/** @param positive-int $length */
1152-
private function removeFirstElements(int $length, bool $reindex = true): self
1151+
private function removeFirstElements(int $length, bool $reindex = true): Type
11531152
{
11541153
$builder = ConstantArrayTypeBuilder::createEmpty();
11551154

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

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

12051181
public function toBoolean(): BooleanType

0 commit comments

Comments
 (0)