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

fix for big int-ranges #934

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Expand Up @@ -5,6 +5,7 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
Expand All @@ -13,7 +14,6 @@
use function count;
use function is_float;
use function max;
use function range;

/** @api */
class ConstantArrayTypeBuilder
Expand Down Expand Up @@ -101,34 +101,57 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt

$scalarTypes = TypeUtils::getConstantScalars($offsetType);
if (count($scalarTypes) === 0) {
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
$integerRangesBackup = TypeUtils::getIntegerRanges($offsetType);
$integerRanges = $integerRangesBackup;
if (count($integerRanges) > 0) {
foreach ($integerRanges as $integerRange) {
if ($integerRange->getMin() === null) {
$minRange = $integerRange->getMin();
if ($minRange === null) {
break;
}
if ($integerRange->getMax() === null) {

$maxRange = $integerRange->getMax();
if ($maxRange === null) {
break;
}

foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
$rangeValue = $minRange;
$rangeCount = 0;
do {
$rangeCount++;
if ($rangeCount > self::ARRAY_COUNT_LIMIT) {
$scalarTypes = $integerRangesBackup;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the logic. The $scalarTypes array should never contain IntegerRangeType, why do you assign it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure that the classes that triggers the issue #6375 contains int-ranges and that the range call triggers the fatal error. So I tried to test it via unit tests.

What type triggers the fatal error from issue #6375? Does the int type somehow contains the int-range? I thought that this is a new type? 🤔


break;
}

$scalarTypes[] = new ConstantIntegerType($rangeValue);
}
$rangeValue++;
} while ($rangeValue <= $maxRange);
}
}
}

if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
$match = true;
$valueTypes = $this->valueTypes;
foreach ($scalarTypes as $scalarType) {
$scalarOffsetType = ArrayType::castToArrayKeyType($scalarType);

if ($scalarOffsetType instanceof IntegerRangeType) {
$match = false;

break;
}

if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
throw new ShouldNotHappenException();
}
$offsetMatch = false;

$offsetMatch = false;
/** @var ConstantIntegerType|ConstantStringType $keyType */
foreach ($this->keyTypes as $i => $keyType) {

if ($keyType->getValue() !== $scalarOffsetType->getValue()) {
continue;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/PhpDoc/TypeDescriptionTest.php
Expand Up @@ -9,9 +9,12 @@
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -67,6 +70,24 @@ public function dataTest(): iterable
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType(new ConstantStringType('"foo"'), new IntegerType());
yield ['array{\'"foo"\': int}', $builder->getArray()];

$builder = ConstantArrayTypeBuilder::createFromConstantArray(
new ConstantArrayType(
[new ConstantIntegerType(0), new ConstantIntegerType(1)],
[new StringType(), new StringType()],
),
);
$builder->setOffsetValueType(IntegerRangeType::fromInterval(-2147483648, 2147483647), new StringType());
yield ['non-empty-array<int<-2147483648, 2147483647>, string>', $builder->getArray()];

$builder = ConstantArrayTypeBuilder::createFromConstantArray(
new ConstantArrayType(
[new ConstantIntegerType(0), new ConstantIntegerType(20)],
[new StringType(), new StringType()],
),
);
$builder->setOffsetValueType(IntegerRangeType::fromInterval(1, 19), new StringType());
yield ['non-empty-array<int<0, 20>, string>', $builder->getArray()];
}

/**
Expand Down