Skip to content

Commit 774f54e

Browse files
herndlmondrejmirtes
authored andcommitted
Deprecate TypeUtils::getArrays() and TypeUtils::getAnyArrays()
1 parent e05a659 commit 774f54e

19 files changed

+101
-14
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,8 +4701,8 @@ private static function generalizeType(Type $a, Type $b): Type
47014701

47024702
$aValueType = $generalArraysA->getIterableValueType();
47034703
$bValueType = $generalArraysB->getIterableValueType();
4704-
$aArrays = TypeUtils::getAnyArrays($aValueType);
4705-
$bArrays = TypeUtils::getAnyArrays($bValueType);
4704+
$aArrays = $aValueType->getArrays();
4705+
$bArrays = $bValueType->getArrays();
47064706
if (
47074707
count($aArrays) === 1
47084708
&& !$aArrays[0] instanceof ConstantArrayType
@@ -4883,7 +4883,7 @@ private static function getArrayDepth(ArrayType $type): int
48834883
$depth = 0;
48844884
while ($type instanceof ArrayType) {
48854885
$temp = $type->getIterableValueType();
4886-
$arrays = TypeUtils::getAnyArrays($temp);
4886+
$arrays = $temp->getArrays();
48874887
if (count($arrays) === 1) {
48884888
$type = $arrays[0];
48894889
} else {

src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Rules\Rule;
88
use PHPStan\Rules\RuleErrorBuilder;
99
use PHPStan\Type\MixedType;
10-
use PHPStan\Type\TypeUtils;
1110
use PHPStan\Type\VerbosityLevel;
1211
use function count;
1312
use function sprintf;
@@ -34,7 +33,7 @@ public function processNode(Node $node, Scope $scope): array
3433
}
3534

3635
$varType = $scope->getType($node->var);
37-
if (count(TypeUtils::getAnyArrays($varType)) === 0) {
36+
if (count($varType->getArrays()) === 0) {
3837
return [];
3938
}
4039

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,30 @@ public function findSpecifiedType(
108108
}
109109

110110
if (!$haystackType instanceof ConstantArrayType || count($haystackType->getValueTypes()) > 0) {
111-
$haystackArrayTypes = TypeUtils::getArrays($haystackType);
111+
$haystackArrayTypes = $haystackType->getArrays();
112112
if (count($haystackArrayTypes) === 1 && $haystackArrayTypes[0]->getIterableValueType() instanceof NeverType) {
113113
return null;
114114
}
115115

116116
if ($isNeedleSupertype->maybe() || $isNeedleSupertype->yes()) {
117117
foreach ($haystackArrayTypes as $haystackArrayType) {
118-
foreach (TypeUtils::getConstantScalars($haystackArrayType->getIterableValueType()) as $constantScalarType) {
119-
if ($constantScalarType->isSuperTypeOf($needleType)->yes()) {
120-
continue 2;
118+
if ($haystackArrayType instanceof ConstantArrayType) {
119+
foreach ($haystackArrayType->getValueTypes() as $i => $haystackArrayValueType) {
120+
if ($haystackArrayType->isOptionalKey($i)) {
121+
continue;
122+
}
123+
124+
foreach (TypeUtils::getConstantScalars($haystackArrayValueType) as $constantScalarType) {
125+
if ($constantScalarType->isSuperTypeOf($needleType)->yes()) {
126+
continue 3;
127+
}
128+
}
129+
}
130+
} else {
131+
foreach (TypeUtils::getConstantScalars($haystackArrayType->getIterableValueType()) as $constantScalarType) {
132+
if ($constantScalarType->isSuperTypeOf($needleType)->yes()) {
133+
continue 2;
134+
}
121135
}
122136
}
123137

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function getReferencedClasses(): array
4444
return [];
4545
}
4646

47+
public function getArrays(): array
48+
{
49+
return [];
50+
}
51+
4752
public function getConstantArrays(): array
4853
{
4954
return [];

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function getReferencedClasses(): array
4141
return [];
4242
}
4343

44+
public function getArrays(): array
45+
{
46+
return [];
47+
}
48+
4449
public function getConstantArrays(): array
4550
{
4651
return [];

src/Type/Accessory/OversizedArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function getReferencedClasses(): array
4040
return [];
4141
}
4242

43+
public function getArrays(): array
44+
{
45+
return [];
46+
}
47+
4348
public function getConstantArrays(): array
4449
{
4550
return [];

src/Type/ArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function getReferencedClasses(): array
7373
);
7474
}
7575

76+
public function getArrays(): array
77+
{
78+
return [$this];
79+
}
80+
7681
public function getConstantArrays(): array
7782
{
7883
return [];

src/Type/IntersectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public function getReferencedClasses(): array
100100
return UnionTypeHelper::getReferencedClasses($this->types);
101101
}
102102

103+
public function getArrays(): array
104+
{
105+
return UnionTypeHelper::getArrays($this->getTypes());
106+
}
107+
103108
public function getConstantArrays(): array
104109
{
105110
return UnionTypeHelper::getConstantArrays($this->getTypes());

src/Type/MixedType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public function getReferencedClasses(): array
6363
return [];
6464
}
6565

66+
public function getArrays(): array
67+
{
68+
return [];
69+
}
70+
6671
public function getConstantArrays(): array
6772
{
6873
return [];

src/Type/Php/ArrayFilterFunctionReturnTypeReturnTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use PHPStan\Type\StaticTypeFactory;
3030
use PHPStan\Type\Type;
3131
use PHPStan\Type\TypeCombinator;
32-
use PHPStan\Type\TypeUtils;
3332
use function array_map;
3433
use function count;
3534
use function is_string;
@@ -70,7 +69,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7069

7170
if ($callbackArg === null || ($callbackArg instanceof ConstFetch && strtolower($callbackArg->name->parts[0]) === 'null')) {
7271
return TypeCombinator::union(
73-
...array_map([$this, 'removeFalsey'], TypeUtils::getArrays($arrayArgType)),
72+
...array_map([$this, 'removeFalsey'], $arrayArgType->getArrays()),
7473
);
7574
}
7675

@@ -139,7 +138,7 @@ public function removeFalsey(Type $type): Type
139138
if ($isFalsey->maybe()) {
140139
$builder->setOffsetValueType($keys[$offset], TypeCombinator::remove($value, $falseyTypes), true);
141140
} elseif ($isFalsey->no()) {
142-
$builder->setOffsetValueType($keys[$offset], $value);
141+
$builder->setOffsetValueType($keys[$offset], $value, $type->isOptionalKey($offset));
143142
}
144143
}
145144

0 commit comments

Comments
 (0)