1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Simplify ArrayAnalyzer

Ref #4714
This commit is contained in:
Matt Brown 2020-11-27 14:19:55 -05:00
parent 1ce45516db
commit 5dd4912a99
2 changed files with 341 additions and 263 deletions

View File

@ -34,23 +34,9 @@ class ArrayAnalyzer
return true;
}
$item_key_atomic_types = [];
$item_value_atomic_types = [];
$property_types = [];
$class_strings = [];
$can_create_objectlike = true;
$array_keys = [];
$int_offset_diff = 0;
$codebase = $statements_analyzer->getCodebase();
$all_list = true;
$parent_taint_nodes = [];
$array_creation_info = new ArrayCreationInfo();
foreach ($stmt->items as $int_offset => $item) {
if ($item === null) {
@ -64,244 +50,18 @@ class ArrayAnalyzer
return false;
}
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;
}
foreach ($unpacked_array_type->getAtomicTypes() as $unpacked_atomic_type) {
if ($unpacked_atomic_type instanceof Type\Atomic\TKeyedArray) {
$unpacked_array_offset = 0;
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
}
continue;
}
$item_key_atomic_types[] = new Type\Atomic\TLiteralInt($key);
$item_value_atomic_types = array_merge(
$item_value_atomic_types,
array_values($property_value->getAtomicTypes())
);
$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
|| $unpacked_atomic_type instanceof Type\Atomic\TIterable
|| (
$unpacked_atomic_type instanceof Type\Atomic\TGenericObject
&& $unpacked_atomic_type->hasTraversableInterface($codebase)
)) {
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,
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) {
$item_key_atomic_types[] = new Type\Atomic\TInt();
$item_value_atomic_types = array_merge(
$item_value_atomic_types,
array_values($unpacked_atomic_type->type_param->getAtomicTypes())
);
}
}
}
continue;
}
$item_key_value = null;
if ($item->key) {
$all_list = false;
$was_inside_use = $context->inside_use;
$context->inside_use = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->key, $context) === false) {
return false;
}
$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);
}
$item_key_atomic_types = array_merge(
$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) {
$class_strings[$item_key_value] = true;
}
} elseif ($key_type->isSingleIntLiteral()) {
$item_key_value = $key_type->getSingleIntLiteral()->value;
if ($item_key_value > $int_offset + $int_offset_diff) {
$int_offset_diff = $item_key_value - $int_offset;
}
}
}
} else {
$item_key_value = $int_offset + $int_offset_diff;
$item_key_atomic_types[] = new Type\Atomic\TInt();
}
if ($item_key_value !== null) {
if (isset($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_keys[$item_key_value] = true;
}
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);
foreach ($item_value_type->parent_nodes as $parent_node) {
$data_flow_graph->addPath(
$parent_node,
$new_parent_node,
'array-assignment'
. ($item_key_value !== null ? '-\'' . $item_key_value . '\'' : '')
self::analyzeArrayItem(
$statements_analyzer,
$context,
$array_creation_info,
$int_offset,
$item
);
}
$parent_taint_nodes += [$new_parent_node->id => $new_parent_node];
}
}
}
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 ($item_value_atomic_types && !$can_create_objectlike) {
continue;
}
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
if ($item_key_value !== null && count($property_types) <= 100) {
$property_types[$item_key_value] = $item_value_type;
} else {
$can_create_objectlike = false;
}
$item_value_atomic_types = array_merge(
$item_value_atomic_types,
array_values($item_value_type->getAtomicTypes())
);
} else {
$item_value_atomic_types[] = new Type\Atomic\TMixed();
if ($item_key_value !== null && count($property_types) <= 100) {
$property_types[$item_key_value] = Type::getMixed();
} else {
$can_create_objectlike = false;
}
}
}
if ($item_key_atomic_types) {
if ($array_creation_info->item_key_atomic_types) {
$item_key_type = TypeCombiner::combine(
$item_key_atomic_types,
$array_creation_info->item_key_atomic_types,
$codebase,
false,
true,
@ -311,9 +71,9 @@ class ArrayAnalyzer
$item_key_type = null;
}
if ($item_value_atomic_types) {
if ($array_creation_info->item_value_atomic_types) {
$item_value_type = TypeCombiner::combine(
$item_value_atomic_types,
$array_creation_info->item_value_atomic_types,
$codebase,
false,
true,
@ -327,17 +87,20 @@ class ArrayAnalyzer
if ($item_value_type
&& $item_key_type
&& ($item_key_type->hasString() || $item_key_type->hasInt())
&& $can_create_objectlike
&& $property_types
&& $array_creation_info->can_create_objectlike
&& $array_creation_info->property_types
) {
$object_like = new Type\Atomic\TKeyedArray($property_types, $class_strings);
$object_like = new Type\Atomic\TKeyedArray(
$array_creation_info->property_types,
$array_creation_info->class_strings
);
$object_like->sealed = true;
$object_like->is_list = $all_list;
$object_like->is_list = $array_creation_info->all_list;
$stmt_type = new Type\Union([$object_like]);
if ($parent_taint_nodes) {
$stmt_type->parent_nodes = $parent_taint_nodes;
if ($array_creation_info->parent_taint_nodes) {
$stmt_type->parent_nodes = $array_creation_info->parent_taint_nodes;
}
$statements_analyzer->node_data->setType($stmt, $stmt_type);
@ -345,7 +108,7 @@ class ArrayAnalyzer
return true;
}
if ($all_list) {
if ($array_creation_info->all_list) {
$array_type = new Type\Atomic\TNonEmptyList($item_value_type ?: Type::getMixed());
$array_type->count = count($stmt->items);
@ -353,8 +116,8 @@ class ArrayAnalyzer
$array_type,
]);
if ($parent_taint_nodes) {
$stmt_type->parent_nodes = $parent_taint_nodes;
if ($array_creation_info->parent_taint_nodes) {
$stmt_type->parent_nodes = $array_creation_info->parent_taint_nodes;
}
$statements_analyzer->node_data->setType($stmt, $stmt_type);
@ -397,12 +160,275 @@ class ArrayAnalyzer
$array_type,
]);
if ($parent_taint_nodes) {
$stmt_type->parent_nodes = $parent_taint_nodes;
if ($array_creation_info->parent_taint_nodes) {
$stmt_type->parent_nodes = $array_creation_info->parent_taint_nodes;
}
$statements_analyzer->node_data->setType($stmt, $stmt_type);
return true;
}
private static function analyzeArrayItem(
StatementsAnalyzer $statements_analyzer,
Context $context,
ArrayCreationInfo $array_creation_info,
int $int_offset,
PhpParser\Node\Expr\ArrayItem $item
) : 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,
$int_offset,
$item,
$unpacked_array_type
);
return;
}
$item_key_value = null;
if ($item->key) {
$array_creation_info->all_list = false;
$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;
if ($item_key_value > $int_offset + $array_creation_info->int_offset_diff) {
$array_creation_info->int_offset_diff = $item_key_value - $int_offset;
}
}
}
} else {
$item_key_value = $int_offset + $array_creation_info->int_offset_diff;
$array_creation_info->item_key_atomic_types[] = new Type\Atomic\TInt();
}
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;
}
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);
foreach ($item_value_type->parent_nodes as $parent_node) {
$data_flow_graph->addPath(
$parent_node,
$new_parent_node,
'array-assignment'
. ($item_key_value !== null ? '-\'' . $item_key_value . '\'' : '')
);
}
$array_creation_info->parent_taint_nodes += [$new_parent_node->id => $new_parent_node];
}
}
}
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,
int $int_offset,
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) {
$unpacked_array_offset = 0;
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;
}
$array_creation_info->item_key_atomic_types[] = new Type\Atomic\TLiteralInt($key);
$array_creation_info->item_value_atomic_types = array_merge(
$array_creation_info->item_value_atomic_types,
array_values($property_value->getAtomicTypes())
);
$new_int_offset = $int_offset + $array_creation_info->int_offset_diff + $unpacked_array_offset;
$array_creation_info->array_keys[$new_int_offset] = true;
$array_creation_info->property_types[$new_int_offset] = $property_value;
$unpacked_array_offset++;
}
$array_creation_info->int_offset_diff += $unpacked_array_offset - 1;
} else {
$array_creation_info->can_create_objectlike = false;
$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)
)) {
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) {
$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())
);
}
}
}
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression;
use Psalm\Type;
class ArrayCreationInfo
{
/**
* @var list<Type\Atomic>
*/
public $item_key_atomic_types = [];
/**
* @var list<Type\Atomic>
*/
public $item_value_atomic_types = [];
/**
* @var array<int|string, Type\Union>
*/
public $property_types = [];
/**
* @var array<string, true>
*/
public $class_strings = [];
/**
* @var bool
*/
public $can_create_objectlike = true;
/**
* @var array<int|string, true>
*/
public $array_keys = [];
/**
* @var int
*/
public $int_offset_diff = 0;
/**
* @var bool
*/
public $all_list = true;
/**
* @var array<string, \Psalm\Internal\DataFlow\DataFlowNode>
*/
public $parent_taint_nodes = [];
}