mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #4778 - remove already-initialized properties when checking initialisation
This commit is contained in:
parent
551a5d84ab
commit
a5408aafc9
@ -187,17 +187,24 @@ class CallAnalyzer
|
||||
$local_vars_in_scope = [];
|
||||
$local_vars_possibly_in_scope = [];
|
||||
|
||||
foreach ($context->vars_in_scope as $var => $_) {
|
||||
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
||||
$local_vars_in_scope[$var] = $context->vars_in_scope[$var];
|
||||
foreach ($context->vars_in_scope as $var_id => $type) {
|
||||
if (strpos($var_id, '$this->') === 0) {
|
||||
if ($type->initialized) {
|
||||
$local_vars_in_scope[$var_id] = $context->vars_in_scope[$var_id];
|
||||
|
||||
if (isset($context->vars_possibly_in_scope[$var_id])) {
|
||||
$local_vars_possibly_in_scope[$var_id] = $context->vars_possibly_in_scope[$var_id];
|
||||
}
|
||||
|
||||
unset($context->vars_in_scope[$var_id]);
|
||||
unset($context->vars_possibly_in_scope[$var_id]);
|
||||
}
|
||||
} elseif ($var_id !== '$this') {
|
||||
$local_vars_in_scope[$var_id] = $context->vars_in_scope[$var_id];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($context->vars_possibly_in_scope as $var => $_) {
|
||||
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
||||
$local_vars_possibly_in_scope[$var] = $context->vars_possibly_in_scope[$var];
|
||||
}
|
||||
}
|
||||
$local_vars_possibly_in_scope = $context->vars_possibly_in_scope;
|
||||
|
||||
$old_calling_method_id = $context->calling_method_id;
|
||||
|
||||
|
@ -2203,6 +2203,28 @@ class PropertyTypeTest extends TestCase
|
||||
echo $i->foo;
|
||||
}'
|
||||
],
|
||||
'noRedundantCastWhenCheckingProperties' => [
|
||||
'<?php
|
||||
class Foo
|
||||
{
|
||||
public array $map;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->map = [];
|
||||
$this->map["test"] = "test";
|
||||
|
||||
$this->useMap();
|
||||
}
|
||||
|
||||
public function useMap(): void
|
||||
{
|
||||
$keys = array_keys($this->map);
|
||||
$key = reset($keys);
|
||||
echo (string) $key;
|
||||
}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user