Skip to content

Commit 788bd0b

Browse files
herndlmondrejmirtes
authored andcommitted
Add Type::isConstantArray()
1 parent 330209f commit 788bd0b

18 files changed

+76
-17
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
use PHPStan\Type\ArrayType;
7676
use PHPStan\Type\BooleanType;
7777
use PHPStan\Type\ClosureType;
78-
use PHPStan\Type\Constant\ConstantArrayType;
7978
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
8079
use PHPStan\Type\Constant\ConstantBooleanType;
8180
use PHPStan\Type\Constant\ConstantFloatType;
@@ -4619,7 +4618,7 @@ private static function generalizeType(Type $a, Type $b): Type
46194618
$constantStrings[$key][] = $type;
46204619
continue;
46214620
}
4622-
if ($type instanceof ConstantArrayType) {
4621+
if ($type->isConstantArray()->yes()) {
46234622
$constantArrays[$key][] = $type;
46244623
continue;
46254624
}
@@ -4713,9 +4712,9 @@ private static function generalizeType(Type $a, Type $b): Type
47134712
$bArrays = $bValueType->getArrays();
47144713
if (
47154714
count($aArrays) === 1
4716-
&& !$aArrays[0] instanceof ConstantArrayType
4715+
&& $aArrays[0]->isConstantArray()->no()
47174716
&& count($bArrays) === 1
4718-
&& !$bArrays[0] instanceof ConstantArrayType
4717+
&& $bArrays[0]->isConstantArray()->no()
47194718
) {
47204719
$aDepth = self::getArrayDepth($aArrays[0]);
47214720
$bDepth = self::getArrayDepth($bArrays[0]);

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use PHPStan\ShouldNotHappenException;
3636
use PHPStan\TrinaryLogic;
3737
use PHPStan\Type\ArrayType;
38-
use PHPStan\Type\Constant\ConstantArrayType;
3938
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
4039
use PHPStan\Type\Constant\ConstantStringType;
4140
use PHPStan\Type\Enum\EnumCaseObjectType;
@@ -946,7 +945,7 @@ private function inferAndCachePropertyTypes(
946945
}
947946

948947
$propertyType = $propertyType->generalize(GeneralizePrecision::lessSpecific());
949-
if ($propertyType instanceof ConstantArrayType) {
948+
if ($propertyType->isConstantArray()->yes()) {
950949
$propertyType = new ArrayType(new MixedType(true), new MixedType(true));
951950
}
952951

src/Rules/RuleLevelHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTyp
111111
$acceptedType->isArray()->yes()
112112
&& $acceptingType->isArray()->yes()
113113
&& !$acceptingType->isIterableAtLeastOnce()->yes()
114-
&& count($acceptedType->getConstantArrays()) === 0
115-
&& count($acceptingType->getConstantArrays()) === 0
114+
&& $acceptedType->isConstantArray()->no()
115+
&& $acceptingType->isConstantArray()->no()
116116
) {
117117
return self::accepts(
118118
$acceptingType->getIterableKeyType(),

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ public function isArray(): TrinaryLogic
182182
return TrinaryLogic::createYes();
183183
}
184184

185+
public function isConstantArray(): TrinaryLogic
186+
{
187+
return TrinaryLogic::createMaybe();
188+
}
189+
185190
public function isOversizedArray(): TrinaryLogic
186191
{
187192
return TrinaryLogic::createMaybe();

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ public function isArray(): TrinaryLogic
171171
return TrinaryLogic::createYes();
172172
}
173173

174+
public function isConstantArray(): TrinaryLogic
175+
{
176+
return TrinaryLogic::createMaybe();
177+
}
178+
174179
public function isOversizedArray(): TrinaryLogic
175180
{
176181
return TrinaryLogic::createMaybe();

src/Type/Accessory/OversizedArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public function isArray(): TrinaryLogic
170170
return TrinaryLogic::createYes();
171171
}
172172

173+
public function isConstantArray(): TrinaryLogic
174+
{
175+
return TrinaryLogic::createMaybe();
176+
}
177+
173178
public function isOversizedArray(): TrinaryLogic
174179
{
175180
return TrinaryLogic::createYes();

src/Type/ArrayType.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
126126
public function equals(Type $type): bool
127127
{
128128
return $type instanceof self
129-
&& !$type instanceof ConstantArrayType
130-
&& $this->getItemType()->equals($type->getItemType())
129+
&& $type->isConstantArray()->no()
130+
&& $this->getItemType()->equals($type->getIterableValueType())
131131
&& $this->keyType->equals($type->keyType);
132132
}
133133

@@ -238,6 +238,11 @@ public function isArray(): TrinaryLogic
238238
return TrinaryLogic::createYes();
239239
}
240240

241+
public function isConstantArray(): TrinaryLogic
242+
{
243+
return TrinaryLogic::createNo();
244+
}
245+
241246
public function isOversizedArray(): TrinaryLogic
242247
{
243248
return TrinaryLogic::createMaybe();
@@ -509,19 +514,19 @@ public function traverse(callable $cb): Type
509514

510515
public function tryRemove(Type $typeToRemove): ?Type
511516
{
512-
if ($typeToRemove instanceof ConstantArrayType && $typeToRemove->isIterableAtLeastOnce()->no()) {
517+
if ($typeToRemove->isConstantArray()->yes() && $typeToRemove->isIterableAtLeastOnce()->no()) {
513518
return TypeCombinator::intersect($this, new NonEmptyArrayType());
514519
}
515520

516521
if ($typeToRemove instanceof NonEmptyArrayType) {
517522
return new ConstantArrayType([], []);
518523
}
519524

520-
if ($this instanceof ConstantArrayType && $typeToRemove instanceof HasOffsetType) {
525+
if ($this->isConstantArray()->yes() && $typeToRemove instanceof HasOffsetType) {
521526
return $this->unsetOffset($typeToRemove->getOffsetType());
522527
}
523528

524-
if ($this instanceof ConstantArrayType && $typeToRemove instanceof HasOffsetValueType) {
529+
if ($this->isConstantArray()->yes() && $typeToRemove instanceof HasOffsetValueType) {
525530
return $this->unsetOffset($typeToRemove->getOffsetType());
526531
}
527532

src/Type/Constant/ConstantArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,11 @@ public function getLastIterableValueType(): Type
736736
return TypeCombinator::union(...$valueTypes);
737737
}
738738

739+
public function isConstantArray(): TrinaryLogic
740+
{
741+
return TrinaryLogic::createYes();
742+
}
743+
739744
public function isList(): TrinaryLogic
740745
{
741746
if ($this->isList) {

src/Type/IntersectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ public function isArray(): TrinaryLogic
441441
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isArray());
442442
}
443443

444+
public function isConstantArray(): TrinaryLogic
445+
{
446+
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantArray());
447+
}
448+
444449
public function isOversizedArray(): TrinaryLogic
445450
{
446451
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOversizedArray());

src/Type/MixedType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ public function isArray(): TrinaryLogic
470470
return TrinaryLogic::createMaybe();
471471
}
472472

473+
public function isConstantArray(): TrinaryLogic
474+
{
475+
return TrinaryLogic::createMaybe();
476+
}
477+
473478
public function isOversizedArray(): TrinaryLogic
474479
{
475480
if ($this->subtractedType !== null) {

0 commit comments

Comments
 (0)