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

Refine isset checks

This commit is contained in:
Matthew Brown 2019-10-17 01:09:21 -04:00
parent 54a31f8cff
commit e8618371fb
2 changed files with 22 additions and 10 deletions

View File

@ -96,16 +96,7 @@ class IfAnalyzer
$mixed_var_ids = []; $mixed_var_ids = [];
foreach ($if_context->vars_in_scope as $var_id => $type) { foreach ($if_context->vars_in_scope as $var_id => $type) {
if ($type->hasMixed()) { if ($type->hasMixed() && isset($context->vars_in_scope[$var_id])) {
foreach ($if_context->vars_in_scope as $alt_var_id => $alt_type) {
if ($alt_var_id !== $alt_type
&& preg_match('/^' . preg_quote($alt_var_id, '/') . '(\[|-)/', $var_id)
&& !$alt_type->hasMixed()
) {
continue 2;
}
}
$mixed_var_ids[] = $var_id; $mixed_var_ids[] = $var_id;
} }
} }

View File

@ -358,6 +358,27 @@ class AssertTest extends TestCase
if ($c && rand(0, 1)) {} if ($c && rand(0, 1)) {}
}' }'
], ],
'assertMixedOffsetExists' => [
'<?php
class A {
/** @var mixed */
private $arr;
/**
* @psalm-suppress MixedArrayAccess
* @psalm-suppress MixedReturnStatement
* @psalm-suppress MixedInferredReturnType
*/
public function foo() : stdClass {
if (isset($this->arr[0])) {
return $this->arr[0];
}
$this->arr[0] = new stdClass;
return $this->arr[0];
}
}'
],
]; ];
} }
} }