Skip to content
Permalink
Browse files

Fix #1915 - allow @psalm-assert-if-true to operate on $this

  • Loading branch information...
muglug committed Jul 7, 2019
1 parent 820eba1 commit 5c4ac9754637965fba5111f8e57a76ebcc218642
Showing with 53 additions and 0 deletions.
  1. +28 −0 src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php
  2. +25 −0 tests/AssertTest.php
@@ -1847,6 +1847,20 @@ protected static function processCustomAssertion(
$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 @@ protected static function processCustomAssertion(
$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]]];
}
}
}
}
}
@@ -956,6 +956,31 @@ function consume2($value)
return $value;
}'
],
'assertThisTypeIfTrue' => [
'<?php
class Type {
/**
* @psalm-assert-if-true FooType $this
*/
public function isFoo() : bool {
return $this instanceof FooType;
}
}
class FooType extends Type {
public function bar(): void {}
}
function takesType(Type $t) : void {
if ($t->isFoo()) {
$t->bar();
}
switch (true) {
case $t->isFoo():
$t->bar();
}
}'
],
];
}

0 comments on commit 5c4ac97

Please sign in to comment.
You can’t perform that action at this time.