Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ parameters:
-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantArrayType is error\-prone and deprecated\. Use Type\:\:getConstantArrays\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 7
count: 5
path: src/Type/Constant/ConstantArrayType.php

-
Expand Down
40 changes: 8 additions & 32 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function getAllArrays(): array
}

$array = $builder->getArray();
if (!$array instanceof ConstantArrayType) {
if (!$array instanceof self) {
throw new ShouldNotHappenException();
}

Expand Down Expand Up @@ -995,15 +995,14 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
$isOptional = true;
}

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

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

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

public function isIterableAtLeastOnce(): TrinaryLogic
Expand Down Expand Up @@ -1149,7 +1148,7 @@ private function removeLastElements(int $length): self
}

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

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

$array = $builder->getArray();
if (!$array instanceof self) {
throw new ShouldNotHappenException();
}

return $array;
}

private function reindex(): self
{
$keyTypes = [];
$autoIndex = 0;

foreach ($this->keyTypes as $keyType) {
if (!$keyType instanceof ConstantIntegerType) {
$keyTypes[] = $keyType;
continue;
}

$keyTypes[] = new ConstantIntegerType($autoIndex);
$autoIndex++;
}

return new self($keyTypes, $this->valueTypes, [$autoIndex], $this->optionalKeys, TrinaryLogic::createYes());
return $builder->getArray();
}

public function toBoolean(): BooleanType
Expand Down
Loading