Skip to content

Commit

Permalink
fix for big int-ranges
Browse files Browse the repository at this point in the history
fix issue #6375
  • Loading branch information
voku committed Jan 24, 2022
1 parent 1aef99d commit ca47adf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Expand Up @@ -104,14 +104,17 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
if (count($integerRanges) > 0) {
foreach ($integerRanges as $integerRange) {
if ($integerRange->getMin() === null) {
$minRange = $integerRange->getMin();
if ($minRange === null || $minRange <= -self::ARRAY_COUNT_LIMIT) {
break;
}
if ($integerRange->getMax() === null) {

$maxRange = $integerRange->getMax();
if ($maxRange === null || $maxRange >= self::ARRAY_COUNT_LIMIT) {
break;
}

foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
foreach (range($minRange, $maxRange) as $rangeValue) {
$scalarTypes[] = new ConstantIntegerType($rangeValue);
}
}
Expand Down
12 changes: 12 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,15 @@ 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()];
}

/**
Expand Down

0 comments on commit ca47adf

Please sign in to comment.