From f0a9fd796e1b1d023affdb7fb4ffd344d79becd4 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 18 Jul 2023 16:40:20 +0200 Subject: [PATCH] Pass TrinaryLogic into ConstantArrayType `isList` everywhere --- .../Accessory/AccessoryLiteralStringType.php | 2 +- .../Accessory/AccessoryNonEmptyStringType.php | 2 +- .../Accessory/AccessoryNonFalsyStringType.php | 2 +- .../Accessory/AccessoryNumericStringType.php | 2 +- src/Type/BooleanType.php | 2 +- src/Type/ClosureType.php | 2 +- src/Type/Constant/ConstantArrayType.php | 6 +++--- .../Constant/ConstantArrayTypeBuilder.php | 19 ++++++++++--------- src/Type/FloatType.php | 2 +- .../Generic/TemplateConstantArrayType.php | 2 +- src/Type/IntegerType.php | 2 +- .../Php/HrtimeFunctionReturnTypeExtension.php | 3 ++- .../PregSplitDynamicReturnTypeExtension.php | 3 ++- .../StrSplitFunctionReturnTypeExtension.php | 4 +++- src/Type/ResourceType.php | 2 +- src/Type/StringType.php | 2 +- 16 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Type/Accessory/AccessoryLiteralStringType.php b/src/Type/Accessory/AccessoryLiteralStringType.php index 4552e392de..317315c215 100644 --- a/src/Type/Accessory/AccessoryLiteralStringType.php +++ b/src/Type/Accessory/AccessoryLiteralStringType.php @@ -188,7 +188,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/Accessory/AccessoryNonEmptyStringType.php b/src/Type/Accessory/AccessoryNonEmptyStringType.php index 67281ea606..31ea0c89b0 100644 --- a/src/Type/Accessory/AccessoryNonEmptyStringType.php +++ b/src/Type/Accessory/AccessoryNonEmptyStringType.php @@ -189,7 +189,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/Accessory/AccessoryNonFalsyStringType.php b/src/Type/Accessory/AccessoryNonFalsyStringType.php index dc2ab057fe..1fb1e16378 100644 --- a/src/Type/Accessory/AccessoryNonFalsyStringType.php +++ b/src/Type/Accessory/AccessoryNonFalsyStringType.php @@ -189,7 +189,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/Accessory/AccessoryNumericStringType.php b/src/Type/Accessory/AccessoryNumericStringType.php index dc7675e40b..a852131883 100644 --- a/src/Type/Accessory/AccessoryNumericStringType.php +++ b/src/Type/Accessory/AccessoryNumericStringType.php @@ -191,7 +191,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/BooleanType.php b/src/Type/BooleanType.php index 0534b657cc..c0ac5b5920 100644 --- a/src/Type/BooleanType.php +++ b/src/Type/BooleanType.php @@ -87,7 +87,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/ClosureType.php b/src/Type/ClosureType.php index d0ce4e79d0..e76b5d9de8 100644 --- a/src/Type/ClosureType.php +++ b/src/Type/ClosureType.php @@ -340,7 +340,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 228c1386e5..7ff988a856 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -695,7 +695,7 @@ public function unsetOffset(Type $offsetType): Type $k++; } - return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, false); + return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, TrinaryLogic::createNo()); } return $this; @@ -724,7 +724,7 @@ public function unsetOffset(Type $offsetType): Type } } - return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, $optionalKeys, false); + return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, $optionalKeys, TrinaryLogic::createNo()); } $optionalKeys = $this->optionalKeys; @@ -734,7 +734,7 @@ public function unsetOffset(Type $offsetType): Type continue; } $optionalKeys[] = $i; - $isList = false; + $isList = TrinaryLogic::createNo(); } $optionalKeys = array_values(array_unique($optionalKeys)); diff --git a/src/Type/Constant/ConstantArrayTypeBuilder.php b/src/Type/Constant/ConstantArrayTypeBuilder.php index ecd9461f87..46b4064377 100644 --- a/src/Type/Constant/ConstantArrayTypeBuilder.php +++ b/src/Type/Constant/ConstantArrayTypeBuilder.php @@ -3,6 +3,7 @@ namespace PHPStan\Type\Constant; use PHPStan\ShouldNotHappenException; +use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryArrayListType; use PHPStan\Type\Accessory\NonEmptyArrayType; use PHPStan\Type\Accessory\OversizedArrayType; @@ -42,14 +43,14 @@ private function __construct( private array $valueTypes, private array $nextAutoIndexes, private array $optionalKeys, - private bool $isList, + private TrinaryLogic $isList, ) { } public static function createEmpty(): self { - return new self([], [], [0], [], true); + return new self([], [], [0], [], TrinaryLogic::createYes()); } public static function createFromConstantArray(ConstantArrayType $startArrayType): self @@ -59,7 +60,7 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType $startArrayType->getValueTypes(), $startArrayType->getNextAutoIndexes(), $startArrayType->getOptionalKeys(), - $startArrayType->isList()->yes(), + $startArrayType->isList(), ); if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) { @@ -162,7 +163,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt $min = min($this->nextAutoIndexes); $max = max($this->nextAutoIndexes); if ($offsetType->getValue() > $min) { - $this->isList = false; + $this->isList = TrinaryLogic::createNo(); } if ($offsetType->getValue() >= $max) { /** @var int|float $newAutoIndex */ @@ -177,7 +178,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt } } } else { - $this->isList = false; + $this->isList = TrinaryLogic::createNo(); } if ($optional) { @@ -191,7 +192,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt return; } - $this->isList = false; + $this->isList = TrinaryLogic::createNo(); $scalarTypes = $offsetType->getConstantScalarTypes(); if (count($scalarTypes) === 0) { @@ -254,7 +255,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt if ($offsetType === null) { $offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes)); } else { - $this->isList = false; + $this->isList = TrinaryLogic::createNo(); } $this->keyTypes[] = $offsetType; @@ -297,7 +298,7 @@ public function getArray(): Type $array = TypeCombinator::intersect($array, new OversizedArrayType()); } - if ($this->isList) { + if ($this->isList->yes()) { $array = AccessoryArrayListType::intersectWith($array); } @@ -306,7 +307,7 @@ public function getArray(): Type public function isList(): bool { - return $this->isList; + return $this->isList->yes(); } } diff --git a/src/Type/FloatType.php b/src/Type/FloatType.php index 7bd02b1f92..c82a50e4e7 100644 --- a/src/Type/FloatType.php +++ b/src/Type/FloatType.php @@ -135,7 +135,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/Generic/TemplateConstantArrayType.php b/src/Type/Generic/TemplateConstantArrayType.php index c1372cf9e4..8bb8aa696d 100644 --- a/src/Type/Generic/TemplateConstantArrayType.php +++ b/src/Type/Generic/TemplateConstantArrayType.php @@ -21,7 +21,7 @@ public function __construct( ConstantArrayType $bound, ) { - parent::__construct($bound->getKeyTypes(), $bound->getValueTypes(), $bound->getNextAutoIndexes(), $bound->getOptionalKeys(), $bound->isList()->yes()); + parent::__construct($bound->getKeyTypes(), $bound->getValueTypes(), $bound->getNextAutoIndexes(), $bound->getOptionalKeys(), $bound->isList()); $this->scope = $scope; $this->strategy = $templateTypeStrategy; $this->variance = $templateTypeVariance; diff --git a/src/Type/IntegerType.php b/src/Type/IntegerType.php index 56a8d74b7e..5e81e00519 100644 --- a/src/Type/IntegerType.php +++ b/src/Type/IntegerType.php @@ -87,7 +87,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/Php/HrtimeFunctionReturnTypeExtension.php b/src/Type/Php/HrtimeFunctionReturnTypeExtension.php index 2192fc8745..89f7f97f33 100644 --- a/src/Type/Php/HrtimeFunctionReturnTypeExtension.php +++ b/src/Type/Php/HrtimeFunctionReturnTypeExtension.php @@ -5,6 +5,7 @@ use PhpParser\Node\Expr\FuncCall; use PHPStan\Analyser\Scope; use PHPStan\Reflection\FunctionReflection; +use PHPStan\TrinaryLogic; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\Constant\ConstantIntegerType; @@ -26,7 +27,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type { - $arrayType = new ConstantArrayType([new ConstantIntegerType(0), new ConstantIntegerType(1)], [new IntegerType(), new IntegerType()], [2]); + $arrayType = new ConstantArrayType([new ConstantIntegerType(0), new ConstantIntegerType(1)], [new IntegerType(), new IntegerType()], [2], [], TrinaryLogic::createYes()); $numberType = TypeUtils::toBenevolentUnion(TypeCombinator::union(new IntegerType(), new FloatType())); if (count($functionCall->getArgs()) < 1) { diff --git a/src/Type/Php/PregSplitDynamicReturnTypeExtension.php b/src/Type/Php/PregSplitDynamicReturnTypeExtension.php index ad3e1c3432..cf2b07a2b7 100644 --- a/src/Type/Php/PregSplitDynamicReturnTypeExtension.php +++ b/src/Type/Php/PregSplitDynamicReturnTypeExtension.php @@ -6,6 +6,7 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParametersAcceptorSelector; +use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryArrayListType; use PHPStan\Type\ArrayType; use PHPStan\Type\BitwiseFlagHelper; @@ -41,7 +42,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, if ($flagsArg !== null && $this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($flagsArg->value, $scope, 'PREG_SPLIT_OFFSET_CAPTURE')->yes()) { $type = new ArrayType( new IntegerType(), - new ConstantArrayType([new ConstantIntegerType(0), new ConstantIntegerType(1)], [new StringType(), IntegerRangeType::fromInterval(0, null)], [2]), + new ConstantArrayType([new ConstantIntegerType(0), new ConstantIntegerType(1)], [new StringType(), IntegerRangeType::fromInterval(0, null)], [2], [], TrinaryLogic::createYes()), ); return TypeCombinator::union(AccessoryArrayListType::intersectWith($type), new ConstantBooleanType(false)); } diff --git a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php index 2cc1b3a98b..c16fa7a9a8 100644 --- a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php +++ b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php @@ -8,6 +8,7 @@ use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\ShouldNotHappenException; +use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryArrayListType; use PHPStan\Type\Accessory\NonEmptyArrayType; use PHPStan\Type\ArrayType; @@ -20,6 +21,7 @@ use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; +use function array_is_list; use function array_map; use function array_unique; use function count; @@ -134,7 +136,7 @@ private static function createConstantArrayFrom(array $constantArray, Scope $sco $i++; } - return new ConstantArrayType($keyTypes, $valueTypes, $isList ? [$i] : [0]); + return new ConstantArrayType($keyTypes, $valueTypes, $isList ? [$i] : [0], [], TrinaryLogic::createFromBoolean(array_is_list($constantArray))); } } diff --git a/src/Type/ResourceType.php b/src/Type/ResourceType.php index 4d28cdb2a3..0eee8cea5f 100644 --- a/src/Type/ResourceType.php +++ b/src/Type/ResourceType.php @@ -77,7 +77,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); } diff --git a/src/Type/StringType.php b/src/Type/StringType.php index ab2d44ecf4..9587e48335 100644 --- a/src/Type/StringType.php +++ b/src/Type/StringType.php @@ -155,7 +155,7 @@ public function toArray(): Type [$this], [1], [], - true, + TrinaryLogic::createYes(), ); }