Skip to content

Commit 93a5c26

Browse files
herndlmondrejmirtes
authored andcommitted
Deprecate ConstantArrayType::isEmpty()
1 parent 1c5c636 commit 93a5c26

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
use PHPStan\Type\ArrayType;
3535
use PHPStan\Type\BooleanType;
3636
use PHPStan\Type\ConditionalTypeForParameter;
37-
use PHPStan\Type\Constant\ConstantArrayType;
3837
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
3938
use PHPStan\Type\Constant\ConstantBooleanType;
4039
use PHPStan\Type\Constant\ConstantIntegerType;
@@ -378,15 +377,15 @@ public function specifyTypesInCondition(
378377
if (
379378
!$context->null()
380379
&& $rightType->isArray()->yes()
381-
&& $leftType instanceof ConstantArrayType && $leftType->isEmpty()
380+
&& $leftType->isConstantArray()->yes() && $leftType->isIterableAtLeastOnce()->no()
382381
) {
383382
return $this->create($expr->right, new NonEmptyArrayType(), $context->negate(), false, $scope, $rootExpr);
384383
}
385384

386385
if (
387386
!$context->null()
388387
&& $leftType->isArray()->yes()
389-
&& $rightType instanceof ConstantArrayType && $rightType->isEmpty()
388+
&& $rightType->isConstantArray()->yes() && $rightType->isIterableAtLeastOnce()->no()
390389
) {
391390
return $this->create($expr->left, new NonEmptyArrayType(), $context->negate(), false, $scope, $rootExpr);
392391
}

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,11 +1300,11 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
13001300
return $this->resolveIdenticalType($leftType, $rightType);
13011301
}
13021302

1303-
if ($leftType instanceof ConstantArrayType && $leftType->isEmpty() && $rightType instanceof ConstantScalarType) {
1303+
if ($leftType->isConstantArray()->yes() && $leftType->isIterableAtLeastOnce()->no() && $rightType instanceof ConstantScalarType) {
13041304
// @phpstan-ignore-next-line
13051305
return new ConstantBooleanType($rightType->getValue() == []); // phpcs:ignore
13061306
}
1307-
if ($rightType instanceof ConstantArrayType && $rightType->isEmpty() && $leftType instanceof ConstantScalarType) {
1307+
if ($rightType->isConstantArray()->yes() && $rightType->isIterableAtLeastOnce()->no() && $leftType instanceof ConstantScalarType) {
13081308
// @phpstan-ignore-next-line
13091309
return new ConstantBooleanType($leftType->getValue() == []); // phpcs:ignore
13101310
}

src/Type/Constant/ConstantArrayType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function getConstantArrays(): array
113113
return [$this];
114114
}
115115

116+
/** @deprecated Use isIterableAtLeastOnce()->no() instead */
116117
public function isEmpty(): bool
117118
{
118119
return count($this->keyTypes) === 0;
@@ -1048,13 +1049,14 @@ public function generalizeValues(): ArrayType
10481049

10491050
public function generalizeToArray(): Type
10501051
{
1051-
if ($this->isEmpty()) {
1052+
$isIterableAtLeastOnce = $this->isIterableAtLeastOnce();
1053+
if ($isIterableAtLeastOnce->no()) {
10521054
return $this;
10531055
}
10541056

10551057
$arrayType = new ArrayType($this->getKeyType(), $this->getItemType());
10561058

1057-
if ($this->isIterableAtLeastOnce()->yes()) {
1059+
if ($isIterableAtLeastOnce->yes()) {
10581060
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
10591061
}
10601062
if ($this->isList) {

src/Type/IterableType.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Type;
44

55
use PHPStan\TrinaryLogic;
6-
use PHPStan\Type\Constant\ConstantArrayType;
76
use PHPStan\Type\Generic\GenericObjectType;
87
use PHPStan\Type\Generic\TemplateMixedType;
98
use PHPStan\Type\Generic\TemplateType;
@@ -63,7 +62,7 @@ public function getReferencedClasses(): array
6362

6463
public function accepts(Type $type, bool $strictTypes): TrinaryLogic
6564
{
66-
if ($type instanceof ConstantArrayType && $type->isEmpty()) {
65+
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
6766
return TrinaryLogic::createYes();
6867
}
6968
if ($type->isIterable()->yes()) {
@@ -135,7 +134,7 @@ public function isSubTypeOf(Type $otherType): TrinaryLogic
135134
$limit = TrinaryLogic::createMaybe();
136135
}
137136

138-
if ($otherType instanceof ConstantArrayType && $otherType->isEmpty()) {
137+
if ($otherType->isConstantArray()->yes() && $otherType->isIterableAtLeastOnce()->no()) {
139138
return TrinaryLogic::createMaybe();
140139
}
141140

src/Type/TypeCombinator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ private static function reduceArrays(array $constantArrays): array
674674
continue;
675675
}
676676

677-
if ($constantArray->isEmpty()) {
677+
if ($constantArray->isIterableAtLeastOnce()->no()) {
678678
$emptyArray = $constantArray;
679679
continue;
680680
}

src/Type/UnionTypeHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ public static function sortTypes(array $types): array
140140
return self::compareStrings($a->getValue(), $b->getValue());
141141
}
142142

143-
if ($a instanceof ConstantArrayType && $b instanceof ConstantArrayType) {
144-
if ($a->isEmpty()) {
145-
if ($b->isEmpty()) {
143+
if ($a->isConstantArray()->yes() && $b->isConstantArray()->yes()) {
144+
if ($a->isIterableAtLeastOnce()->no()) {
145+
if ($b->isIterableAtLeastOnce()->no()) {
146146
return 0;
147147
}
148148

149149
return -1;
150-
} elseif ($b->isEmpty()) {
150+
} elseif ($b->isIterableAtLeastOnce()->no()) {
151151
return 1;
152152
}
153153

0 commit comments

Comments
 (0)