1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix #4778 - remove already-initialized properties when checking initialisation

This commit is contained in:
Matt Brown 2020-12-06 11:07:59 -05:00 committed by Daniil Gentili
parent 551a5d84ab
commit a5408aafc9
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 37 additions and 8 deletions

View File

@ -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;

View File

@ -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;
}
}'
],
];
}