2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2018-01-18 23:16:50 +01:00
|
|
|
use Psalm\CodeLocation;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Context;
|
2018-01-18 23:16:50 +01:00
|
|
|
use Psalm\Issue\DuplicateArrayKey;
|
|
|
|
use Psalm\IssueBuffer;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type;
|
2019-02-04 22:06:14 +01:00
|
|
|
use Psalm\Internal\Type\TypeCombination;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function preg_match;
|
|
|
|
use function array_merge;
|
|
|
|
use function array_values;
|
|
|
|
use function count;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class ArrayAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\Array_ $stmt,
|
|
|
|
Context $context
|
2020-05-18 21:13:27 +02:00
|
|
|
) : bool {
|
2018-01-14 18:09:40 +01:00
|
|
|
// if the array is empty, this special type allows us to match any other array type against it
|
|
|
|
if (empty($stmt->items)) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getEmptyArray());
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 22:06:14 +01:00
|
|
|
$item_key_atomic_types = [];
|
|
|
|
$item_value_atomic_types = [];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$property_types = [];
|
2018-08-09 03:31:13 +02:00
|
|
|
$class_strings = [];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$can_create_objectlike = true;
|
|
|
|
|
2018-01-18 23:16:50 +01:00
|
|
|
$array_keys = [];
|
|
|
|
|
2018-05-30 13:42:00 +02:00
|
|
|
$int_offset_diff = 0;
|
|
|
|
|
2019-01-05 06:15:53 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
2019-10-09 00:44:46 +02:00
|
|
|
$all_list = true;
|
|
|
|
|
2020-05-22 04:47:58 +02:00
|
|
|
$parent_taint_nodes = [];
|
2019-10-12 05:28:17 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
foreach ($stmt->items as $int_offset => $item) {
|
|
|
|
if ($item === null) {
|
2020-01-04 22:33:02 +01:00
|
|
|
\Psalm\IssueBuffer::add(
|
|
|
|
new \Psalm\Issue\ParseError(
|
|
|
|
'Array element cannot be empty',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 08:07:17 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->value, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item->unpack) {
|
|
|
|
$unpacked_array_type = $statements_analyzer->node_data->getType($item->value);
|
|
|
|
|
|
|
|
if (!$unpacked_array_type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($unpacked_array_type->getAtomicTypes() as $unpacked_atomic_type) {
|
2020-08-30 17:44:14 +02:00
|
|
|
if ($unpacked_atomic_type instanceof Type\Atomic\TKeyedArray) {
|
2019-11-27 08:07:17 +01:00
|
|
|
$unpacked_array_offset = 0;
|
|
|
|
foreach ($unpacked_atomic_type->properties as $key => $property_value) {
|
2019-11-27 08:11:18 +01:00
|
|
|
if (\is_string($key)) {
|
2019-11-27 08:07:17 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateArrayKey(
|
|
|
|
'String keys are not supported in unpacked arrays',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $item->value)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item_key_atomic_types[] = new Type\Atomic\TLiteralInt($key);
|
|
|
|
$item_value_atomic_types = array_merge(
|
|
|
|
$item_value_atomic_types,
|
2020-01-04 18:20:26 +01:00
|
|
|
array_values($property_value->getAtomicTypes())
|
2019-11-27 08:07:17 +01:00
|
|
|
);
|
|
|
|
$array_keys[$int_offset + $int_offset_diff + $unpacked_array_offset] = true;
|
|
|
|
$property_types[$int_offset + $int_offset_diff + $unpacked_array_offset] = $property_value;
|
|
|
|
|
|
|
|
$unpacked_array_offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$int_offset_diff += $unpacked_array_offset - 1;
|
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
|
|
|
|
if ($unpacked_atomic_type instanceof Type\Atomic\TArray) {
|
|
|
|
if ($unpacked_atomic_type->type_params[0]->hasString()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateArrayKey(
|
|
|
|
'String keys are not supported in unpacked arrays',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $item->value)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} elseif ($unpacked_atomic_type->type_params[0]->hasInt()) {
|
|
|
|
$item_key_atomic_types[] = new Type\Atomic\TInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$item_value_atomic_types = array_merge(
|
|
|
|
$item_value_atomic_types,
|
2020-01-04 18:20:26 +01:00
|
|
|
array_values($unpacked_atomic_type->type_params[1]->getAtomicTypes())
|
2019-11-27 08:07:17 +01:00
|
|
|
);
|
|
|
|
} elseif ($unpacked_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
$item_key_atomic_types[] = new Type\Atomic\TInt();
|
|
|
|
|
|
|
|
$item_value_atomic_types = array_merge(
|
|
|
|
$item_value_atomic_types,
|
2020-01-04 18:20:26 +01:00
|
|
|
array_values($unpacked_atomic_type->type_param->getAtomicTypes())
|
2019-11-27 08:07:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:02:50 +02:00
|
|
|
$item_key_value = null;
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($item->key) {
|
2019-10-09 00:44:46 +02:00
|
|
|
$all_list = false;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->key, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($item_key_type = $statements_analyzer->node_data->getType($item->key)) {
|
|
|
|
$key_type = $item_key_type;
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2019-03-22 21:45:17 +01:00
|
|
|
if ($key_type->isNull()) {
|
|
|
|
$key_type = Type::getString('');
|
|
|
|
}
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
if ($item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
&& preg_match('/^(0|[1-9][0-9]*)$/', $item->key->value)
|
|
|
|
) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$key_type = Type::getInt(false, (int) $item->key->value);
|
2018-05-03 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
$item_key_atomic_types = array_merge(
|
|
|
|
$item_key_atomic_types,
|
|
|
|
array_values($key_type->getAtomicTypes())
|
|
|
|
);
|
2018-05-18 17:02:50 +02:00
|
|
|
|
2019-03-22 21:45:17 +01:00
|
|
|
if ($key_type->isSingleStringLiteral()) {
|
|
|
|
$item_key_literal_type = $key_type->getSingleStringLiteral();
|
2018-08-09 03:31:13 +02:00
|
|
|
$item_key_value = $item_key_literal_type->value;
|
|
|
|
|
|
|
|
if ($item_key_literal_type instanceof Type\Atomic\TLiteralClassString) {
|
|
|
|
$class_strings[$item_key_value] = true;
|
|
|
|
}
|
2019-03-22 21:45:17 +01:00
|
|
|
} elseif ($key_type->isSingleIntLiteral()) {
|
|
|
|
$item_key_value = $key_type->getSingleIntLiteral()->value;
|
2018-05-30 13:42:00 +02:00
|
|
|
|
|
|
|
if ($item_key_value > $int_offset + $int_offset_diff) {
|
2019-01-08 15:56:54 +01:00
|
|
|
$int_offset_diff = $item_key_value - $int_offset;
|
2018-05-18 17:02:50 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-30 13:42:00 +02:00
|
|
|
$item_key_value = $int_offset + $int_offset_diff;
|
2019-02-04 22:06:14 +01:00
|
|
|
$item_key_atomic_types[] = new Type\Atomic\TInt();
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-05-18 17:02:50 +02:00
|
|
|
if ($item_key_value !== null) {
|
|
|
|
if (isset($array_keys[$item_key_value])) {
|
2018-01-18 23:16:50 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateArrayKey(
|
2018-05-18 17:02:50 +02:00
|
|
|
'Key \'' . $item_key_value . '\' already exists on array',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $item)
|
2018-01-18 23:16:50 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-18 23:16:50 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:02:50 +02:00
|
|
|
$array_keys[$item_key_value] = true;
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
if ($statements_analyzer->control_flow_graph
|
2020-07-02 05:23:38 +02:00
|
|
|
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
|
2020-06-19 00:48:19 +02:00
|
|
|
if ($item_value_type->parent_nodes) {
|
|
|
|
$var_location = new CodeLocation($statements_analyzer->getSource(), $item);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$new_parent_node = \Psalm\Internal\ControlFlow\ControlFlowNode::getForAssignment(
|
2020-06-19 00:48:19 +02:00
|
|
|
'array'
|
|
|
|
. ($item_key_value !== null ? '[\'' . $item_key_value . '\']' : ''),
|
|
|
|
$var_location
|
|
|
|
);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$statements_analyzer->control_flow_graph->addNode($new_parent_node);
|
2020-06-19 00:48:19 +02:00
|
|
|
|
|
|
|
foreach ($item_value_type->parent_nodes as $parent_node) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$statements_analyzer->control_flow_graph->addPath(
|
2020-06-19 00:48:19 +02:00
|
|
|
$parent_node,
|
|
|
|
$new_parent_node,
|
|
|
|
'array-assignment'
|
|
|
|
. ($item_key_value !== null ? '-\'' . $item_key_value . '\'' : '')
|
|
|
|
);
|
|
|
|
}
|
2019-10-12 05:28:17 +02:00
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$parent_taint_nodes[$new_parent_node->id] = $new_parent_node;
|
2020-06-19 00:48:19 +02:00
|
|
|
}
|
2019-10-12 05:28:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:32:40 +02:00
|
|
|
if ($item->byRef) {
|
2020-05-18 21:13:27 +02:00
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId(
|
2019-06-08 16:32:40 +02:00
|
|
|
$item->value,
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->removeDescendents(
|
|
|
|
$var_id,
|
2019-06-12 15:13:59 +02:00
|
|
|
$context->vars_in_scope[$var_id] ?? null,
|
2019-06-08 16:32:40 +02:00
|
|
|
null,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:06:14 +01:00
|
|
|
if ($item_value_atomic_types && !$can_create_objectlike) {
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
|
2019-10-10 16:26:13 +02:00
|
|
|
if ($item_key_value !== null && count($property_types) <= 100) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$property_types[$item_key_value] = $item_value_type;
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:06:14 +01:00
|
|
|
$item_value_atomic_types = array_merge(
|
|
|
|
$item_value_atomic_types,
|
2020-01-04 18:20:26 +01:00
|
|
|
array_values($item_value_type->getAtomicTypes())
|
2019-02-04 22:06:14 +01:00
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
2019-02-04 22:06:14 +01:00
|
|
|
$item_value_atomic_types[] = new Type\Atomic\TMixed();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-10-10 16:26:13 +02:00
|
|
|
if ($item_key_value !== null && count($property_types) <= 100) {
|
2019-02-04 22:06:14 +01:00
|
|
|
$property_types[$item_key_value] = Type::getMixed();
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:06:14 +01:00
|
|
|
if ($item_key_atomic_types) {
|
|
|
|
$item_key_type = TypeCombination::combineTypes(
|
|
|
|
$item_key_atomic_types,
|
|
|
|
$codebase,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
30
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$item_key_type = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item_value_atomic_types) {
|
|
|
|
$item_value_type = TypeCombination::combineTypes(
|
|
|
|
$item_value_atomic_types,
|
|
|
|
$codebase,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
30
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$item_value_type = null;
|
|
|
|
}
|
2019-10-09 00:44:46 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
// if this array looks like an object-like array, let's return that instead
|
|
|
|
if ($item_value_type
|
|
|
|
&& $item_key_type
|
|
|
|
&& ($item_key_type->hasString() || $item_key_type->hasInt())
|
|
|
|
&& $can_create_objectlike
|
2020-01-30 05:41:17 +01:00
|
|
|
&& $property_types
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
2020-08-30 17:44:14 +02:00
|
|
|
$object_like = new Type\Atomic\TKeyedArray($property_types, $class_strings);
|
2018-05-03 19:56:30 +02:00
|
|
|
$object_like->sealed = true;
|
2019-10-09 00:44:46 +02:00
|
|
|
$object_like->is_list = $all_list;
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = new Type\Union([$object_like]);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-05-22 04:47:58 +02:00
|
|
|
if ($parent_taint_nodes) {
|
|
|
|
$stmt_type->parent_nodes = $parent_taint_nodes;
|
2019-10-12 05:28:17 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-09 00:44:46 +02:00
|
|
|
if ($all_list) {
|
|
|
|
$array_type = new Type\Atomic\TNonEmptyList($item_value_type ?: Type::getMixed());
|
|
|
|
$array_type->count = count($stmt->items);
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = new Type\Union([
|
2019-10-09 00:44:46 +02:00
|
|
|
$array_type,
|
|
|
|
]);
|
|
|
|
|
2020-05-22 04:47:58 +02:00
|
|
|
if ($parent_taint_nodes) {
|
|
|
|
$stmt_type->parent_nodes = $parent_taint_nodes;
|
2019-10-12 05:28:17 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2019-10-09 00:44:46 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:56:27 +01:00
|
|
|
$array_type = new Type\Atomic\TNonEmptyArray([
|
2020-02-22 18:20:03 +01:00
|
|
|
$item_key_type && !$item_key_type->hasMixed() ? $item_key_type : Type::getArrayKey(),
|
2018-05-03 19:56:30 +02:00
|
|
|
$item_value_type ?: Type::getMixed(),
|
|
|
|
]);
|
|
|
|
|
2018-05-18 17:02:50 +02:00
|
|
|
$array_type->count = count($stmt->items);
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = new Type\Union([
|
2018-05-03 19:56:30 +02:00
|
|
|
$array_type,
|
2018-01-14 18:09:40 +01:00
|
|
|
]);
|
|
|
|
|
2020-05-22 04:47:58 +02:00
|
|
|
if ($parent_taint_nodes) {
|
|
|
|
$stmt_type->parent_nodes = $parent_taint_nodes;
|
2019-10-12 05:28:17 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|