Skip to content

Commit e1204ea

Browse files
committed
Add Type::spliceArray(), improve splice_array() array type narrowing
1 parent e1f4594 commit e1204ea

21 files changed

+470
-21
lines changed

src/Analyser/NodeScopeResolver.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -2670,19 +2670,20 @@ static function (): void {
26702670
if (
26712671
$functionReflection !== null
26722672
&& $functionReflection->getName() === 'array_splice'
2673-
&& count($expr->getArgs()) >= 1
2673+
&& count($expr->getArgs()) >= 2
26742674
) {
26752675
$arrayArg = $expr->getArgs()[0]->value;
26762676
$arrayArgType = $scope->getType($arrayArg);
2677-
$valueType = $arrayArgType->getIterableValueType();
2678-
if (count($expr->getArgs()) >= 4) {
2679-
$replacementType = $scope->getType($expr->getArgs()[3]->value)->toArray();
2680-
$valueType = TypeCombinator::union($valueType, $replacementType->getIterableValueType());
2681-
}
2677+
$arrayArgNativeType = $scope->getNativeType($arrayArg);
2678+
2679+
$offsetType = $scope->getType($expr->getArgs()[1]->value);
2680+
$lengthType = isset($expr->getArgs()[2]) ? $scope->getType($expr->getArgs()[2]->value) : new NullType();
2681+
$replacementType = isset($expr->getArgs()[3]) ? $scope->getType($expr->getArgs()[3]->value) : new ConstantArrayType([], []);
2682+
26822683
$scope = $scope->invalidateExpression($arrayArg)->assignExpression(
26832684
$arrayArg,
2684-
new ArrayType($arrayArgType->getIterableKeyType(), $valueType),
2685-
new ArrayType($arrayArgType->getIterableKeyType(), $valueType),
2685+
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
2686+
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
26862687
);
26872688
}
26882689

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

+91
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,97 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
10161016
return $builder->getArray();
10171017
}
10181018

1019+
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
1020+
{
1021+
$allArrays = $this->getAllArrays();
1022+
if (count($allArrays) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
1023+
return $this->degradeToGeneralArray()
1024+
->spliceArray($offsetType, $lengthType, $replacementType);
1025+
}
1026+
1027+
$types = [];
1028+
foreach ($this->getAllArrays() as $array) {
1029+
$types[] = $array->spliceArrayWithoutOptionalKeys($offsetType, $lengthType, $replacementType);
1030+
}
1031+
1032+
return TypeCombinator::union(...$types);
1033+
}
1034+
1035+
private function spliceArrayWithoutOptionalKeys(Type $offsetType, Type $lengthType, Type $replacementType): Type
1036+
{
1037+
$keyTypesCount = count($this->keyTypes);
1038+
1039+
$offset = $offsetType instanceof ConstantIntegerType ? $offsetType->getValue() : null;
1040+
1041+
if ($lengthType instanceof ConstantIntegerType) {
1042+
$length = $lengthType->getValue();
1043+
} elseif ($lengthType->isNull()->yes()) {
1044+
$length = $keyTypesCount;
1045+
} else {
1046+
$length = null;
1047+
}
1048+
1049+
if ($offset === null || $length === null) {
1050+
return $this->degradeToGeneralArray()
1051+
->spliceArray($offsetType, $lengthType, $replacementType);
1052+
}
1053+
1054+
if ($keyTypesCount + $offset <= 0) {
1055+
// A negative offset cannot reach left outside the array twice
1056+
$offset = 0;
1057+
}
1058+
1059+
if ($keyTypesCount + $length <= 0) {
1060+
// A negative length cannot reach left outside the array twice
1061+
$length = 0;
1062+
}
1063+
1064+
if ($offset < 0) {
1065+
$offset = $keyTypesCount + $offset;
1066+
}
1067+
1068+
if ($length < 0) {
1069+
$length = $keyTypesCount - $offset + $length;
1070+
}
1071+
1072+
$removeKeysCount = 0;
1073+
$builder = ConstantArrayTypeBuilder::createEmpty();
1074+
for ($i = 0;; $i++) {
1075+
if ($i === $offset) {
1076+
// When the offset is reached we have to a) put the replacement array in and b) remove $length elements
1077+
$removeKeysCount = $length;
1078+
1079+
$replacementArrayType = $replacementType->toArray();
1080+
$constantArrays = $replacementArrayType->getConstantArrays();
1081+
if (count($constantArrays) === 1) {
1082+
$valuesArray = $constantArrays[0]->getValuesArray();
1083+
for ($j = 0, $jMax = count($valuesArray->keyTypes); $j < $jMax; $j++) {
1084+
$builder->setOffsetValueType(null, $valuesArray->valueTypes[$j], $valuesArray->isOptionalKey($j));
1085+
}
1086+
} else {
1087+
$builder->degradeToGeneralArray();
1088+
$builder->setOffsetValueType($replacementArrayType->getValuesArray()->getIterableKeyType(), $replacementArrayType->getIterableValueType(), true);
1089+
}
1090+
}
1091+
1092+
if (!isset($this->keyTypes[$i])) {
1093+
break;
1094+
}
1095+
1096+
if ($removeKeysCount > 0) {
1097+
$removeKeysCount--;
1098+
continue;
1099+
}
1100+
1101+
$builder->setOffsetValueType(
1102+
$this->keyTypes[$i]->isInteger()->no() ? $this->keyTypes[$i] : null,
1103+
$this->valueTypes[$i],
1104+
);
1105+
}
1106+
1107+
return $builder->getArray();
1108+
}
1109+
10191110
public function isIterableAtLeastOnce(): TrinaryLogic
10201111
{
10211112
$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)