Skip to content

Commit

Permalink
Fix error The supplied range exceeds the maximum array size
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 29, 2022
1 parent 8213dd7 commit d3f968d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
break;
}

$rangeLength = $integerRange->getMax() - $integerRange->getMin();
if ($rangeLength >= self::ARRAY_COUNT_LIMIT) {
$scalarTypes = [];
break;
}

foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
$scalarTypes[] = new ConstantIntegerType($rangeValue);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ public function testBug6442(): void
$this->assertSame(9, $errors[1]->getLine());
}

public function testBug6375(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-6375.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6375.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug6375;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param int<-2147483648, 2147483647> $i
* @return void
*/
public function sayHello($i): void
{
$a = [];
$a[$i] = 5;
assertType('non-empty-array<int<-2147483648, 2147483647>, 5>', $a);
}
}

0 comments on commit d3f968d

Please sign in to comment.