Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant array union set #2734

Draft
wants to merge 2 commits into
base: 1.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -828,18 +828,18 @@ parameters:

-
message: "#^PHPDoc tag @var assumes the expression with type PHPStan\\\\Type\\\\Type is always PHPStan\\\\Type\\\\Constant\\\\ConstantIntegerType\\|PHPStan\\\\Type\\\\Constant\\\\ConstantStringType but it's error\\-prone and dangerous\\.$#"
count: 2
path: src/Type/Constant/ConstantArrayTypeBuilder.php
count: 1
path: src/Type/Constant/ConstantArrayTypeBuilderInnards.php

-
message: "#^PHPDoc tag @var with type float\\|int is not subtype of native type int\\.$#"
count: 2
path: src/Type/Constant/ConstantArrayTypeBuilder.php
path: src/Type/Constant/ConstantArrayTypeBuilderInnards.php

-
message: "#^PHPDoc tag @var with type float\\|int is not subtype of type int\\.$#"
count: 1
path: src/Type/Constant/ConstantArrayTypeBuilder.php
path: src/Type/Constant/ConstantArrayTypeBuilderInnards.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\BooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isBoolean\\(\\) instead\\.$#"
Expand Down
249 changes: 51 additions & 198 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@

use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Accessory\OversizedArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_filter;
use function array_map;
use function array_unique;
use function array_values;
use function count;
use function in_array;
use function is_float;
use function max;
use function min;
use function range;

/** @api */
Expand All @@ -28,9 +16,8 @@ class ConstantArrayTypeBuilder

public const ARRAY_COUNT_LIMIT = 256;

private bool $degradeToGeneralArray = false;

private bool $oversized = false;
/** @var list<ConstantArrayTypeBuilderInnards> */
private array $innards;

/**
* @param array<int, Type> $keyTypes
Expand All @@ -39,13 +26,14 @@ class ConstantArrayTypeBuilder
* @param array<int> $optionalKeys
*/
private function __construct(
private array $keyTypes,
private array $valueTypes,
private array $nextAutoIndexes,
private array $optionalKeys,
private TrinaryLogic $isList,
array $keyTypes,
array $valueTypes,
array $nextAutoIndexes,
array $optionalKeys,
TrinaryLogic $isList,
)
{
$this->innards = [new ConstantArrayTypeBuilderInnards($keyTypes, $valueTypes, $nextAutoIndexes, $optionalKeys, $isList)];
}

public static function createEmpty(): self
Expand All @@ -70,134 +58,40 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
return $builder;
}

private function isAtLeastOneInnardDegraded(): bool
{
foreach ($this->innards as $innard) {
if (!$innard->degradeToGeneralArray) {
continue;
}

return true;
}

return false;
}

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $optional = false): void
{
if ($offsetType !== null) {
$offsetType = $offsetType->toArrayKey();
}

if (!$this->degradeToGeneralArray) {
if (!$this->isAtLeastOneInnardDegraded()) {
if ($offsetType === null) {
$newAutoIndexes = $optional ? $this->nextAutoIndexes : [];
$hasOptional = false;
foreach ($this->keyTypes as $i => $keyType) {
if (!$keyType instanceof ConstantIntegerType) {
continue;
}

if (!in_array($keyType->getValue(), $this->nextAutoIndexes, true)) {
continue;
}

$this->valueTypes[$i] = TypeCombinator::union($this->valueTypes[$i], $valueType);

if (!$hasOptional && !$optional) {
$this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
}

/** @var int|float $newAutoIndex */
$newAutoIndex = $keyType->getValue() + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $keyType->getValue();
}

$newAutoIndexes[] = $newAutoIndex;
$hasOptional = true;
foreach ($this->innards as $innard) {
$innard->setValueTypeForNewOffset($valueType, $optional);
}

$max = max($this->nextAutoIndexes);

$this->keyTypes[] = new ConstantIntegerType($max);
$this->valueTypes[] = $valueType;

/** @var int|float $newAutoIndex */
$newAutoIndex = $max + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $max;
}

$newAutoIndexes[] = $newAutoIndex;
$this->nextAutoIndexes = array_values(array_unique($newAutoIndexes));

if ($optional || $hasOptional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
}

if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
$this->degradeToGeneralArray = true;
}

return;
}

if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
/** @var ConstantIntegerType|ConstantStringType $keyType */
foreach ($this->keyTypes as $i => $keyType) {
if ($keyType->getValue() !== $offsetType->getValue()) {
continue;
}

if ($optional) {
$valueType = TypeCombinator::union($valueType, $this->valueTypes[$i]);
}

$this->valueTypes[$i] = $valueType;

if (!$optional) {
$this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
if ($keyType instanceof ConstantIntegerType) {
$nextAutoIndexes = array_values(array_filter($this->nextAutoIndexes, static fn (int $index) => $index > $keyType->getValue()));
if (count($nextAutoIndexes) === 0) {
throw new ShouldNotHappenException();
}
$this->nextAutoIndexes = $nextAutoIndexes;
}
}
return;
foreach ($this->innards as $innard) {
$innard->setValueTypeForConstantOffset($offsetType, $valueType, $optional);
}

$this->keyTypes[] = $offsetType;
$this->valueTypes[] = $valueType;

if ($offsetType instanceof ConstantIntegerType) {
$min = min($this->nextAutoIndexes);
$max = max($this->nextAutoIndexes);
if ($offsetType->getValue() > $min) {
if ($offsetType->getValue() <= $max) {
$this->isList = $this->isList->and(TrinaryLogic::createMaybe());
} else {
$this->isList = TrinaryLogic::createNo();
}
}
if ($offsetType->getValue() >= $max) {
/** @var int|float $newAutoIndex */
$newAutoIndex = $offsetType->getValue() + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $max;
}
if (!$optional) {
$this->nextAutoIndexes = [$newAutoIndex];
} else {
$this->nextAutoIndexes[] = $newAutoIndex;
}
}
} else {
$this->isList = TrinaryLogic::createNo();
}

if ($optional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
}

if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
$this->degradeToGeneralArray = true;
}

return;
}

$this->isList = TrinaryLogic::createNo();

$scalarTypes = $offsetType->getConstantScalarTypes();
if (count($scalarTypes) === 0) {
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
Expand All @@ -222,96 +116,55 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
}
}
}
if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
$match = true;
$valueTypes = $this->valueTypes;
if (
count($scalarTypes) > 0
&& (count($scalarTypes) * count($this->innards)) < self::ARRAY_COUNT_LIMIT
) {
$newInnards = [];
foreach ($scalarTypes as $scalarType) {
$scalarOffsetType = $scalarType->toArrayKey();
if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
throw new ShouldNotHappenException();
}
$offsetMatch = false;

/** @var ConstantIntegerType|ConstantStringType $keyType */
foreach ($this->keyTypes as $i => $keyType) {
if ($keyType->getValue() !== $scalarOffsetType->getValue()) {
continue;
foreach ($this->innards as $innard) {
$scalarOffsetType = $scalarType->toArrayKey();
if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
throw new ShouldNotHappenException();
}

$valueTypes[$i] = TypeCombinator::union($valueTypes[$i], $valueType);
$offsetMatch = true;
}

if ($offsetMatch) {
continue;
$newInnard = $innard->duplicate();
$newInnard->setValueTypeForConstantOffset($scalarOffsetType, $valueType, $optional);
$newInnards[] = $newInnard;
}

$match = false;
}

if ($match) {
$this->valueTypes = $valueTypes;
return;
}
}
}
$this->innards = $newInnards;

if ($offsetType === null) {
$offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes));
} else {
$this->isList = TrinaryLogic::createNo();
return;
}
}

$this->keyTypes[] = $offsetType;
$this->valueTypes[] = $valueType;
if ($optional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
foreach ($this->innards as $innard) {
$innard->setValueTypeForGeneralOffset($offsetType, $valueType, $optional);
}
$this->degradeToGeneralArray = true;
}

public function degradeToGeneralArray(bool $oversized = false): void
{
$this->degradeToGeneralArray = true;
$this->oversized = $this->oversized || $oversized;
foreach ($this->innards as $innard) {
$innard->degradeToGeneralArray($oversized);
}
}

public function getArray(): Type
{
$keyTypesCount = count($this->keyTypes);
if ($keyTypesCount === 0) {
return new ConstantArrayType([], []);
}

if (!$this->degradeToGeneralArray) {
/** @var array<int, ConstantIntegerType|ConstantStringType> $keyTypes */
$keyTypes = $this->keyTypes;
return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
}

$array = new ArrayType(
TypeCombinator::union(...$this->keyTypes),
TypeCombinator::union(...$this->valueTypes),
);

if (count($this->optionalKeys) < $keyTypesCount) {
$array = TypeCombinator::intersect($array, new NonEmptyArrayType());
}

if ($this->oversized) {
$array = TypeCombinator::intersect($array, new OversizedArrayType());
}

if ($this->isList->yes()) {
$array = AccessoryArrayListType::intersectWith($array);
$arrays = [];
foreach ($this->innards as $innard) {
$arrays[] = $innard->getArray();
}

return $array;
return TypeCombinator::union(...$arrays);
}

public function isList(): bool
{
return $this->isList->yes();
return TrinaryLogic::lazyExtremeIdentity($this->innards, static fn (ConstantArrayTypeBuilderInnards $innard) => $innard->isList)->yes();
}

}
Loading