mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix #2172 - only complain about missing offset if it’s really missing
This commit is contained in:
parent
2fa0e05802
commit
2c9a082b3e
@ -532,13 +532,32 @@ class ArrayFetchAnalyzer
|
||||
$union_comparison_results
|
||||
);
|
||||
|
||||
if ($offset_type_contained_by_expected
|
||||
if ($codebase->config->ensure_array_string_offsets_exist
|
||||
&& $offset_type_contained_by_expected
|
||||
&& $offset_type->hasLiteralString()
|
||||
&& !$expected_offset_type->hasLiteralClassString()
|
||||
&& !$context->inside_isset
|
||||
&& !$context->inside_unset
|
||||
) {
|
||||
if ($codebase->config->ensure_array_string_offsets_exist) {
|
||||
$found_match = false;
|
||||
|
||||
foreach ($offset_type->getTypes() as $offset_type_part) {
|
||||
if ($array_var_id
|
||||
&& $offset_type_part instanceof TLiteralString
|
||||
&& isset(
|
||||
$context->vars_in_scope[
|
||||
$array_var_id . '[\'' . $offset_type_part->value . '\']'
|
||||
]
|
||||
)
|
||||
&& !$context->vars_in_scope[
|
||||
$array_var_id . '[\'' . $offset_type_part->value . '\']'
|
||||
]->possibly_undefined
|
||||
) {
|
||||
$found_match = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$found_match) {
|
||||
if (IssueBuffer::accepts(
|
||||
new PossiblyUndefinedArrayOffset(
|
||||
'Possibly undefined array offset \''
|
||||
|
@ -76,6 +76,27 @@ class ArrayAccessTest extends TestCase
|
||||
$this->analyzeFile('somefile.php', new \Psalm\Context());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEnsureArrayOffsetsExistWithIssetCheckFollowedByIsArray()
|
||||
{
|
||||
\Psalm\Config::getInstance()->ensure_array_string_offsets_exist = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/** @param array<string, mixed> $s */
|
||||
function foo(array $s) : void {
|
||||
if (isset($s["a"]) && \is_array($s["a"])) {}
|
||||
}'
|
||||
);
|
||||
|
||||
$this->analyzeFile('somefile.php', new \Psalm\Context());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user