Skip to content

Commit d51f1d6

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

21 files changed

+471
-22
lines changed

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

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
@@ -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();

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
@@ -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());

src/Type/Constant/ConstantArrayType.php

+92-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,97 @@ 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+
$allArrays = $this->getAllArrays();
1012+
if (count($allArrays) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
1013+
return $this->degradeToGeneralArray()
1014+
->spliceArray($offsetType, $lengthType, $replacementType);
1015+
}
1016+
1017+
$types = [];
1018+
foreach ($this->getAllArrays() as $array) {
1019+
$types[] = $array->spliceArrayWithoutOptionalKeys($offsetType, $lengthType, $replacementType);
1020+
}
1021+
1022+
return TypeCombinator::union(...$types);
1023+
}
1024+
1025+
private function spliceArrayWithoutOptionalKeys(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+
if ($offset < 0) {
1055+
$offset = $keyTypesCount + $offset;
1056+
}
1057+
1058+
if ($length < 0) {
1059+
$length = $keyTypesCount - $offset + $length;
1060+
}
1061+
1062+
$removeKeysCount = 0;
1063+
$builder = ConstantArrayTypeBuilder::createEmpty();
1064+
for ($i = 0;; $i++) {
1065+
if ($i === $offset) {
1066+
// When the offset is reached we have to a) put the replacement array in and b) remove $length elements
1067+
$removeKeysCount = $length;
1068+
1069+
$replacementArrayType = $replacementType->toArray();
1070+
$constantArrays = $replacementArrayType->getConstantArrays();
1071+
if (count($constantArrays) === 1) {
1072+
$valuesArray = $constantArrays[0]->getValuesArray();
1073+
for ($j = 0, $jMax = count($valuesArray->keyTypes); $j < $jMax; $j++) {
1074+
$builder->setOffsetValueType(null, $valuesArray->valueTypes[$j], $valuesArray->isOptionalKey($j));
1075+
}
1076+
} else {
1077+
$builder->degradeToGeneralArray();
1078+
$builder->setOffsetValueType($replacementArrayType->getValuesArray()->getIterableKeyType(), $replacementArrayType->getIterableValueType(), true);
1079+
}
1080+
}
1081+
1082+
if (!isset($this->keyTypes[$i])) {
1083+
break;
1084+
}
1085+
1086+
if ($removeKeysCount > 0) {
1087+
$removeKeysCount--;
1088+
continue;
1089+
}
1090+
1091+
$builder->setOffsetValueType(
1092+
$this->keyTypes[$i]->isInteger()->no() ? $this->keyTypes[$i] : null,
1093+
$this->valueTypes[$i],
1094+
);
1095+
}
1096+
1097+
return $builder->getArray();
1098+
}
1099+
10091100
public function isIterableAtLeastOnce(): TrinaryLogic
10101101
{
10111102
$keysCount = count($this->keyTypes);

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 = [];

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)