1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #4791 - only generate special has-array-key assertions for unknown key types

This commit is contained in:
Matt Brown 2020-12-08 19:45:47 -05:00
parent 524084a64c
commit 9423324d65
2 changed files with 20 additions and 1 deletions

View File

@ -380,7 +380,11 @@ class Reconciler
if ($type[0][0] === 'array-key-exists') {
$key_parts = Reconciler::breakUpPathIntoParts($nk);
if (count($key_parts) === 4 && $key_parts[1] === '[') {
if (count($key_parts) === 4
&& $key_parts[1] === '['
&& $key_parts[2][0] !== '\''
&& !\is_numeric($key_parts[2])
) {
if (isset($new_types[$key_parts[2]])) {
$new_types[$key_parts[2]][] = ['=in-array-' . $key_parts[0]];
} else {

View File

@ -362,6 +362,21 @@ class ArrayKeyExistsTest extends \Psalm\Tests\TestCase
}',
'error_message' => 'PossiblyUndefinedArrayOffset',
],
'dontCreateWeirdString' => [
'<?php
/**
* @psalm-param array{inner:string} $options
*/
function go(array $options): void {
if (!array_key_exists(\'size\', $options)) {
throw new Exception(\'bad\');
}
/** @psalm-suppress MixedArgument */
echo $options[\'\\\'size\\\'\'];
}',
'error_message' => 'InvalidArrayOffset',
],
];
}
}