1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Filter conditions better when encountering parent instanceof

This commit is contained in:
Matt Brown 2018-05-16 11:47:39 -04:00
parent 1ecf1c4113
commit 3263b64301
3 changed files with 30 additions and 5 deletions

View File

@ -665,6 +665,8 @@ class Reconciler
$any_scalar_type_match_found = false;
$matching_atomic_types = [];
foreach ($new_type->getTypes() as $new_type_part) {
$has_local_match = false;
@ -689,6 +691,9 @@ class Reconciler
) || $type_coerced
) {
$has_local_match = true;
if ($type_coerced) {
$matching_atomic_types[] = $existing_var_type_part;
}
break;
}
@ -719,6 +724,10 @@ class Reconciler
}
}
if ($matching_atomic_types) {
$new_type = new Type\Union($matching_atomic_types);
}
if (!$has_match && (!$is_loose_equality || !$any_scalar_type_match_found)) {
if ($new_var_type === 'null') {
if ($existing_var_type->from_docblock) {

View File

@ -795,15 +795,15 @@ class TypeAlgebraTest extends TestCase
],
'instanceofNoRedundant' => [
'<?php
function logic(Bar $a, ?Bar $b) : void {
if ((!$a instanceof Foo || !$b instanceof Foo)
&& (!$a instanceof Foo || !$b instanceof Bar)
&& (!$a instanceof Bar || !$b instanceof Foo)
function logic(Foo $a, Foo $b) : void {
if ((!$a instanceof Bat || !$b instanceof Bat)
&& (!$a instanceof Bat || !$b instanceof Bar)
&& (!$a instanceof Bar || !$b instanceof Bat)
&& (!$a instanceof Bar || !$b instanceof Bar)
) {
} else {
if ($b instanceof Foo) {}
if ($b instanceof Bat) {}
}
}

View File

@ -828,6 +828,22 @@ class TypeReconciliationTest extends TestCase
if ($f == 0) {}
}',
],
'filterSubclassBasedOnParentInstanceof' => [
'<?php
class A {}
class B extends A {
public function foo() : void {}
}
class C {}
class D extends C {}
$b_or_d = rand(0, 1) ? new B : new D;
if ($b_or_d instanceof A) {
$b_or_d->foo();
}',
],
];
}