From 24d67e9615de36b165d1f40ab34fa1b4c4c0980d Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 9 Aug 2016 18:10:46 -0400 Subject: [PATCH] Add support for special typeof types (used in get_class checks) --- src/Psalm/StatementsChecker.php | 23 ++++++++++++----------- src/Psalm/Type/T.php | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 src/Psalm/Type/T.php diff --git a/src/Psalm/StatementsChecker.php b/src/Psalm/StatementsChecker.php index 6d498cb06..20c85e8ea 100644 --- a/src/Psalm/StatementsChecker.php +++ b/src/Psalm/StatementsChecker.php @@ -2909,21 +2909,14 @@ class StatementsChecker { $type_candidate_var = null; - if ($stmt->cond instanceof PhpParser\Node\Expr\FuncCall && - $stmt->cond->name instanceof PhpParser\Node\Name && - $stmt->cond->name->parts === ['get_class']) { - - $var = $stmt->cond->args[0]->value; - - if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) { - $type_candidate_var = $var->name; - } - } - if ($this->_checkExpression($stmt->cond, $context) === false) { return false; } + if (isset($stmt->cond->inferredType) && array_values($stmt->cond->inferredType->types)[0] instanceof Type\T) { + $type_candidate_var = array_values($stmt->cond->inferredType->types)[0]->typeof; + } + $original_context = clone $context; $case_types = []; @@ -3239,6 +3232,14 @@ class StatementsChecker } } } + + if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['get_class']) { + $var = $stmt->args[0]->value; + + if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) { + $stmt->inferredType = new Type\Union([new Type\T($var->name)]); + } + } } /** diff --git a/src/Psalm/Type/T.php b/src/Psalm/Type/T.php new file mode 100644 index 000000000..5d19951f2 --- /dev/null +++ b/src/Psalm/Type/T.php @@ -0,0 +1,24 @@ +value = 'string'; + + $this->typeof = $typeof; + } +}