1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Allow union of 0|positive-int to bypass PossiblyUndefinedIntArrayOffset error

This commit is contained in:
Matt Brown 2021-05-23 13:59:40 -04:00
parent e89f7e3960
commit 938afc5aea
2 changed files with 24 additions and 0 deletions

View File

@ -866,6 +866,11 @@ class ArrayFetchAnalyzer
$found_match = true; $found_match = true;
break; break;
} }
if ($offset_type_part instanceof Type\Atomic\TPositiveInt) {
$found_match = true;
break;
}
} }
if (!$found_match) { if (!$found_match) {

View File

@ -335,6 +335,25 @@ class ArrayAccessTest extends TestCase
$this->analyzeFile('somefile.php', new \Psalm\Context()); $this->analyzeFile('somefile.php', new \Psalm\Context());
} }
public function testDontWorryWhenUnionedWithPositiveInt(): void
{
\Psalm\Config::getInstance()->ensure_array_int_offsets_exist = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @param list<string> $a
* @param 0|positive-int $b
*/
function foo(array $a, int $b): void {
echo $a[$b];
}'
);
$this->analyzeFile('somefile.php', new \Psalm\Context());
}
/** /**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}> * @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/ */