1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix #4743 - simplify assertions generated from array_key_exists check

This commit is contained in:
Matt Brown 2020-11-30 22:10:17 -05:00 committed by Daniil Gentili
parent c6577751da
commit 024bbef5cd
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 66 additions and 44 deletions

View File

@ -3717,6 +3717,8 @@ class AssertionFinder
): array {
$if_types = [];
$literal_assertions = [];
if (isset($expr->args[0])
&& isset($expr->args[1])
&& $first_var_type
@ -3725,8 +3727,6 @@ class AssertionFinder
&& $source instanceof StatementsAnalyzer
&& ($second_var_type = $source->node_data->getType($expr->args[1]->value))
) {
$literal_assertions = [];
foreach ($second_var_type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TArray
|| $atomic_type instanceof Type\Atomic\TKeyedArray
@ -3744,59 +3744,59 @@ class AssertionFinder
}
}
}
if ($literal_assertions) {
$if_types[$first_var_name] = [$literal_assertions];
}
}
$array_root = isset($expr->args[1]->value)
? ExpressionIdentifier::getArrayVarId(
$expr->args[1]->value,
$this_class_name,
$source
)
: null;
if ($literal_assertions) {
$if_types[$first_var_name] = [$literal_assertions];
} else {
$array_root = isset($expr->args[1]->value)
? ExpressionIdentifier::getArrayVarId(
$expr->args[1]->value,
$this_class_name,
$source
)
: null;
if ($array_root) {
if ($first_var_name === null && isset($expr->args[0])) {
$first_arg = $expr->args[0];
if ($array_root) {
if ($first_var_name === null && isset($expr->args[0])) {
$first_arg = $expr->args[0];
if ($first_arg->value instanceof PhpParser\Node\Scalar\String_) {
$first_var_name = '\'' . $first_arg->value->value . '\'';
} elseif ($first_arg->value instanceof PhpParser\Node\Scalar\LNumber) {
$first_var_name = (string)$first_arg->value->value;
}
}
if ($expr->args[0]->value instanceof PhpParser\Node\Expr\ClassConstFetch
&& $expr->args[0]->value->name instanceof PhpParser\Node\Identifier
&& $expr->args[0]->value->name->name !== 'class'
) {
$const_type = null;
if ($source instanceof StatementsAnalyzer) {
$const_type = $source->node_data->getType($expr->args[0]->value);
if ($first_arg->value instanceof PhpParser\Node\Scalar\String_) {
$first_var_name = '\'' . $first_arg->value->value . '\'';
} elseif ($first_arg->value instanceof PhpParser\Node\Scalar\LNumber) {
$first_var_name = (string)$first_arg->value->value;
}
}
if ($const_type) {
if ($const_type->isSingleStringLiteral()) {
$first_var_name = $const_type->getSingleStringLiteral()->value;
} elseif ($const_type->isSingleIntLiteral()) {
$first_var_name = (string)$const_type->getSingleIntLiteral()->value;
if ($expr->args[0]->value instanceof PhpParser\Node\Expr\ClassConstFetch
&& $expr->args[0]->value->name instanceof PhpParser\Node\Identifier
&& $expr->args[0]->value->name->name !== 'class'
) {
$const_type = null;
if ($source instanceof StatementsAnalyzer) {
$const_type = $source->node_data->getType($expr->args[0]->value);
}
if ($const_type) {
if ($const_type->isSingleStringLiteral()) {
$first_var_name = $const_type->getSingleStringLiteral()->value;
} elseif ($const_type->isSingleIntLiteral()) {
$first_var_name = (string)$const_type->getSingleIntLiteral()->value;
} else {
$first_var_name = null;
}
} else {
$first_var_name = null;
}
} else {
$first_var_name = null;
}
}
if ($first_var_name !== null
&& !strpos($first_var_name, '->')
&& !strpos($first_var_name, '[')
) {
$if_types[$array_root . '[' . $first_var_name . ']'] = [['array-key-exists']];
if ($first_var_name !== null
&& !strpos($first_var_name, '->')
&& !strpos($first_var_name, '[')
) {
$if_types[$array_root . '[' . $first_var_name . ']'] = [['array-key-exists']];
}
}
}

View File

@ -850,6 +850,28 @@ class ValueTest extends \Psalm\Tests\TestCase
[],
'8.0'
],
'arrayKeyExistsComplex' => [
'<?php
class A {
private const MAP = [
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
"e" => 5,
"f" => 6,
"g" => 7,
"h" => 8,
"i" => 9,
"j" => 10,
"k" => 11,
];
public function doWork(string $a): void {
if (!array_key_exists($a, self::MAP)) {}
}
}'
],
];
}