From 58835c0ad543826855e3a838dbdba10af5ed337a Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 13 Sep 2016 18:58:49 -0400 Subject: [PATCH] Add getArrayVarId for making assertions about array keys --- src/Psalm/Checker/StatementsChecker.php | 10 ++++++++++ src/Psalm/Checker/TypeChecker.php | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Checker/StatementsChecker.php b/src/Psalm/Checker/StatementsChecker.php index f75680601..71b63aa24 100644 --- a/src/Psalm/Checker/StatementsChecker.php +++ b/src/Psalm/Checker/StatementsChecker.php @@ -2211,6 +2211,16 @@ class StatementsChecker return null; } + public static function getArrayVarId(PhpParser\Node\Expr $stmt) + { + if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $stmt->dim instanceof PhpParser\Node\Scalar\String_) { + $root_var_id = self::getArrayVarId($stmt->var); + return $root_var_id ? $root_var_id . '[\'' . $stmt->dim->value . '\']' : null; + } + + return self::getVarId($stmt); + } + protected function checkArrayAssignment(PhpParser\Node\Expr\ArrayDimFetch $stmt, Context $context, Type\Union $assignment_value_type) { if ($stmt->dim && $this->checkExpression($stmt->dim, $context, false) === false) { diff --git a/src/Psalm/Checker/TypeChecker.php b/src/Psalm/Checker/TypeChecker.php index ea1d34dae..6b7e57903 100644 --- a/src/Psalm/Checker/TypeChecker.php +++ b/src/Psalm/Checker/TypeChecker.php @@ -256,7 +256,7 @@ class TypeChecker } else if ($conditional->expr instanceof PhpParser\Node\Expr\Isset_) { foreach ($conditional->expr->vars as $isset_var) { - $var_name = StatementsChecker::getVarId($isset_var); + $var_name = StatementsChecker::getVarId($isset_var) ?: StatementsChecker::getArrayVarId($isset_var); if ($var_name) { $if_types[$var_name] = 'null'; } @@ -409,7 +409,7 @@ class TypeChecker } else if ($conditional instanceof PhpParser\Node\Expr\Isset_) { foreach ($conditional->vars as $isset_var) { - $var_name = StatementsChecker::getVarId($isset_var); + $var_name = StatementsChecker::getVarId($isset_var) ?: StatementsChecker::getArrayVarId($isset_var); if ($var_name) { $if_types[$var_name] = '!null'; }