Skip to content

Commit bd4c3ed

Browse files
committed
[BCB] Parameter $nextAutoIndexes in ConstantArrayType constructor can only be non-empty-list<int>
1 parent a5297b0 commit bd4c3ed

File tree

7 files changed

+50
-64
lines changed

7 files changed

+50
-64
lines changed

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,5 @@ Instead of `PHPStanTestCase::createBroker()`, call `PHPStanTestCase::createRefle
274274
* Remove `TypeUtils::containsCallable()`, use [`Type::isCallable()`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.Type.html#_isCallable) instead
275275
* Removed `Scope::doNotTreatPhpDocTypesAsCertain()`, use `getNativeType()` instead
276276
* Parameter `$isList` in `ConstantArrayType` constructor can only be `TrinaryLogic`, no longer bool
277+
* Parameter `$nextAutoIndexes` in `ConstantArrayType` constructor can only be `non-empty-list<int>`, no longer int
277278
* Remove `ConstantType` interface, use [`Type::isConstantValue()`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.Type.html#_isConstantValue) instead

src/Type/Constant/ConstantArrayType.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
use function count;
5959
use function implode;
6060
use function in_array;
61-
use function is_int;
6261
use function is_string;
6362
use function min;
6463
use function pow;
@@ -87,9 +86,6 @@ class ConstantArrayType implements Type
8786
/** @var self[]|null */
8887
private ?array $allArrays = null;
8988

90-
/** @var non-empty-list<int> */
91-
private array $nextAutoIndexes;
92-
9389
private ?Type $iterableKeyType = null;
9490

9591
private ?Type $iterableValueType = null;
@@ -98,25 +94,19 @@ class ConstantArrayType implements Type
9894
* @api
9995
* @param array<int, ConstantIntegerType|ConstantStringType> $keyTypes
10096
* @param array<int, Type> $valueTypes
101-
* @param non-empty-list<int>|int $nextAutoIndexes
97+
* @param non-empty-list<int> $nextAutoIndexes
10298
* @param int[] $optionalKeys
10399
*/
104100
public function __construct(
105101
private array $keyTypes,
106102
private array $valueTypes,
107-
int|array $nextAutoIndexes = [0],
103+
private array $nextAutoIndexes = [0],
108104
private array $optionalKeys = [],
109105
?TrinaryLogic $isList = null,
110106
)
111107
{
112108
assert(count($keyTypes) === count($valueTypes));
113109

114-
if (is_int($nextAutoIndexes)) {
115-
$nextAutoIndexes = [$nextAutoIndexes];
116-
}
117-
118-
$this->nextAutoIndexes = $nextAutoIndexes;
119-
120110
$keyTypesCount = count($this->keyTypes);
121111
if ($keyTypesCount === 0) {
122112
$isList = TrinaryLogic::createYes();
@@ -201,11 +191,6 @@ public function getNextAutoIndexes(): array
201191
return $this->nextAutoIndexes;
202192
}
203193

204-
private function getNextAutoIndex(): int
205-
{
206-
return $this->nextAutoIndexes[count($this->nextAutoIndexes) - 1];
207-
}
208-
209194
/**
210195
* @return int[]
211196
*/
@@ -1048,7 +1033,7 @@ private function removeLastElements(int $length): self
10481033
$keyTypes = $this->keyTypes;
10491034
$valueTypes = $this->valueTypes;
10501035
$optionalKeys = $this->optionalKeys;
1051-
$nextAutoindex = $this->nextAutoIndexes;
1036+
$nextAutoindexes = $this->nextAutoIndexes;
10521037

10531038
$optionalKeysRemoved = 0;
10541039
$newLength = $keyTypesCount - $length;
@@ -1068,9 +1053,9 @@ private function removeLastElements(int $length): self
10681053

10691054
$removedKeyType = array_pop($keyTypes);
10701055
array_pop($valueTypes);
1071-
$nextAutoindex = $removedKeyType instanceof ConstantIntegerType
1072-
? $removedKeyType->getValue()
1073-
: $this->getNextAutoIndex();
1056+
$nextAutoindexes = $removedKeyType instanceof ConstantIntegerType
1057+
? [$removedKeyType->getValue()]
1058+
: $this->nextAutoIndexes;
10741059
continue;
10751060
}
10761061

@@ -1085,7 +1070,7 @@ private function removeLastElements(int $length): self
10851070
return new self(
10861071
$keyTypes,
10871072
$valueTypes,
1088-
$nextAutoindex,
1073+
$nextAutoindexes,
10891074
array_values($optionalKeys),
10901075
$this->isList,
10911076
);

tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function dataAccepts(): iterable
189189
], [
190190
new StringType(),
191191
new IntegerType(),
192-
], 0, [0, 1]),
192+
], [0], [0, 1]),
193193
new ConstantArrayType([
194194
new ConstantStringType('sorton'),
195195
new ConstantStringType('limit'),
@@ -225,7 +225,7 @@ public function dataAccepts(): iterable
225225
], [
226226
new StringType(),
227227
new IntegerType(),
228-
], 0, [1]),
228+
], [0], [1]),
229229
new ConstantArrayType([
230230
new ConstantStringType('sorton'),
231231
new ConstantStringType('limit'),
@@ -241,7 +241,7 @@ public function dataAccepts(): iterable
241241
new ConstantStringType('limit'),
242242
], [
243243
new IntegerType(),
244-
], 0, [0]),
244+
], [0], [0]),
245245
new ConstantArrayType([
246246
new ConstantStringType('limit'),
247247
], [
@@ -255,7 +255,7 @@ public function dataAccepts(): iterable
255255
new ConstantStringType('limit'),
256256
], [
257257
new IntegerType(),
258-
], 0),
258+
], [0]),
259259
new ConstantArrayType([
260260
new ConstantStringType('limit'),
261261
], [
@@ -271,7 +271,7 @@ public function dataAccepts(): iterable
271271
], [
272272
new StringType(),
273273
new StringType(),
274-
], 0, [0, 1]),
274+
], [0], [0, 1]),
275275
new ConstantArrayType([
276276
new ConstantStringType('sorton'),
277277
new ConstantStringType('limit'),
@@ -289,7 +289,7 @@ public function dataAccepts(): iterable
289289
], [
290290
new StringType(),
291291
new StringType(),
292-
], 0, [0, 1]),
292+
], [0], [0, 1]),
293293
new ConstantArrayType([
294294
new ConstantStringType('color'),
295295
], [
@@ -305,7 +305,7 @@ public function dataAccepts(): iterable
305305
], [
306306
new StringType(),
307307
new StringType(),
308-
], 0, [0, 1]),
308+
], [0], [0, 1]),
309309
new ConstantArrayType([
310310
new ConstantStringType('sound'),
311311
], [
@@ -321,14 +321,14 @@ public function dataAccepts(): iterable
321321
], [
322322
new StringType(),
323323
new StringType(),
324-
], 0, [0, 1]),
324+
], [0], [0, 1]),
325325
new ConstantArrayType([
326326
new ConstantStringType('foo'),
327327
new ConstantStringType('bar'),
328328
], [
329329
new ConstantStringType('s'),
330330
new ConstantStringType('m'),
331-
], 0, [0, 1]),
331+
], [0], [0, 1]),
332332
TrinaryLogic::createYes(),
333333
];
334334

@@ -339,7 +339,7 @@ public function dataAccepts(): iterable
339339
], [
340340
new StringType(),
341341
new IntegerType(),
342-
], 0, [0, 1]),
342+
], [0], [0, 1]),
343343
new ConstantArrayType([
344344
new ConstantStringType('sorton'),
345345
new ConstantStringType('limit'),
@@ -522,7 +522,7 @@ public function dataIsSuperTypeOf(): iterable
522522
], [
523523
new IntegerType(),
524524
new IntegerType(),
525-
], 2),
525+
], [2]),
526526
new ConstantArrayType([], []),
527527
TrinaryLogic::createNo(),
528528
];
@@ -534,7 +534,7 @@ public function dataIsSuperTypeOf(): iterable
534534
], [
535535
new IntegerType(),
536536
new IntegerType(),
537-
], 2, [0]),
537+
], [2], [0]),
538538
new ConstantArrayType([], []),
539539
TrinaryLogic::createNo(),
540540
];
@@ -546,7 +546,7 @@ public function dataIsSuperTypeOf(): iterable
546546
], [
547547
new IntegerType(),
548548
new IntegerType(),
549-
], 2, [0, 1]),
549+
], [2], [0, 1]),
550550
new ConstantArrayType([], []),
551551
TrinaryLogic::createYes(),
552552
];
@@ -558,12 +558,12 @@ public function dataIsSuperTypeOf(): iterable
558558
], [
559559
new IntegerType(),
560560
new IntegerType(),
561-
], 2, [0, 1]),
561+
], [2], [0, 1]),
562562
new ConstantArrayType([
563563
new ConstantStringType('foo'),
564564
], [
565565
new IntegerType(),
566-
], 1, [0]),
566+
], [1], [0]),
567567
TrinaryLogic::createYes(),
568568
];
569569

@@ -579,7 +579,7 @@ public function dataIsSuperTypeOf(): iterable
579579
], [
580580
new IntegerType(),
581581
new IntegerType(),
582-
], 2, [0, 1]),
582+
], [2], [0, 1]),
583583
TrinaryLogic::createMaybe(),
584584
];
585585

@@ -595,7 +595,7 @@ public function dataIsSuperTypeOf(): iterable
595595
], [
596596
new IntegerType(),
597597
new IntegerType(),
598-
], 2, [0, 1]),
598+
], [2], [0, 1]),
599599
TrinaryLogic::createNo(),
600600
];
601601

@@ -606,7 +606,7 @@ public function dataIsSuperTypeOf(): iterable
606606
], [
607607
new IntegerType(),
608608
new IntegerType(),
609-
], 2, [0, 1]),
609+
], [2], [0, 1]),
610610
new ConstantArrayType([
611611
new ConstantStringType('foo'),
612612
], [
@@ -623,7 +623,7 @@ public function dataIsSuperTypeOf(): iterable
623623
], [
624624
new IntegerType(),
625625
new IntegerType(),
626-
], 2, [0, 1]),
626+
], [2], [0, 1]),
627627
TrinaryLogic::createMaybe(),
628628
];
629629

@@ -632,7 +632,7 @@ public function dataIsSuperTypeOf(): iterable
632632
new ConstantStringType('foo'),
633633
], [
634634
new IntegerType(),
635-
], 1, [0]),
635+
], [1], [0]),
636636
new ConstantArrayType([
637637
new ConstantStringType('foo'),
638638
], [
@@ -651,7 +651,7 @@ public function dataIsSuperTypeOf(): iterable
651651
new ConstantStringType('foo'),
652652
], [
653653
new IntegerType(),
654-
], 1, [0]),
654+
], [1], [0]),
655655
TrinaryLogic::createMaybe(),
656656
];
657657
}

tests/PHPStan/Type/SimultaneousTypeTraverserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function dataChangeStringIntoNonEmptyString(): iterable
2222
new ConstantArrayType(
2323
[new ConstantIntegerType(0)],
2424
[new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()])],
25-
1,
25+
[1],
2626
),
2727
'array<non-empty-string>',
2828
];
@@ -31,7 +31,7 @@ public function dataChangeStringIntoNonEmptyString(): iterable
3131
new ConstantArrayType(
3232
[new ConstantIntegerType(0)],
3333
[new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()])],
34-
1,
34+
[1],
3535
),
3636
'array<int>',
3737
];
@@ -40,7 +40,7 @@ public function dataChangeStringIntoNonEmptyString(): iterable
4040
new ConstantArrayType(
4141
[new ConstantIntegerType(0)],
4242
[new IntegerType()],
43-
1,
43+
[1],
4444
),
4545
'array<string>',
4646
];

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ public function dataUnion(): iterable
17941794
new ConstantIntegerType(0),
17951795
], [
17961796
new StringType(),
1797-
], 1, [0]),
1797+
], [1], [0]),
17981798
],
17991799
UnionType::class,
18001800
'array{}|array{0?: string}',
@@ -3766,7 +3766,7 @@ public function dataIntersect(): iterable
37663766
], [
37673767
new IntegerType(),
37683768
new IntegerType(),
3769-
], 2, [0]),
3769+
], [2], [0]),
37703770
new HasOffsetType(new ConstantStringType('a')),
37713771
],
37723772
ConstantArrayType::class,
@@ -4900,7 +4900,7 @@ public function dataRemove(): array
49004900
], [
49014901
new StringType(),
49024902
new StringType(),
4903-
], 2),
4903+
], [2]),
49044904
new HasOffsetType(new ConstantIntegerType(1)),
49054905
NeverType::class,
49064906
'*NEVER*=implicit',
@@ -4912,7 +4912,7 @@ public function dataRemove(): array
49124912
], [
49134913
new StringType(),
49144914
new StringType(),
4915-
], 2, [1]),
4915+
], [2], [1]),
49164916
new HasOffsetType(new ConstantIntegerType(1)),
49174917
ConstantArrayType::class,
49184918
'array{string}',
@@ -4924,7 +4924,7 @@ public function dataRemove(): array
49244924
], [
49254925
new StringType(),
49264926
new StringType(),
4927-
], 2, [1]),
4927+
], [2], [1]),
49284928
new HasOffsetType(new ConstantIntegerType(0)),
49294929
NeverType::class,
49304930
'*NEVER*=implicit',

tests/PHPStan/Type/TypeGetFiniteTypesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,36 @@ public function dataGetFiniteTypes(): iterable
8989
], [
9090
new BooleanType(),
9191
new BooleanType(),
92-
], 2),
92+
], [2]),
9393
[
9494
new ConstantArrayType([
9595
new ConstantIntegerType(0),
9696
new ConstantIntegerType(1),
9797
], [
9898
new ConstantBooleanType(true),
9999
new ConstantBooleanType(true),
100-
], 2, [], TrinaryLogic::createYes()),
100+
], [2], [], TrinaryLogic::createYes()),
101101
new ConstantArrayType([
102102
new ConstantIntegerType(0),
103103
new ConstantIntegerType(1),
104104
], [
105105
new ConstantBooleanType(true),
106106
new ConstantBooleanType(false),
107-
], 2, [], TrinaryLogic::createYes()),
107+
], [2], [], TrinaryLogic::createYes()),
108108
new ConstantArrayType([
109109
new ConstantIntegerType(0),
110110
new ConstantIntegerType(1),
111111
], [
112112
new ConstantBooleanType(false),
113113
new ConstantBooleanType(true),
114-
], 2, [], TrinaryLogic::createYes()),
114+
], [2], [], TrinaryLogic::createYes()),
115115
new ConstantArrayType([
116116
new ConstantIntegerType(0),
117117
new ConstantIntegerType(1),
118118
], [
119119
new ConstantBooleanType(false),
120120
new ConstantBooleanType(false),
121-
], 2, [], TrinaryLogic::createYes()),
121+
], [2], [], TrinaryLogic::createYes()),
122122
],
123123
];
124124
}

0 commit comments

Comments
 (0)