Skip to content
Permalink
Browse files

Fix #1657 - allow interface_exists to generate class-string assertion

  • Loading branch information...
muglug committed May 19, 2019
1 parent 4035823 commit 1e853800a332e46583f766b22ec4dd8bc1b67cb6
Showing with 20 additions and 3 deletions.
  1. +6 −3 src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php
  2. +14 −0 tests/ClassTest.php
@@ -1664,7 +1664,7 @@ public static function processFunctionCall(
if ($first_var_name) {
$if_types[$first_var_name] = [[$prefix . 'iterable']];
}
} elseif (self::hasClassExistsCheck($expr)) {
} elseif (self::hasClassOrInterfaceExistsCheck($expr)) {
if ($first_var_name) {
$if_types[$first_var_name] = [[$prefix . 'class-string']];
}
@@ -2285,9 +2285,12 @@ protected static function hasIterableCheck(PhpParser\Node\Expr\FuncCall $stmt)
*
* @return bool
*/
protected static function hasClassExistsCheck(PhpParser\Node\Expr\FuncCall $stmt)
protected static function hasClassOrInterfaceExistsCheck(PhpParser\Node\Expr\FuncCall $stmt)
{
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'class_exists') {
if ($stmt->name instanceof PhpParser\Node\Name
&& (($function_name = strtolower($stmt->name->parts[0])) === 'class_exists'
|| $function_name === 'interface_exists')
) {
return true;
}
@@ -371,6 +371,20 @@ function foo(string $a_class) : void {
}
}',
],
'interfaceExistsCreatesClassString' => [
'<?php
function funB(string $className) : ?ReflectionClass {
if (class_exists($className)) {
return new ReflectionClass($className);
}
if (interface_exists($className)) {
return new ReflectionClass($className);
}
return null;
}',
],
];
}

0 comments on commit 1e85380

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