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

Fix null array access complaints

This commit is contained in:
Matt Brown 2020-12-01 20:10:48 -05:00 committed by Daniil Gentili
parent 04486ebdc0
commit d6b98f2afe
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 39 additions and 4 deletions

View File

@ -284,7 +284,8 @@ class ArrayFetchAnalyzer
) {
if (IssueBuffer::accepts(
new PossiblyUndefinedArrayOffset(
'Possibly undefined array key ' . $keyed_array_var_id,
'Possibly undefined array key ' . $keyed_array_var_id
. ($stmt_var_type ? ' on ' . $stmt_var_type->getId() : ''),
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()

View File

@ -174,6 +174,13 @@ class Reconciler
$codebase = $statements_analyzer->getCodebase();
foreach ($new_types as $key => $new_type_parts) {
if (strpos($key, '::')
&& !strpos($key, '$')
&& !strpos($key, '[')
) {
continue;
}
$has_negation = false;
$has_isset = false;
$has_inverted_isset = false;
@ -498,6 +505,10 @@ class Reconciler
$base_key = array_shift($key_parts);
if ($base_key === 'C::A' && isset($existing_keys[$base_key]) && $existing_keys[$base_key]->isMixed()) {
throw new \Exception("Error Processing Request", 1);
}
if ($base_key[0] !== '$' && count($key_parts) > 2 && $key_parts[0] === '::$') {
$base_key .= array_shift($key_parts);
$base_key .= array_shift($key_parts);
@ -543,7 +554,7 @@ class Reconciler
$atomic_types = $existing_keys[$base_key]->getAtomicTypes();
while ($atomic_types) {
$existing_key_type_part = array_pop($atomic_types);
$existing_key_type_part = array_shift($atomic_types);
if ($existing_key_type_part instanceof TTemplateParam) {
$atomic_types = array_merge($atomic_types, $existing_key_type_part->as->getAtomicTypes());
@ -654,7 +665,7 @@ class Reconciler
$atomic_types = $existing_keys[$base_key]->getAtomicTypes();
while ($atomic_types) {
$existing_key_type_part = array_pop($atomic_types);
$existing_key_type_part = array_shift($atomic_types);
if ($existing_key_type_part instanceof TTemplateParam) {
$atomic_types = array_merge($atomic_types, $existing_key_type_part->as->getAtomicTypes());

View File

@ -366,7 +366,10 @@ class ArrayAccessTest extends TestCase
'<?php
/** @psalm-suppress UndefinedClass */
$a = new A();
/** @psalm-suppress UndefinedClass */
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress PossiblyUndefinedArrayOffset
*/
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
'assertions' => [],
'error_levels' => ['MixedArgument', 'MixedArrayAccess'],
@ -992,6 +995,25 @@ class ArrayAccessTest extends TestCase
$a = [1 => $test];
unset($a[$test->value]);'
],
'arrayAssertionShouldNotBeNull' => [
'<?php
function foo(?array $arr, string $s) : void {
/**
* @psalm-suppress PossiblyNullArrayAccess
* @psalm-suppress MixedArrayAccess
*/
if ($arr[$s]["b"] !== true) {
return;
}
/**
* @psalm-suppress MixedArgument
* @psalm-suppress MixedArrayAccess
* @psalm-suppress PossiblyNullArrayAccess
*/
echo $arr[$s]["c"];
}'
],
];
}

View File

@ -1006,6 +1006,7 @@ class IssetTest extends \Psalm\Tests\TestCase
if (isset($param["name"])) {
/**
* @psalm-suppress MixedArgument
* @psalm-suppress PossiblyUndefinedArrayOffset
*/
echo $param["name"];
}