1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-03 10:07:52 +01:00

Add getArrayVarId for making assertions about array keys

This commit is contained in:
Matthew Brown 2016-09-13 18:58:49 -04:00
parent 352f5e64b3
commit 58835c0ad5
2 changed files with 12 additions and 2 deletions

View File

@ -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) {

View File

@ -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';
}