Skip to content

Commit a76dc4a

Browse files
committed
Add Type::spliceArray(), improve splice_array() array type narrowing
1 parent 9ffae27 commit a76dc4a

21 files changed

+480
-22
lines changed

Diff for: src/Analyser/NodeScopeResolver.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -2675,19 +2675,20 @@ static function (): void {
26752675
if (
26762676
$functionReflection !== null
26772677
&& $functionReflection->getName() === 'array_splice'
2678-
&& count($expr->getArgs()) >= 1
2678+
&& count($expr->getArgs()) >= 2
26792679
) {
26802680
$arrayArg = $expr->getArgs()[0]->value;
26812681
$arrayArgType = $scope->getType($arrayArg);
2682-
$valueType = $arrayArgType->getIterableValueType();
2683-
if (count($expr->getArgs()) >= 4) {
2684-
$replacementType = $scope->getType($expr->getArgs()[3]->value)->toArray();
2685-
$valueType = TypeCombinator::union($valueType, $replacementType->getIterableValueType());
2686-
}
2682+
$arrayArgNativeType = $scope->getNativeType($arrayArg);
2683+
2684+
$offsetType = $scope->getType($expr->getArgs()[1]->value);
2685+
$lengthType = isset($expr->getArgs()[2]) ? $scope->getType($expr->getArgs()[2]->value) : new NullType();
2686+
$replacementType = isset($expr->getArgs()[3]) ? $scope->getType($expr->getArgs()[3]->value) : new ConstantArrayType([], []);
2687+
26872688
$scope = $scope->invalidateExpression($arrayArg)->assignExpression(
26882689
$arrayArg,
2689-
new ArrayType($arrayArgType->getIterableKeyType(), $valueType),
2690-
new ArrayType($arrayArgType->getIterableKeyType(), $valueType),
2690+
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
2691+
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
26912692
);
26922693
}
26932694

Diff for: 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();

Diff for: 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();

Diff for: 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();

Diff for: src/Type/Accessory/NonEmptyArrayType.php

+12
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
226226
return new MixedType();
227227
}
228228

229+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
230+
{
231+
if (
232+
(new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()
233+
|| $replacementType->toArray()->isIterableAtLeastOnce()->yes()
234+
) {
235+
return $this;
236+
}
237+
238+
return new MixedType();
239+
}
240+
229241
public function isIterable(): TrinaryLogic
230242
{
231243
return TrinaryLogic::createYes();

Diff for: 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();

Diff for: src/Type/ArrayType.php

+21
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,27 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
449449
return $this;
450450
}
451451

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

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

+101-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
945945
}
946946

947947
if ($keyTypesCount + $offset <= 0) {
948-
// A negative offset cannot reach left outside the array
948+
// A negative offset cannot reach left outside the array twice
949949
$offset = 0;
950950
}
951951

@@ -1006,6 +1006,106 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
10061006
return $builder->getArray();
10071007
}
10081008

1009+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
1010+
{
1011+
$keyTypesCount = count($this->keyTypes);
1012+
1013+
$offset = $offsetType instanceof ConstantIntegerType ? $offsetType->getValue() : null;
1014+
1015+
if ($lengthType instanceof ConstantIntegerType) {
1016+
$length = $lengthType->getValue();
1017+
} elseif ($lengthType->isNull()->yes()) {
1018+
$length = $keyTypesCount;
1019+
} else {
1020+
$length = null;
1021+
}
1022+
1023+
if (
1024+
$offset === null || $length === null
1025+
|| (count($this->optionalKeys) > 0 && ($offset < 0 || $length < 0)) // Negative offsets or lengths with optional keys are not supported yet
1026+
) {
1027+
return $this->degradeToGeneralArray()
1028+
->spliceArray($offsetType, $lengthType, $replacementType);
1029+
}
1030+
1031+
if ($keyTypesCount + $offset <= 0) {
1032+
// A negative offset cannot reach left outside the array twice
1033+
$offset = 0;
1034+
}
1035+
1036+
if ($keyTypesCount + $length <= 0) {
1037+
// A negative length cannot reach left outside the array twice
1038+
$length = 0;
1039+
}
1040+
1041+
if ($offset < 0) {
1042+
$offset = $keyTypesCount + $offset;
1043+
}
1044+
1045+
if ($length < 0) {
1046+
$length = $keyTypesCount - $offset + $length;
1047+
}
1048+
1049+
$removeKeysCount = 0;
1050+
$optionalKeysIgnored = 0;
1051+
$optionalKeysBeforeReplacement = 0;
1052+
$builder = ConstantArrayTypeBuilder::createEmpty();
1053+
for ($i = 0;; $i++) {
1054+
$isOptional = $this->isOptionalKey($i);
1055+
1056+
if ($i < $offset && $isOptional) {
1057+
$optionalKeysIgnored++;
1058+
$optionalKeysBeforeReplacement++;
1059+
}
1060+
1061+
if ($i === $offset + $optionalKeysBeforeReplacement) {
1062+
// When the offset is reached we have to a) put the replacement array in and b) remove $length elements
1063+
$removeKeysCount = $length;
1064+
$optionalKeysIgnored += $optionalKeysBeforeReplacement;
1065+
1066+
$replacementArrayType = $replacementType->toArray();
1067+
$constantArrays = $replacementArrayType->getConstantArrays();
1068+
if (count($constantArrays) === 1) {
1069+
$valuesArray = $constantArrays[0]->getValuesArray();
1070+
for ($j = 0, $jMax = count($valuesArray->keyTypes); $j < $jMax; $j++) {
1071+
$builder->setOffsetValueType(null, $valuesArray->valueTypes[$j], $valuesArray->isOptionalKey($j));
1072+
}
1073+
} else {
1074+
$builder->degradeToGeneralArray();
1075+
$builder->setOffsetValueType($replacementArrayType->getValuesArray()->getIterableKeyType(), $replacementArrayType->getIterableValueType(), true);
1076+
}
1077+
}
1078+
1079+
if (!isset($this->keyTypes[$i])) {
1080+
break;
1081+
}
1082+
1083+
if ($removeKeysCount > 0) {
1084+
$removeKeysCount--;
1085+
1086+
if ($optionalKeysIgnored === 0) {
1087+
if ($isOptional && $removeKeysCount === 0) {
1088+
$optionalKeysIgnored++;
1089+
}
1090+
continue;
1091+
}
1092+
}
1093+
1094+
if (!$isOptional && $optionalKeysIgnored > 0) {
1095+
$optionalKeysIgnored--;
1096+
$isOptional = true;
1097+
}
1098+
1099+
$builder->setOffsetValueType(
1100+
$this->keyTypes[$i]->isInteger()->no() ? $this->keyTypes[$i] : null,
1101+
$this->valueTypes[$i],
1102+
$isOptional,
1103+
);
1104+
}
1105+
1106+
return $builder->getArray();
1107+
}
1108+
10091109
public function isIterableAtLeastOnce(): TrinaryLogic
10101110
{
10111111
$keysCount = count($this->keyTypes);

Diff for: src/Type/IntersectionType.php

+5
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,11 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
899899
return $this->intersectTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys));
900900
}
901901

902+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
903+
{
904+
return $this->intersectTypes(static fn (Type $type): Type => $type->spliceArray($offsetType, $lengthType, $replacementType));
905+
}
906+
902907
public function getEnumCases(): array
903908
{
904909
$compare = [];

Diff for: 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) {

Diff for: 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();

Diff for: 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();

Diff for: 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();

Diff for: 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
}

Diff for: 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
}

Diff for: 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
*/

Diff for: 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)