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

Fix #677 - handle get_class checks more accurately

This commit is contained in:
Matt Brown 2018-04-17 10:28:49 -04:00
parent b28cc19fd2
commit df5d369443
3 changed files with 31 additions and 3 deletions

View File

@ -342,7 +342,7 @@ class AssertionFinder
// fall through // fall through
} else { } else {
if ($var_name && $var_type) { if ($var_name && $var_type) {
$if_types[$var_name] = $var_type; $if_types[$var_name] = 'getclass-' . $var_type;
} }
} }
@ -614,7 +614,7 @@ class AssertionFinder
// fall through // fall through
} else { } else {
if ($var_name && $var_type) { if ($var_name && $var_type) {
$if_types[$var_name] = '!' . $var_type; $if_types[$var_name] = '!getclass-' . $var_type;
} }
} }

View File

@ -479,6 +479,8 @@ class Reconciler
) { ) {
$existing_var_type->removeType('iterable'); $existing_var_type->removeType('iterable');
$existing_var_type->addType(new TNamedObject('Traversable')); $existing_var_type->addType(new TNamedObject('Traversable'));
} elseif (substr($negated_type, 0, 9) === 'getclass-') {
$negated_type = substr($negated_type, 9);
} else { } else {
$existing_var_type->removeType($negated_type); $existing_var_type->removeType($negated_type);
} }
@ -795,6 +797,10 @@ class Reconciler
} else { } else {
$new_type = Type::getMixed(); $new_type = Type::getMixed();
} }
} elseif (substr($new_var_type, 0, 9) === 'getclass-') {
$new_var_type = substr($new_var_type, 9);
$new_type = Type::parseString($new_var_type);
$is_strict_equality = true;
} else { } else {
$new_type = Type::parseString($new_var_type); $new_type = Type::parseString($new_var_type);
} }
@ -850,7 +856,10 @@ class Reconciler
} elseif ($code_location && !$new_type->isMixed()) { } elseif ($code_location && !$new_type->isMixed()) {
$has_match = true; $has_match = true;
if ($key && $new_type->getId() === $existing_var_type->getId() && !$is_strict_equality) { if ($key
&& $new_type->getId() === $existing_var_type->getId()
&& !$is_strict_equality
) {
self::triggerIssueForImpossible( self::triggerIssueForImpossible(
$existing_var_type, $existing_var_type,
$old_var_type_string, $old_var_type_string,

View File

@ -571,6 +571,25 @@ class TypeAlgebraTest extends TestCase
} }
}', }',
], ],
'getClassComparison' => [
'<?php
class Foo {
public function bar() : void {}
}
class Bar extends Foo{
public function bar() : void {}
}
class Baz {
public function test(Foo $foo) : void {
if (get_class($foo) !== Foo::class) {
// do nothing
} else {
$foo->bar();
}
}
}',
],
]; ];
} }