Skip to content

Commit

Permalink
Allow union of 0|positive-int to bypass PossiblyUndefinedIntArrayOffs…
Browse files Browse the repository at this point in the history
…et error
  • Loading branch information
muglug committed May 23, 2021
1 parent e89f7e3 commit 938afc5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Expand Up @@ -866,6 +866,11 @@ private static function checkLiteralIntArrayOffset(
$found_match = true;
break;
}

if ($offset_type_part instanceof Type\Atomic\TPositiveInt) {
$found_match = true;
break;
}
}

if (!$found_match) {
Expand Down
19 changes: 19 additions & 0 deletions tests/ArrayAccessTest.php
Expand Up @@ -335,6 +335,25 @@ function takesList(array $arr) : void {
$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[]}>
*/
Expand Down

0 comments on commit 938afc5

Please sign in to comment.