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;
|
2021-03-20 03:41:41 +01:00
|
|
|
|
use Psalm\Codebase;
|
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;
|
2020-11-23 21:20:39 +01:00
|
|
|
|
use Psalm\Issue\InvalidArrayOffset;
|
2020-12-16 14:18:18 +01:00
|
|
|
|
use Psalm\Issue\MixedArrayOffset;
|
2018-01-18 23:16:50 +01:00
|
|
|
|
use Psalm\IssueBuffer;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
use Psalm\Type;
|
2020-11-22 00:11:29 +01:00
|
|
|
|
use Psalm\Internal\Type\TypeCombiner;
|
2021-03-20 03:41:41 +01:00
|
|
|
|
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
|
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function preg_match;
|
|
|
|
|
use function array_merge;
|
|
|
|
|
use function array_values;
|
|
|
|
|
use function count;
|
2020-11-25 21:48:53 +01:00
|
|
|
|
use const PHP_INT_MAX;
|
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-01-05 06:15:53 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$array_creation_info = new ArrayCreationInfo();
|
2019-10-12 05:28:17 +02:00
|
|
|
|
|
2021-03-20 02:44:44 +01:00
|
|
|
|
foreach ($stmt->items as $item) {
|
2018-01-14 18:09:40 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
self::analyzeArrayItem(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$context,
|
|
|
|
|
$array_creation_info,
|
2021-03-20 03:41:41 +01:00
|
|
|
|
$item,
|
|
|
|
|
$codebase
|
2020-11-27 20:19:55 +01:00
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
if ($array_creation_info->item_key_atomic_types) {
|
2020-11-22 00:11:29 +01:00
|
|
|
|
$item_key_type = TypeCombiner::combine(
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$array_creation_info->item_key_atomic_types,
|
2019-02-04 22:06:14 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
false,
|
|
|
|
|
true,
|
|
|
|
|
30
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$item_key_type = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
if ($array_creation_info->item_value_atomic_types) {
|
2020-11-22 00:11:29 +01:00
|
|
|
|
$item_value_type = TypeCombiner::combine(
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$array_creation_info->item_value_atomic_types,
|
2019-02-04 22:06:14 +01:00
|
|
|
|
$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())
|
2020-11-27 20:19:55 +01:00
|
|
|
|
&& $array_creation_info->can_create_objectlike
|
|
|
|
|
&& $array_creation_info->property_types
|
2018-01-14 18:09:40 +01:00
|
|
|
|
) {
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$object_like = new Type\Atomic\TKeyedArray(
|
|
|
|
|
$array_creation_info->property_types,
|
|
|
|
|
$array_creation_info->class_strings
|
|
|
|
|
);
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$object_like->sealed = true;
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$object_like->is_list = $array_creation_info->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-11-27 20:19:55 +01:00
|
|
|
|
if ($array_creation_info->parent_taint_nodes) {
|
|
|
|
|
$stmt_type->parent_nodes = $array_creation_info->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
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
if ($array_creation_info->all_list) {
|
2021-03-20 03:43:58 +01:00
|
|
|
|
if (empty($array_creation_info->item_key_atomic_types)) {
|
|
|
|
|
$array_type = new Type\Atomic\TList($item_value_type ?: Type::getMixed());
|
|
|
|
|
} else {
|
|
|
|
|
$array_type = new Type\Atomic\TNonEmptyList($item_value_type ?: Type::getMixed());
|
|
|
|
|
$array_type->count = count($array_creation_info->property_types);
|
|
|
|
|
}
|
2019-10-09 00:44:46 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$stmt_type = new Type\Union([
|
2019-10-09 00:44:46 +02:00
|
|
|
|
$array_type,
|
|
|
|
|
]);
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
if ($array_creation_info->parent_taint_nodes) {
|
|
|
|
|
$stmt_type->parent_nodes = $array_creation_info->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
|
|
|
|
}
|
|
|
|
|
|
2020-11-23 21:20:39 +01:00
|
|
|
|
if ($item_key_type) {
|
2020-12-16 14:07:57 +01:00
|
|
|
|
$bad_types = [];
|
|
|
|
|
$good_types = [];
|
|
|
|
|
|
2020-11-23 21:20:39 +01:00
|
|
|
|
foreach ($item_key_type->getAtomicTypes() as $atomic_key_type) {
|
2020-12-16 14:18:18 +01:00
|
|
|
|
if ($atomic_key_type instanceof Type\Atomic\TMixed) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArrayOffset(
|
|
|
|
|
'Cannot create mixed offset – expecting array-key',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bad_types[] = $atomic_key_type;
|
|
|
|
|
|
|
|
|
|
$good_types[] = new Type\Atomic\TArrayKey;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 14:07:57 +01:00
|
|
|
|
if (!$atomic_key_type instanceof Type\Atomic\TString
|
|
|
|
|
&& !$atomic_key_type instanceof Type\Atomic\TInt
|
|
|
|
|
&& !$atomic_key_type instanceof Type\Atomic\TArrayKey
|
|
|
|
|
&& !$atomic_key_type instanceof Type\Atomic\TMixed
|
2021-01-14 22:35:06 +01:00
|
|
|
|
&& !$atomic_key_type instanceof Type\Atomic\TTemplateParam
|
2020-12-16 14:07:57 +01:00
|
|
|
|
&& !(
|
|
|
|
|
$atomic_key_type instanceof Type\Atomic\TObjectWithProperties
|
|
|
|
|
&& isset($atomic_key_type->methods['__toString'])
|
2020-11-23 21:20:39 +01:00
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidArrayOffset(
|
|
|
|
|
'Cannot create offset of type ' . $item_key_type->getKey() . ', expecting array-key',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
2020-12-16 14:18:18 +01:00
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bad_types[] = $atomic_key_type;
|
|
|
|
|
|
|
|
|
|
if ($atomic_key_type instanceof Type\Atomic\TFalse) {
|
|
|
|
|
$good_types[] = new Type\Atomic\TLiteralInt(0);
|
|
|
|
|
} elseif ($atomic_key_type instanceof Type\Atomic\TTrue) {
|
|
|
|
|
$good_types[] = new Type\Atomic\TLiteralInt(1);
|
|
|
|
|
} elseif ($atomic_key_type instanceof Type\Atomic\TBool) {
|
|
|
|
|
$good_types[] = new Type\Atomic\TLiteralInt(0);
|
|
|
|
|
$good_types[] = new Type\Atomic\TLiteralInt(1);
|
2021-05-23 20:43:30 +02:00
|
|
|
|
} elseif ($atomic_key_type instanceof Type\Atomic\TLiteralFloat) {
|
|
|
|
|
$good_types[] = new Type\Atomic\TLiteralInt((int) $atomic_key_type->value);
|
2020-12-16 14:18:18 +01:00
|
|
|
|
} elseif ($atomic_key_type instanceof Type\Atomic\TFloat) {
|
|
|
|
|
$good_types[] = new Type\Atomic\TInt;
|
|
|
|
|
} else {
|
|
|
|
|
$good_types[] = new Type\Atomic\TArrayKey;
|
2020-11-23 21:20:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-16 14:07:57 +01:00
|
|
|
|
|
|
|
|
|
if ($bad_types && $good_types) {
|
|
|
|
|
$item_key_type->substitute(
|
|
|
|
|
TypeCombiner::combine($bad_types, $codebase),
|
|
|
|
|
TypeCombiner::combine($good_types, $codebase)
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-11-23 21:20:39 +01: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(),
|
|
|
|
|
]);
|
|
|
|
|
|
2021-03-20 03:43:58 +01:00
|
|
|
|
$array_type->count = count($array_creation_info->property_types);
|
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-11-27 20:19:55 +01:00
|
|
|
|
if ($array_creation_info->parent_taint_nodes) {
|
|
|
|
|
$stmt_type->parent_nodes = $array_creation_info->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
|
|
|
|
}
|
2020-11-27 20:19:55 +01:00
|
|
|
|
|
|
|
|
|
private static function analyzeArrayItem(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
Context $context,
|
|
|
|
|
ArrayCreationInfo $array_creation_info,
|
2021-03-20 03:41:41 +01:00
|
|
|
|
PhpParser\Node\Expr\ArrayItem $item,
|
|
|
|
|
Codebase $codebase
|
2020-11-27 20:19:55 +01:00
|
|
|
|
) : void {
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->value, $context) === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($item->unpack) {
|
|
|
|
|
$unpacked_array_type = $statements_analyzer->node_data->getType($item->value);
|
|
|
|
|
|
|
|
|
|
if (!$unpacked_array_type) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::handleUnpackedArray(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$array_creation_info,
|
|
|
|
|
$item,
|
|
|
|
|
$unpacked_array_type
|
|
|
|
|
);
|
|
|
|
|
|
2021-01-20 23:42:11 +01:00
|
|
|
|
if (($data_flow_graph = $statements_analyzer->data_flow_graph)
|
|
|
|
|
&& $data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
|
|
|
|
&& $unpacked_array_type->parent_nodes
|
|
|
|
|
) {
|
|
|
|
|
$var_location = new CodeLocation($statements_analyzer->getSource(), $item->value);
|
|
|
|
|
|
|
|
|
|
$new_parent_node = \Psalm\Internal\DataFlow\DataFlowNode::getForAssignment(
|
|
|
|
|
'array',
|
|
|
|
|
$var_location
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data_flow_graph->addNode($new_parent_node);
|
|
|
|
|
|
|
|
|
|
foreach ($unpacked_array_type->parent_nodes as $parent_node) {
|
|
|
|
|
$data_flow_graph->addPath(
|
|
|
|
|
$parent_node,
|
|
|
|
|
$new_parent_node,
|
2021-03-24 20:23:56 +01:00
|
|
|
|
'arrayvalue-assignment'
|
2021-01-20 23:42:11 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->parent_taint_nodes += [$new_parent_node->id => $new_parent_node];
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$item_key_value = null;
|
2021-03-24 20:32:56 +01:00
|
|
|
|
$item_key_type = null;
|
2021-03-20 02:44:44 +01:00
|
|
|
|
$item_is_list_item = false;
|
2020-11-27 20:19:55 +01:00
|
|
|
|
|
|
|
|
|
if ($item->key) {
|
|
|
|
|
$was_inside_use = $context->inside_use;
|
|
|
|
|
$context->inside_use = true;
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->key, $context) === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$context->inside_use = $was_inside_use;
|
|
|
|
|
|
|
|
|
|
if ($item_key_type = $statements_analyzer->node_data->getType($item->key)) {
|
|
|
|
|
$key_type = $item_key_type;
|
|
|
|
|
|
|
|
|
|
if ($key_type->isNull()) {
|
|
|
|
|
$key_type = Type::getString('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
&& preg_match('/^(0|[1-9][0-9]*)$/', $item->key->value)
|
|
|
|
|
&& (
|
|
|
|
|
(int) $item->key->value < PHP_INT_MAX ||
|
|
|
|
|
$item->key->value === (string) PHP_INT_MAX
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$key_type = Type::getInt(false, (int) $item->key->value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->item_key_atomic_types = array_merge(
|
|
|
|
|
$array_creation_info->item_key_atomic_types,
|
|
|
|
|
array_values($key_type->getAtomicTypes())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($key_type->isSingleStringLiteral()) {
|
|
|
|
|
$item_key_literal_type = $key_type->getSingleStringLiteral();
|
|
|
|
|
$item_key_value = $item_key_literal_type->value;
|
|
|
|
|
|
|
|
|
|
if ($item_key_literal_type instanceof Type\Atomic\TLiteralClassString) {
|
|
|
|
|
$array_creation_info->class_strings[$item_key_value] = true;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($key_type->isSingleIntLiteral()) {
|
|
|
|
|
$item_key_value = $key_type->getSingleIntLiteral()->value;
|
|
|
|
|
|
2021-03-20 02:44:44 +01:00
|
|
|
|
if ($item_key_value >= $array_creation_info->int_offset) {
|
|
|
|
|
if ($item_key_value === $array_creation_info->int_offset) {
|
|
|
|
|
$item_is_list_item = true;
|
|
|
|
|
}
|
|
|
|
|
$array_creation_info->int_offset = $item_key_value + 1;
|
2020-11-27 20:19:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-03-20 02:44:44 +01:00
|
|
|
|
$item_is_list_item = true;
|
|
|
|
|
$item_key_value = $array_creation_info->int_offset++;
|
2021-03-20 03:43:58 +01:00
|
|
|
|
$array_creation_info->item_key_atomic_types[] = new Type\Atomic\TLiteralInt($item_key_value);
|
2020-11-27 20:19:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 02:44:44 +01:00
|
|
|
|
$array_creation_info->all_list = $array_creation_info->all_list && $item_is_list_item;
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
if ($item_key_value !== null) {
|
|
|
|
|
if (isset($array_creation_info->array_keys[$item_key_value])) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DuplicateArrayKey(
|
|
|
|
|
'Key \'' . $item_key_value . '\' already exists on array',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $item)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->array_keys[$item_key_value] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 03:41:41 +01:00
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
if (($data_flow_graph = $statements_analyzer->data_flow_graph)
|
|
|
|
|
&& ($data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
|
|
|
|
|| !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues()))
|
|
|
|
|
) {
|
|
|
|
|
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
|
|
|
|
|
if ($item_value_type->parent_nodes
|
|
|
|
|
&& !($item_value_type->isSingle()
|
|
|
|
|
&& $item_value_type->hasLiteralValue()
|
|
|
|
|
&& $data_flow_graph instanceof \Psalm\Internal\Codebase\TaintFlowGraph)
|
|
|
|
|
) {
|
|
|
|
|
$var_location = new CodeLocation($statements_analyzer->getSource(), $item);
|
|
|
|
|
|
|
|
|
|
$new_parent_node = \Psalm\Internal\DataFlow\DataFlowNode::getForAssignment(
|
|
|
|
|
'array'
|
|
|
|
|
. ($item_key_value !== null ? '[\'' . $item_key_value . '\']' : ''),
|
|
|
|
|
$var_location
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data_flow_graph->addNode($new_parent_node);
|
|
|
|
|
|
2021-03-20 03:41:41 +01:00
|
|
|
|
$event = new AddRemoveTaintsEvent($item, $context, $statements_analyzer, $codebase);
|
|
|
|
|
|
|
|
|
|
$added_taints = $codebase->config->eventDispatcher->dispatchAddTaints($event);
|
|
|
|
|
$removed_taints = $codebase->config->eventDispatcher->dispatchRemoveTaints($event);
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
foreach ($item_value_type->parent_nodes as $parent_node) {
|
|
|
|
|
$data_flow_graph->addPath(
|
|
|
|
|
$parent_node,
|
|
|
|
|
$new_parent_node,
|
2021-03-24 20:23:56 +01:00
|
|
|
|
'arrayvalue-assignment'
|
2021-03-20 03:41:41 +01:00
|
|
|
|
. ($item_key_value !== null ? '-\'' . $item_key_value . '\'' : ''),
|
|
|
|
|
$added_taints,
|
|
|
|
|
$removed_taints
|
2020-11-27 20:19:55 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->parent_taint_nodes += [$new_parent_node->id => $new_parent_node];
|
|
|
|
|
}
|
2021-03-24 20:32:56 +01:00
|
|
|
|
|
|
|
|
|
if ($item_key_type
|
|
|
|
|
&& $item_key_type->parent_nodes
|
|
|
|
|
&& $item_key_value === null
|
|
|
|
|
&& !($item_key_type->isSingle()
|
|
|
|
|
&& $item_key_type->hasLiteralValue()
|
|
|
|
|
&& $data_flow_graph instanceof \Psalm\Internal\Codebase\TaintFlowGraph)
|
|
|
|
|
) {
|
|
|
|
|
$var_location = new CodeLocation($statements_analyzer->getSource(), $item);
|
|
|
|
|
|
|
|
|
|
$new_parent_node = \Psalm\Internal\DataFlow\DataFlowNode::getForAssignment(
|
|
|
|
|
'array',
|
|
|
|
|
$var_location
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data_flow_graph->addNode($new_parent_node);
|
|
|
|
|
|
|
|
|
|
$event = new AddRemoveTaintsEvent($item, $context, $statements_analyzer, $codebase);
|
|
|
|
|
|
|
|
|
|
$added_taints = $codebase->config->eventDispatcher->dispatchAddTaints($event);
|
|
|
|
|
$removed_taints = $codebase->config->eventDispatcher->dispatchRemoveTaints($event);
|
|
|
|
|
|
|
|
|
|
foreach ($item_key_type->parent_nodes as $parent_node) {
|
|
|
|
|
$data_flow_graph->addPath(
|
|
|
|
|
$parent_node,
|
|
|
|
|
$new_parent_node,
|
|
|
|
|
'arraykey-assignment',
|
|
|
|
|
$added_taints,
|
|
|
|
|
$removed_taints
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->parent_taint_nodes += [$new_parent_node->id => $new_parent_node];
|
|
|
|
|
}
|
2020-11-27 20:19:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($item->byRef) {
|
|
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$item->value,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
|
$context->removeDescendents(
|
|
|
|
|
$var_id,
|
|
|
|
|
$context->vars_in_scope[$var_id] ?? null,
|
|
|
|
|
null,
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($array_creation_info->item_value_atomic_types && !$array_creation_info->can_create_objectlike) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
|
|
|
|
|
if ($item_key_value !== null && count($array_creation_info->property_types) <= 100) {
|
|
|
|
|
$array_creation_info->property_types[$item_key_value] = $item_value_type;
|
|
|
|
|
} else {
|
|
|
|
|
$array_creation_info->can_create_objectlike = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->item_value_atomic_types = array_merge(
|
|
|
|
|
$array_creation_info->item_value_atomic_types,
|
|
|
|
|
array_values($item_value_type->getAtomicTypes())
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$array_creation_info->item_value_atomic_types[] = new Type\Atomic\TMixed();
|
|
|
|
|
|
|
|
|
|
if ($item_key_value !== null && count($array_creation_info->property_types) <= 100) {
|
|
|
|
|
$array_creation_info->property_types[$item_key_value] = Type::getMixed();
|
|
|
|
|
} else {
|
|
|
|
|
$array_creation_info->can_create_objectlike = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function handleUnpackedArray(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
ArrayCreationInfo $array_creation_info,
|
|
|
|
|
PhpParser\Node\Expr\ArrayItem $item,
|
|
|
|
|
Type\Union $unpacked_array_type
|
|
|
|
|
) : void {
|
|
|
|
|
foreach ($unpacked_array_type->getAtomicTypes() as $unpacked_atomic_type) {
|
|
|
|
|
if ($unpacked_atomic_type instanceof Type\Atomic\TKeyedArray) {
|
|
|
|
|
foreach ($unpacked_atomic_type->properties as $key => $property_value) {
|
|
|
|
|
if (\is_string($key)) {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 03:43:58 +01:00
|
|
|
|
$new_int_offset = $array_creation_info->int_offset++;
|
|
|
|
|
|
|
|
|
|
$array_creation_info->item_key_atomic_types[] = new Type\Atomic\TLiteralInt($new_int_offset);
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$array_creation_info->item_value_atomic_types = array_merge(
|
|
|
|
|
$array_creation_info->item_value_atomic_types,
|
|
|
|
|
array_values($property_value->getAtomicTypes())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$array_creation_info->array_keys[$new_int_offset] = true;
|
|
|
|
|
$array_creation_info->property_types[$new_int_offset] = $property_value;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
|
|
if ($unpacked_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
|
|| $unpacked_atomic_type instanceof Type\Atomic\TIterable
|
|
|
|
|
|| (
|
|
|
|
|
$unpacked_atomic_type instanceof Type\Atomic\TGenericObject
|
|
|
|
|
&& $unpacked_atomic_type->hasTraversableInterface($codebase)
|
2021-03-23 04:26:03 +01:00
|
|
|
|
&& \count($unpacked_atomic_type->type_params) === 2
|
2020-11-27 20:19:55 +01:00
|
|
|
|
)) {
|
2021-03-23 04:26:03 +01:00
|
|
|
|
/** @psalm-suppress PossiblyUndefinedArrayOffset provably true, but Psalm can’t see it */
|
2021-03-20 03:43:58 +01:00
|
|
|
|
if ($unpacked_atomic_type->type_params[1]->isEmpty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$array_creation_info->can_create_objectlike = false;
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
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()) {
|
|
|
|
|
$array_creation_info->item_key_atomic_types[] = new Type\Atomic\TInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_creation_info->item_value_atomic_types = array_merge(
|
|
|
|
|
$array_creation_info->item_value_atomic_types,
|
|
|
|
|
array_values(
|
|
|
|
|
isset($unpacked_atomic_type->type_params[1])
|
|
|
|
|
? $unpacked_atomic_type->type_params[1]->getAtomicTypes()
|
|
|
|
|
: [new Type\Atomic\TMixed()]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} elseif ($unpacked_atomic_type instanceof Type\Atomic\TList) {
|
2021-03-20 03:43:58 +01:00
|
|
|
|
if ($unpacked_atomic_type->type_param->isEmpty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$array_creation_info->can_create_objectlike = false;
|
|
|
|
|
|
2020-11-27 20:19:55 +01:00
|
|
|
|
$array_creation_info->item_key_atomic_types[] = new Type\Atomic\TInt();
|
|
|
|
|
|
|
|
|
|
$array_creation_info->item_value_atomic_types = array_merge(
|
|
|
|
|
$array_creation_info->item_value_atomic_types,
|
|
|
|
|
array_values($unpacked_atomic_type->type_param->getAtomicTypes())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
}
|