Skip to content

Commit

Permalink
Pass TrinaryLogic into ConstantArrayType isList everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 18, 2023
1 parent a8818be commit f0a9fd7
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -734,7 +734,7 @@ public function unsetOffset(Type $offsetType): Type
continue;
}
$optionalKeys[] = $i;
$isList = false;
$isList = TrinaryLogic::createNo();
}
$optionalKeys = array_values(array_unique($optionalKeys));

Expand Down
19 changes: 10 additions & 9 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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 */
Expand All @@ -177,7 +178,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
}
}
} else {
$this->isList = false;
$this->isList = TrinaryLogic::createNo();
}

if ($optional) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -306,7 +307,7 @@ public function getArray(): Type

public function isList(): bool
{
return $this->isList;
return $this->isList->yes();
}

}
2 changes: 1 addition & 1 deletion src/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Generic/TemplateConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Type/Php/HrtimeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Php/PregSplitDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
4 changes: 3 additions & 1 deletion src/Type/Php/StrSplitFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}

}
2 changes: 1 addition & 1 deletion src/Type/ResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function toArray(): Type
[$this],
[1],
[],
true,
TrinaryLogic::createYes(),
);
}

Expand Down

0 comments on commit f0a9fd7

Please sign in to comment.