mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix array_key_exists calls on possibly undefined objectlike
This commit is contained in:
parent
e61815bb72
commit
b0b3c9287e
@ -875,6 +875,26 @@ class AssertionFinder
|
||||
if ($first_var_name) {
|
||||
$if_types[$first_var_name] = $prefix . 'callable';
|
||||
}
|
||||
} elseif (self::hasArrayKeyExistsCheck($expr)) {
|
||||
$array_root = isset($expr->args[1]->value)
|
||||
? ExpressionChecker::getArrayVarId(
|
||||
$expr->args[1]->value,
|
||||
$this_class_name,
|
||||
$source
|
||||
)
|
||||
: null;
|
||||
|
||||
if ($first_var_name === null && isset($expr->args[0]->value)) {
|
||||
if ($expr->args[0]->value instanceof PhpParser\Node\Scalar\String_) {
|
||||
$first_var_name = '"' . $expr->args[0]->value->value . '"';
|
||||
} elseif ($expr->args[0]->value instanceof PhpParser\Node\Scalar\LNumber) {
|
||||
$first_var_name = (string) $expr->args[0]->value->value;
|
||||
}
|
||||
}
|
||||
|
||||
if ($first_var_name !== null && $array_root) {
|
||||
$if_types[$array_root . '[' . $first_var_name . ']'] = $prefix . 'array-key-exists';
|
||||
}
|
||||
}
|
||||
|
||||
return $if_types;
|
||||
@ -1263,4 +1283,18 @@ class AssertionFinder
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PhpParser\Node\Expr\FuncCall $stmt
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function hasArrayKeyExistsCheck(PhpParser\Node\Expr\FuncCall $stmt)
|
||||
{
|
||||
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['array_key_exists']) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ class Reconciler
|
||||
return Type::getMixed();
|
||||
}
|
||||
|
||||
if ($new_var_type === 'isset' || $new_var_type === '!empty') {
|
||||
if ($new_var_type === 'isset' || $new_var_type === '!empty' || $new_var_type === 'array-key-exists') {
|
||||
return Type::getMixed();
|
||||
}
|
||||
|
||||
@ -205,7 +205,7 @@ class Reconciler
|
||||
return $existing_var_type;
|
||||
}
|
||||
|
||||
if ($new_var_type === '!isset') {
|
||||
if ($new_var_type === '!isset' || $new_var_type === '!array-key-exists') {
|
||||
return Type::getNull();
|
||||
}
|
||||
|
||||
@ -489,6 +489,12 @@ class Reconciler
|
||||
return $existing_var_type;
|
||||
}
|
||||
|
||||
if ($new_var_type === 'array-key-exists') {
|
||||
$existing_var_type->possibly_undefined = false;
|
||||
|
||||
return $existing_var_type;
|
||||
}
|
||||
|
||||
if ($new_var_type === '^!empty') {
|
||||
$existing_var_type->removeType('null');
|
||||
$existing_var_type->removeType('false');
|
||||
|
@ -850,6 +850,18 @@ class ArrayAssignmentTest extends TestCase
|
||||
echo $a[0];
|
||||
}',
|
||||
],
|
||||
'possiblyUndefinedArrayAccessWithArrayKeyExists' => [
|
||||
'<?php
|
||||
if (rand(0,1)) {
|
||||
$a = ["a" => 1];
|
||||
} else {
|
||||
$a = [2, 3];
|
||||
}
|
||||
|
||||
if (array_key_exists(0, $a)) {
|
||||
echo $a[0];
|
||||
}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -929,6 +941,32 @@ class ArrayAssignmentTest extends TestCase
|
||||
}',
|
||||
'error_message' => 'InvalidPropertyAssignmentValue',
|
||||
],
|
||||
'possiblyUndefinedArrayAccessWithArrayKeyExistsOnWrongKey' => [
|
||||
'<?php
|
||||
if (rand(0,1)) {
|
||||
$a = ["a" => 1];
|
||||
} else {
|
||||
$a = [2, 3];
|
||||
}
|
||||
|
||||
if (array_key_exists("a", $a)) {
|
||||
echo $a[0];
|
||||
}',
|
||||
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
||||
],
|
||||
'possiblyUndefinedArrayAccessWithArrayKeyExistsOnMissingKey' => [
|
||||
'<?php
|
||||
if (rand(0,1)) {
|
||||
$a = ["a" => 1];
|
||||
} else {
|
||||
$a = [2, 3];
|
||||
}
|
||||
|
||||
if (array_key_exists("b", $a)) {
|
||||
echo $a[0];
|
||||
}',
|
||||
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user