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;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
|
|
|
use Psalm\Type\Atomic\TString;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr\Array_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
// if the array is empty, this special type allows us to match any other array type against it
|
|
|
|
if (empty($stmt->items)) {
|
|
|
|
$stmt->inferredType = Type::getEmptyArray();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item_key_type = null;
|
|
|
|
|
|
|
|
$item_value_type = null;
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
/** @var int $int_offset */
|
2018-01-14 18:09:40 +01:00
|
|
|
foreach ($stmt->items as $int_offset => $item) {
|
|
|
|
if ($item === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:02:50 +02:00
|
|
|
$item_key_value = null;
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($item->key) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($item->key->inferredType)) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$key_type = $item->key->inferredType;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($item_key_type) {
|
2019-01-05 06:15:53 +01:00
|
|
|
$item_key_type = Type::combineUnionTypes($key_type, $item_key_type, $codebase, false, true, 30);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
2018-05-03 19:56:30 +02:00
|
|
|
$item_key_type = $key_type;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2018-05-18 17:02:50 +02:00
|
|
|
|
2018-05-30 13:42:00 +02:00
|
|
|
if ($item->key->inferredType->isSingleStringLiteral()) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$item_key_literal_type = $item->key->inferredType->getSingleStringLiteral();
|
|
|
|
$item_key_value = $item_key_literal_type->value;
|
|
|
|
|
|
|
|
if ($item_key_literal_type instanceof Type\Atomic\TLiteralClassString) {
|
|
|
|
$class_strings[$item_key_value] = true;
|
|
|
|
}
|
2018-05-30 13:42:00 +02:00
|
|
|
} elseif ($item->key->inferredType->isSingleIntLiteral()) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$item_key_value = $item->key->inferredType->getSingleIntLiteral()->value;
|
2018-05-30 13:42:00 +02:00
|
|
|
|
|
|
|
if ($item_key_value > $int_offset + $int_offset_diff) {
|
|
|
|
$int_offset_diff = $item_key_value - ($int_offset + $int_offset_diff);
|
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;
|
2018-01-14 18:09:40 +01:00
|
|
|
$item_key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->value, $context) === false) {
|
2018-05-18 17:02:50 +02:00
|
|
|
return false;
|
2018-01-18 23:16:50 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($item_value_type && $item_value_type->hasMixed() && !$can_create_objectlike) {
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($item->value->inferredType)) {
|
2018-11-10 23:15:37 +01:00
|
|
|
if ($item_key_value !== null && count($property_types) <= 50) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$property_types[$item_key_value] = $item->value->inferredType;
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item_value_type) {
|
2018-11-28 21:12:08 +01:00
|
|
|
$item_value_type = Type::combineUnionTypes(
|
|
|
|
$item->value->inferredType,
|
|
|
|
clone $item_value_type,
|
2019-01-05 06:15:53 +01:00
|
|
|
$codebase,
|
2018-11-28 21:12:08 +01:00
|
|
|
false,
|
2018-12-08 19:18:55 +01:00
|
|
|
true,
|
2018-11-28 21:12:08 +01:00
|
|
|
30
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$item_value_type = $item->value->inferredType;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$item_value_type = Type::getMixed();
|
|
|
|
|
2018-11-10 23:15:37 +01:00
|
|
|
if ($item_key_value !== null && count($property_types) <= 50) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$property_types[$item_key_value] = $item_value_type;
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$object_like = new Type\Atomic\ObjectLike($property_types, $class_strings);
|
2018-05-03 19:56:30 +02:00
|
|
|
$object_like->sealed = true;
|
|
|
|
|
|
|
|
$stmt->inferredType = new Type\Union([$object_like]);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:56:27 +01:00
|
|
|
$array_type = new Type\Atomic\TNonEmptyArray([
|
2018-05-03 19:56:30 +02:00
|
|
|
$item_key_type ?: new Type\Union([new TInt, new TString]),
|
|
|
|
$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
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->inferredType = new Type\Union([
|
2018-05-03 19:56:30 +02:00
|
|
|
$array_type,
|
2018-01-14 18:09:40 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|