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

Fix #4794 - invvalidate dependent types when their variables change

This commit is contained in:
Matt Brown 2020-12-06 18:14:21 -05:00 committed by Daniil Gentili
parent 35fc84cdbb
commit ce9d100908
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 77 additions and 3 deletions

View File

@ -656,10 +656,18 @@ class Context
$statements_analyzer
);
foreach ($this->vars_in_scope as $var_id => $_) {
foreach ($this->vars_in_scope as $var_id => $type) {
if (preg_match('/' . preg_quote($remove_var_id, '/') . '[\]\[\-]/', $var_id)) {
unset($this->vars_in_scope[$var_id]);
}
foreach ($type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\DependentType
&& $atomic_type->getVarId() === $remove_var_id
) {
$type->addType($atomic_type->getReplacement());
}
}
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace Psalm\Type\Atomic;
interface DependentType
{
public function getVarId() : string;
/**
* This returns a replacement type for when the dependent data is invalidated
*/
public function getReplacement() : \Psalm\Type\Atomic;
}

View File

@ -6,7 +6,7 @@ use Psalm\Type\Union;
/**
* Represents a string whose value is a fully-qualified class found by get_class($var)
*/
class TDependentGetClass extends TString
class TDependentGetClass extends TString implements DependentType
{
/**
* Used to hold information as to what this refers to
@ -37,6 +37,23 @@ class TDependentGetClass extends TString
: 'class-string<' . $this->as_type->getId() . '>';
}
public function getKey(bool $include_extra = true): string
{
return 'get-class-of<' . $this->typeof
. (!$this->as_type->isMixed() && !$this->as_type->hasObject() ? ', ' . $this->as_type->getId() : '')
. '>';
}
public function getVarId() : string
{
return $this->typeof;
}
public function getReplacement() : \Psalm\Type\Atomic
{
return new TClassString();
}
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;

View File

@ -4,7 +4,7 @@ namespace Psalm\Type\Atomic;
/**
* Represents a string whose value is that of a type found by get_debug_type($var)
*/
class TDependentGetDebugType extends TString
class TDependentGetDebugType extends TString implements DependentType
{
/**
* Used to hold information as to what this refers to
@ -21,6 +21,21 @@ class TDependentGetDebugType extends TString
$this->typeof = $typeof;
}
public function getKey(bool $include_extra = true): string
{
return 'get-debug-type-of<' . $this->typeof . '>';
}
public function getVarId() : string
{
return $this->typeof;
}
public function getReplacement() : \Psalm\Type\Atomic
{
return new TString();
}
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;

View File

@ -1301,6 +1301,28 @@ class SwitchTypeTest extends TestCase
}',
'error_message' => 'InvalidReturnType'
],
'clearDependentTypeWhenAssigning' => [
'<?php
class A {}
class AChild extends A {
public function bar() : void {}
}
class B {}
function foo(A $a) : void {
$a_class = get_class($a);
$a = new B();
switch ($a_class) {
case AChild::class:
$a->bar();
}
}',
'error_message' => 'UndefinedMethod'
],
];
}
}