Skip to content

Commit 74e8549

Browse files
committed
[BCB] ConstantArrayType no longer extends ArrayType
1 parent 66fdf25 commit 74e8549

11 files changed

+354
-238
lines changed

UPGRADING.md

+6
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ If you want to change `$overwrite` or `$rootExpr` (previous parameters also used
203203

204204
If you want to change `$overwrite` or `$rootExpr` (previous parameters also used to be accepted by the constructor), call `setAlwaysOverwriteTypes()` and `setRootExpr()`. These methods return a new object (SpecifiedTypes is immutable).
205205

206+
### `ConstantArrayType` no longer extends `ArrayType`
207+
208+
`Type::getArrays()` now returns `list<ArrayType|ConstantArrayType>`.
209+
210+
Using `$type instanceof ArrayType` is [being deprecated anyway](https://phpstan.org/blog/why-is-instanceof-type-wrong-and-getting-deprecated) so the impact of this change should be minimal.
211+
206212
### Changed `TypeSpecifier::specifyTypesInCondition()`
207213

208214
This method now longer accepts `Expr $rootExpr`. If you want to change it, call `setRootExpr()` on [`SpecifiedTypes`](https://apiref.phpstan.org/2.0.x/PHPStan.Analyser.SpecifiedTypes.html) (object returned by `TypeSpecifier::specifyTypesInCondition()`). `setRootExpr()` method returns a new object (SpecifiedTypes is immutable).

phpstan-baseline.neon

+17-2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ parameters:
175175
count: 1
176176
path: src/PhpDoc/TypeNodeResolver.php
177177

178+
-
179+
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
180+
count: 1
181+
path: src/PhpDoc/TypeNodeResolver.php
182+
178183
-
179184
message: "#^Doing instanceof PHPStan\\\\Type\\\\IterableType is error\\-prone and deprecated\\. Use Type\\:\\:isIterable\\(\\) instead\\.$#"
180185
count: 1
@@ -505,6 +510,11 @@ parameters:
505510
count: 1
506511
path: src/Rules/Methods/MethodParameterComparisonHelper.php
507512

513+
-
514+
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
515+
count: 1
516+
path: src/Rules/Methods/MethodParameterComparisonHelper.php
517+
508518
-
509519
message: "#^Doing instanceof PHPStan\\\\Type\\\\IterableType is error\\-prone and deprecated\\. Use Type\\:\\:isIterable\\(\\) instead\\.$#"
510520
count: 1
@@ -657,7 +667,7 @@ parameters:
657667

658668
-
659669
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
660-
count: 1
670+
count: 2
661671
path: src/Type/ArrayType.php
662672

663673
-
@@ -1392,7 +1402,7 @@ parameters:
13921402

13931403
-
13941404
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
1395-
count: 10
1405+
count: 14
13961406
path: src/Type/TypeCombinator.php
13971407

13981408
-
@@ -1465,6 +1475,11 @@ parameters:
14651475
count: 3
14661476
path: src/Type/TypehintHelper.php
14671477

1478+
-
1479+
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
1480+
count: 3
1481+
path: src/Type/TypehintHelper.php
1482+
14681483
-
14691484
message: "#^Doing instanceof PHPStan\\\\Type\\\\IterableType is error\\-prone and deprecated\\. Use Type\\:\\:isIterable\\(\\) instead\\.$#"
14701485
count: 1

src/Analyser/NodeScopeResolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3868,7 +3868,7 @@ private function getArraySortPreserveListFunctionType(Type $type): Type
38683868
return $traverse($type);
38693869
}
38703870

3871-
if (!$type instanceof ArrayType) {
3871+
if (!$type instanceof ArrayType && !$type instanceof ConstantArrayType) {
38723872
return $type;
38733873
}
38743874

src/PhpDoc/TypeNodeResolver.php

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use PHPStan\Type\ClosureType;
6060
use PHPStan\Type\ConditionalType;
6161
use PHPStan\Type\ConditionalTypeForParameter;
62+
use PHPStan\Type\Constant\ConstantArrayType;
6263
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
6364
use PHPStan\Type\Constant\ConstantBooleanType;
6465
use PHPStan\Type\Constant\ConstantFloatType;
@@ -571,6 +572,8 @@ private function resolveUnionTypeNode(UnionTypeNode $typeNode, NameScope $nameSc
571572
$type = new IntersectionType([$type, new IterableType(new MixedType(), $arrayTypeType)]);
572573
} elseif ($type instanceof ArrayType) {
573574
$type = new ArrayType(new MixedType(), $arrayTypeType);
575+
} elseif ($type instanceof ConstantArrayType) {
576+
$type = new ArrayType(new MixedType(), $arrayTypeType);
574577
} elseif ($type instanceof IterableType) {
575578
$type = new IterableType(new MixedType(), $arrayTypeType);
576579
} else {

src/Rules/Methods/MethodParameterComparisonHelper.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Rules\IdentifierRuleError;
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Type\ArrayType;
12+
use PHPStan\Type\Constant\ConstantArrayType;
1213
use PHPStan\Type\IterableType;
1314
use PHPStan\Type\MixedType;
1415
use PHPStan\Type\Type;
@@ -392,6 +393,9 @@ private function isTypeCompatible(Type $methodParameterType, Type $prototypePara
392393
if ($prototypeParameterType instanceof ArrayType) {
393394
return true;
394395
}
396+
if ($prototypeParameterType instanceof ConstantArrayType) {
397+
return true;
398+
}
395399
if ($prototypeParameterType->isObject()->yes() && $prototypeParameterType->getObjectClassNames() === [Traversable::class]) {
396400
return true;
397401
}

0 commit comments

Comments
 (0)