Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2978d1

Browse files
committedMay 8, 2025
Add Type::spliceArray(), improve splice_array() array type narrowing
1 parent 4b24f0f commit a2978d1

22 files changed

+597
-32
lines changed
 

‎phpstan-baseline.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ parameters:
903903
-
904904
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantArrayType is error\-prone and deprecated\. Use Type\:\:getConstantArrays\(\) instead\.$#'
905905
identifier: phpstanApi.instanceofType
906-
count: 5
906+
count: 6
907907
path: src/Type/Constant/ConstantArrayType.php
908908

909909
-

‎src/Analyser/NodeScopeResolver.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -2678,19 +2678,20 @@ static function (): void {
26782678
if (
26792679
$functionReflection !== null
26802680
&& $functionReflection->getName() === 'array_splice'
2681-
&& count($expr->getArgs()) >= 1
2681+
&& count($expr->getArgs()) >= 2
26822682
) {
26832683
$arrayArg = $expr->getArgs()[0]->value;
26842684
$arrayArgType = $scope->getType($arrayArg);
2685-
$valueType = $arrayArgType->getIterableValueType();
2686-
if (count($expr->getArgs()) >= 4) {
2687-
$replacementType = $scope->getType($expr->getArgs()[3]->value)->toArray();
2688-
$valueType = TypeCombinator::union($valueType, $replacementType->getIterableValueType());
2689-
}
2685+
$arrayArgNativeType = $scope->getNativeType($arrayArg);
2686+
2687+
$offsetType = $scope->getType($expr->getArgs()[1]->value);
2688+
$lengthType = isset($expr->getArgs()[2]) ? $scope->getType($expr->getArgs()[2]->value) : new NullType();
2689+
$replacementType = isset($expr->getArgs()[3]) ? $scope->getType($expr->getArgs()[3]->value) : new ConstantArrayType([], []);
2690+
26902691
$scope = $scope->invalidateExpression($arrayArg)->assignExpression(
26912692
$arrayArg,
2692-
new ArrayType($arrayArgType->getIterableKeyType(), $valueType),
2693-
new ArrayType($arrayArgType->getIterableKeyType(), $valueType),
2693+
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
2694+
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
26942695
);
26952696
}
26962697

‎src/Type/Accessory/AccessoryArrayListType.php

+5
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
248248
return new MixedType();
249249
}
250250

251+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
252+
{
253+
return $this;
254+
}
255+
251256
public function isIterable(): TrinaryLogic
252257
{
253258
return TrinaryLogic::createYes();

‎src/Type/Accessory/HasOffsetType.php

+9
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
214214
return new MixedType();
215215
}
216216

217+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
218+
{
219+
if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
220+
return $this;
221+
}
222+
223+
return new MixedType();
224+
}
225+
217226
public function isIterableAtLeastOnce(): TrinaryLogic
218227
{
219228
return TrinaryLogic::createYes();

‎src/Type/Accessory/HasOffsetValueType.php

+9
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
274274
return new MixedType();
275275
}
276276

277+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
278+
{
279+
if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
280+
return $this;
281+
}
282+
283+
return new MixedType();
284+
}
285+
277286
public function isIterableAtLeastOnce(): TrinaryLogic
278287
{
279288
return TrinaryLogic::createYes();

‎src/Type/Accessory/NonEmptyArrayType.php

+12
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
223223
return new MixedType();
224224
}
225225

226+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
227+
{
228+
if (
229+
(new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()
230+
|| $replacementType->toArray()->isIterableAtLeastOnce()->yes()
231+
) {
232+
return $this;
233+
}
234+
235+
return new MixedType();
236+
}
237+
226238
public function isIterable(): TrinaryLogic
227239
{
228240
return TrinaryLogic::createYes();

‎src/Type/Accessory/OversizedArrayType.php

+5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
214214
return $this;
215215
}
216216

217+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
218+
{
219+
return $this;
220+
}
221+
217222
public function isIterable(): TrinaryLogic
218223
{
219224
return TrinaryLogic::createYes();

‎src/Type/ArrayType.php

+21
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,27 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
453453
return $this;
454454
}
455455

456+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
457+
{
458+
$replacementArrayType = $replacementType->toArray();
459+
$replacementArrayTypeIsIterableAtLeastOnce = $replacementArrayType->isIterableAtLeastOnce();
460+
461+
if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes() && $replacementArrayTypeIsIterableAtLeastOnce->no()) {
462+
return new ConstantArrayType([], []);
463+
}
464+
465+
$arrayType = new self(
466+
TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType()),
467+
TypeCombinator::union($this->getIterableValueType(), $replacementArrayType->getIterableValueType()),
468+
);
469+
470+
if ($replacementArrayTypeIsIterableAtLeastOnce->yes()) {
471+
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
472+
}
473+
474+
return $arrayType;
475+
}
476+
456477
public function isCallable(): TrinaryLogic
457478
{
458479
return TrinaryLogic::createMaybe()->and($this->itemType->isString());

‎src/Type/Constant/ConstantArrayType.php

+102
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,108 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
10221022
return $builder->getArray();
10231023
}
10241024

1025+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
1026+
{
1027+
$keyTypesCount = count($this->keyTypes);
1028+
1029+
$offset = $offsetType instanceof ConstantIntegerType ? $offsetType->getValue() : null;
1030+
1031+
if ($lengthType instanceof ConstantIntegerType) {
1032+
$length = $lengthType->getValue();
1033+
} elseif ($lengthType->isNull()->yes()) {
1034+
$length = $keyTypesCount;
1035+
} else {
1036+
$length = null;
1037+
}
1038+
1039+
if ($offset === null || $length === null) {
1040+
return $this->degradeToGeneralArray()
1041+
->spliceArray($offsetType, $lengthType, $replacementType);
1042+
}
1043+
1044+
if ($keyTypesCount + $offset <= 0) {
1045+
// A negative offset cannot reach left outside the array twice
1046+
$offset = 0;
1047+
}
1048+
1049+
if ($keyTypesCount + $length <= 0) {
1050+
// A negative length cannot reach left outside the array twice
1051+
$length = 0;
1052+
}
1053+
1054+
$offsetWasNegative = false;
1055+
if ($offset < 0) {
1056+
$offsetWasNegative = true;
1057+
$offset = $keyTypesCount + $offset;
1058+
}
1059+
1060+
if ($length < 0) {
1061+
$length = $keyTypesCount - $offset + $length;
1062+
}
1063+
1064+
$extractType = $this->sliceArray($offsetType, $lengthType, TrinaryLogic::createYes());
1065+
1066+
$types = [];
1067+
foreach ($replacementType->toArray()->getArrays() as $replacementArrayType) {
1068+
$removeKeysCount = 0;
1069+
$optionalKeysBeforeReplacement = 0;
1070+
1071+
$builder = ConstantArrayTypeBuilder::createEmpty();
1072+
for ($i = 0;; $i++) {
1073+
$isOptional = $this->isOptionalKey($i);
1074+
1075+
if (!$offsetWasNegative && $i < $offset && $isOptional) {
1076+
$optionalKeysBeforeReplacement++;
1077+
}
1078+
1079+
if ($i === $offset + $optionalKeysBeforeReplacement) {
1080+
// When the offset is reached we have to a) put the replacement array in and b) remove $length elements
1081+
$removeKeysCount = $length;
1082+
1083+
if ($replacementArrayType instanceof self) {
1084+
$valuesArray = $replacementArrayType->getValuesArray();
1085+
for ($j = 0, $jMax = count($valuesArray->keyTypes); $j < $jMax; $j++) {
1086+
$builder->setOffsetValueType(null, $valuesArray->valueTypes[$j], $valuesArray->isOptionalKey($j));
1087+
}
1088+
} else {
1089+
$builder->degradeToGeneralArray();
1090+
$builder->setOffsetValueType($replacementArrayType->getValuesArray()->getIterableKeyType(), $replacementArrayType->getIterableValueType(), true);
1091+
}
1092+
}
1093+
1094+
if (!isset($this->keyTypes[$i])) {
1095+
break;
1096+
}
1097+
1098+
if ($removeKeysCount > 0) {
1099+
$extractTypeHasOffsetValueType = $extractType->hasOffsetValueType($this->keyTypes[$i]);
1100+
1101+
if (
1102+
(!$isOptional && $extractTypeHasOffsetValueType->yes())
1103+
|| ($isOptional && $extractTypeHasOffsetValueType->maybe())
1104+
) {
1105+
$removeKeysCount--;
1106+
continue;
1107+
}
1108+
}
1109+
1110+
if (!$isOptional && $extractType->hasOffsetValueType($this->keyTypes[$i])->maybe()) {
1111+
$isOptional = true;
1112+
}
1113+
1114+
$builder->setOffsetValueType(
1115+
$this->keyTypes[$i]->isInteger()->no() ? $this->keyTypes[$i] : null,
1116+
$this->valueTypes[$i],
1117+
$isOptional,
1118+
);
1119+
}
1120+
1121+
$types[] = $builder->getArray();
1122+
}
1123+
1124+
return TypeCombinator::union(...$types);
1125+
}
1126+
10251127
public function isIterableAtLeastOnce(): TrinaryLogic
10261128
{
10271129
$keysCount = count($this->keyTypes);

‎src/Type/IntersectionType.php

+5
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
910910
return $result;
911911
}
912912

913+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
914+
{
915+
return $this->intersectTypes(static fn (Type $type): Type => $type->spliceArray($offsetType, $lengthType, $replacementType));
916+
}
917+
913918
public function getEnumCases(): array
914919
{
915920
$compare = [];

‎src/Type/MixedType.php

+9
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
287287
return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
288288
}
289289

290+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
291+
{
292+
if ($this->isArray()->no()) {
293+
return new ErrorType();
294+
}
295+
296+
return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
297+
}
298+
290299
public function isCallable(): TrinaryLogic
291300
{
292301
if ($this->subtractedType !== null) {

‎src/Type/NeverType.php

+5
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
333333
return new NeverType();
334334
}
335335

336+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
337+
{
338+
return new NeverType();
339+
}
340+
336341
public function isCallable(): TrinaryLogic
337342
{
338343
return TrinaryLogic::createNo();

‎src/Type/StaticType.php

+5
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
456456
return $this->getStaticObjectType()->sliceArray($offsetType, $lengthType, $preserveKeys);
457457
}
458458

459+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
460+
{
461+
return $this->getStaticObjectType()->spliceArray($offsetType, $lengthType, $replacementType);
462+
}
463+
459464
public function isCallable(): TrinaryLogic
460465
{
461466
return $this->getStaticObjectType()->isCallable();

‎src/Type/Traits/LateResolvableTypeTrait.php

+5
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
308308
return $this->resolve()->sliceArray($offsetType, $lengthType, $preserveKeys);
309309
}
310310

311+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
312+
{
313+
return $this->resolve()->spliceArray($offsetType, $lengthType, $replacementType);
314+
}
315+
311316
public function isCallable(): TrinaryLogic
312317
{
313318
return $this->resolve()->isCallable();

‎src/Type/Traits/MaybeArrayTypeTrait.php

+5
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
9999
return new ErrorType();
100100
}
101101

102+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
103+
{
104+
return new ErrorType();
105+
}
106+
102107
}

‎src/Type/Traits/NonArrayTypeTrait.php

+5
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
9999
return new ErrorType();
100100
}
101101

102+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
103+
{
104+
return new ErrorType();
105+
}
106+
102107
}

‎src/Type/Type.php

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public function shuffleArray(): Type;
158158

159159
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type;
160160

161+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type;
162+
161163
/**
162164
* @return list<EnumCaseObjectType>
163165
*/

‎src/Type/UnionType.php

+5
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
790790
return $this->unionTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys));
791791
}
792792

793+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
794+
{
795+
return $this->unionTypes(static fn (Type $type): Type => $type->spliceArray($offsetType, $lengthType, $replacementType));
796+
}
797+
793798
public function getEnumCases(): array
794799
{
795800
return $this->pickFromTypes(

0 commit comments

Comments
 (0)