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:
parent
04486ebdc0
commit
d6b98f2afe
@ -284,7 +284,8 @@ class ArrayFetchAnalyzer
|
|||||||
) {
|
) {
|
||||||
if (IssueBuffer::accepts(
|
if (IssueBuffer::accepts(
|
||||||
new PossiblyUndefinedArrayOffset(
|
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)
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
||||||
),
|
),
|
||||||
$statements_analyzer->getSuppressedIssues()
|
$statements_analyzer->getSuppressedIssues()
|
||||||
|
@ -174,6 +174,13 @@ class Reconciler
|
|||||||
$codebase = $statements_analyzer->getCodebase();
|
$codebase = $statements_analyzer->getCodebase();
|
||||||
|
|
||||||
foreach ($new_types as $key => $new_type_parts) {
|
foreach ($new_types as $key => $new_type_parts) {
|
||||||
|
if (strpos($key, '::')
|
||||||
|
&& !strpos($key, '$')
|
||||||
|
&& !strpos($key, '[')
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$has_negation = false;
|
$has_negation = false;
|
||||||
$has_isset = false;
|
$has_isset = false;
|
||||||
$has_inverted_isset = false;
|
$has_inverted_isset = false;
|
||||||
@ -498,6 +505,10 @@ class Reconciler
|
|||||||
|
|
||||||
$base_key = array_shift($key_parts);
|
$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] === '::$') {
|
if ($base_key[0] !== '$' && count($key_parts) > 2 && $key_parts[0] === '::$') {
|
||||||
$base_key .= array_shift($key_parts);
|
$base_key .= array_shift($key_parts);
|
||||||
$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();
|
$atomic_types = $existing_keys[$base_key]->getAtomicTypes();
|
||||||
|
|
||||||
while ($atomic_types) {
|
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) {
|
if ($existing_key_type_part instanceof TTemplateParam) {
|
||||||
$atomic_types = array_merge($atomic_types, $existing_key_type_part->as->getAtomicTypes());
|
$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();
|
$atomic_types = $existing_keys[$base_key]->getAtomicTypes();
|
||||||
|
|
||||||
while ($atomic_types) {
|
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) {
|
if ($existing_key_type_part instanceof TTemplateParam) {
|
||||||
$atomic_types = array_merge($atomic_types, $existing_key_type_part->as->getAtomicTypes());
|
$atomic_types = array_merge($atomic_types, $existing_key_type_part->as->getAtomicTypes());
|
||||||
|
@ -366,7 +366,10 @@ class ArrayAccessTest extends TestCase
|
|||||||
'<?php
|
'<?php
|
||||||
/** @psalm-suppress UndefinedClass */
|
/** @psalm-suppress UndefinedClass */
|
||||||
$a = new A();
|
$a = new A();
|
||||||
/** @psalm-suppress UndefinedClass */
|
/**
|
||||||
|
* @psalm-suppress UndefinedClass
|
||||||
|
* @psalm-suppress PossiblyUndefinedArrayOffset
|
||||||
|
*/
|
||||||
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
|
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
|
||||||
'assertions' => [],
|
'assertions' => [],
|
||||||
'error_levels' => ['MixedArgument', 'MixedArrayAccess'],
|
'error_levels' => ['MixedArgument', 'MixedArrayAccess'],
|
||||||
@ -992,6 +995,25 @@ class ArrayAccessTest extends TestCase
|
|||||||
$a = [1 => $test];
|
$a = [1 => $test];
|
||||||
unset($a[$test->value]);'
|
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"];
|
||||||
|
}'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1006,6 +1006,7 @@ class IssetTest extends \Psalm\Tests\TestCase
|
|||||||
if (isset($param["name"])) {
|
if (isset($param["name"])) {
|
||||||
/**
|
/**
|
||||||
* @psalm-suppress MixedArgument
|
* @psalm-suppress MixedArgument
|
||||||
|
* @psalm-suppress PossiblyUndefinedArrayOffset
|
||||||
*/
|
*/
|
||||||
echo $param["name"];
|
echo $param["name"];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user