diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index 52d62ecbb..d76f4db27 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -1847,6 +1847,20 @@ class AssertionFinder $if_types[$var_name] = [[$prefix . $assertion->rule[0][0]]]; } } + } elseif ($assertion->var_id === '$this' && $expr instanceof PhpParser\Node\Expr\MethodCall) { + $var_id = ExpressionAnalyzer::getArrayVarId( + $expr->var, + $this_class_name, + $source + ); + + if ($var_id) { + if ($prefix === $assertion->rule[0][0][0]) { + $if_types[$var_id] = [[substr($assertion->rule[0][0], 1)]]; + } else { + $if_types[$var_id] = [[$prefix . $assertion->rule[0][0]]]; + } + } } } } @@ -1873,6 +1887,20 @@ class AssertionFinder $if_types[$var_name] = [[$negated_prefix . $assertion->rule[0][0]]]; } } + } elseif ($assertion->var_id === '$this' && $expr instanceof PhpParser\Node\Expr\MethodCall) { + $var_id = ExpressionAnalyzer::getArrayVarId( + $expr->var, + $this_class_name, + $source + ); + + if ($var_id) { + if ($negated_prefix === $assertion->rule[0][0][0]) { + $if_types[$var_id] = [[substr($assertion->rule[0][0], 1)]]; + } else { + $if_types[$var_id] = [[$negated_prefix . $assertion->rule[0][0]]]; + } + } } } } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index f57ab027c..cc6aaa0c7 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -956,6 +956,31 @@ class AssertTest extends TestCase return $value; }' ], + 'assertThisTypeIfTrue' => [ + 'isFoo()) { + $t->bar(); + } + switch (true) { + case $t->isFoo(): + $t->bar(); + } + }' + ], ]; }