2019-02-16 17:16:52 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Scanner;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Codebase;
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
|
|
|
use function implode;
|
|
|
|
use function is_string;
|
|
|
|
use function strpos;
|
|
|
|
use function str_replace;
|
2019-02-16 17:16:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class PhpStormMetaScanner
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $args
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-02-17 00:50:25 +01:00
|
|
|
public static function handleOverride(array $args, Codebase $codebase)
|
2019-02-16 17:16:52 +01:00
|
|
|
{
|
|
|
|
$identifier = $args[0]->value;
|
|
|
|
|
|
|
|
if (!$args[1]->value instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|| !$args[1]->value->name instanceof PhpParser\Node\Name
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$map = [];
|
|
|
|
|
|
|
|
if ($args[1]->value->name->parts === ['map']
|
|
|
|
&& $args[1]->value->args
|
|
|
|
&& $args[1]->value->args[0]->value instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
|
|
|
foreach ($args[1]->value->args[0]->value->items as $array_item) {
|
|
|
|
if ($array_item
|
|
|
|
&& $array_item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
) {
|
|
|
|
if ($array_item->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $array_item->value->class instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
&& $array_item->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& strtolower($array_item->value->name->name)
|
|
|
|
) {
|
|
|
|
$map[$array_item->key->value] = new Type\Union([
|
|
|
|
new Type\Atomic\TNamedObject(implode('\\', $array_item->value->class->parts))
|
|
|
|
]);
|
2019-02-16 18:16:34 +01:00
|
|
|
} elseif ($array_item->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$map[$array_item->key->value] = $array_item->value->value;
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_offset = null;
|
|
|
|
|
|
|
|
if ($args[1]->value->name->parts === ['type']
|
|
|
|
&& $args[1]->value->args
|
|
|
|
&& $args[1]->value->args[0]->value instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
|
|
|
$type_offset = $args[1]->value->args[0]->value->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$element_type_offset = null;
|
|
|
|
|
|
|
|
if ($args[1]->value->name->parts === ['elementType']
|
|
|
|
&& $args[1]->value->args
|
|
|
|
&& $args[1]->value->args[0]->value instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
|
|
|
$element_type_offset = $args[1]->value->args[0]->value->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($identifier instanceof PhpParser\Node\Expr\StaticCall
|
|
|
|
&& $identifier->class instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
&& $identifier->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& $identifier->args
|
|
|
|
&& $identifier->args[0]->value instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
2019-02-27 23:06:44 +01:00
|
|
|
$meta_fq_classlike_name = implode('\\', $identifier->class->parts);
|
|
|
|
|
2020-03-01 23:11:44 +01:00
|
|
|
$meta_method_name = strtolower($identifier->name->name);
|
2019-02-16 17:16:52 +01:00
|
|
|
|
|
|
|
if ($map) {
|
|
|
|
$offset = $identifier->args[0]->value->value;
|
|
|
|
|
|
|
|
$codebase->methods->return_type_provider->registerClosure(
|
2019-02-27 23:06:44 +01:00
|
|
|
$meta_fq_classlike_name,
|
2019-02-16 17:16:52 +01:00
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-02-27 23:06:44 +01:00
|
|
|
* @return ?Type\Union
|
2019-02-16 17:16:52 +01:00
|
|
|
*/
|
|
|
|
function (
|
2019-11-25 17:44:54 +01:00
|
|
|
\Psalm\StatementsSource $statements_analyzer,
|
2019-02-27 23:06:44 +01:00
|
|
|
string $fq_classlike_name,
|
|
|
|
string $method_name,
|
2019-02-16 17:16:52 +01:00
|
|
|
array $call_args,
|
|
|
|
Context $_context,
|
|
|
|
CodeLocation $_code_location
|
|
|
|
) use (
|
|
|
|
$map,
|
2019-02-27 23:06:44 +01:00
|
|
|
$offset,
|
|
|
|
$meta_fq_classlike_name,
|
|
|
|
$meta_method_name
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
if ($meta_method_name !== $method_name
|
|
|
|
|| $meta_fq_classlike_name !== $fq_classlike_name
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (isset($call_args[$offset]->value)
|
|
|
|
&& ($call_arg_type = $statements_analyzer->node_data->getType($call_args[$offset]->value))
|
2019-02-16 17:16:52 +01:00
|
|
|
&& $call_arg_type->isSingleStringLiteral()
|
|
|
|
) {
|
|
|
|
$offset_arg_value = $call_arg_type->getSingleStringLiteral()->value;
|
|
|
|
|
2019-02-16 18:16:34 +01:00
|
|
|
if ($mapped_type = $map[$offset_arg_value] ?? null) {
|
|
|
|
if ($mapped_type instanceof Type\Union) {
|
|
|
|
return clone $mapped_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($mapped_type = $map[''] ?? null) && is_string($mapped_type)) {
|
|
|
|
if (strpos($mapped_type, '@') !== false) {
|
|
|
|
$mapped_type = str_replace('@', $offset_arg_value, $mapped_type);
|
|
|
|
|
|
|
|
if (strpos($mapped_type, '.') === false) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TNamedObject($mapped_type)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
return null;
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} elseif ($type_offset !== null) {
|
|
|
|
$codebase->methods->return_type_provider->registerClosure(
|
2019-02-27 23:06:44 +01:00
|
|
|
$meta_fq_classlike_name,
|
2019-02-16 17:16:52 +01:00
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-02-27 23:06:44 +01:00
|
|
|
* @return ?Type\Union
|
2019-02-16 17:16:52 +01:00
|
|
|
*/
|
|
|
|
function (
|
2019-11-25 17:44:54 +01:00
|
|
|
\Psalm\StatementsSource $statements_analyzer,
|
2019-02-27 23:06:44 +01:00
|
|
|
string $fq_classlike_name,
|
|
|
|
string $method_name,
|
2019-02-16 17:16:52 +01:00
|
|
|
array $call_args,
|
|
|
|
Context $_context,
|
|
|
|
CodeLocation $_code_location
|
|
|
|
) use (
|
|
|
|
$map,
|
2019-02-27 23:06:44 +01:00
|
|
|
$type_offset,
|
|
|
|
$meta_fq_classlike_name,
|
|
|
|
$meta_method_name
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
if ($meta_method_name !== $method_name
|
|
|
|
|| $meta_fq_classlike_name !== $fq_classlike_name
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (isset($call_args[$type_offset]->value)
|
|
|
|
&& ($call_arg_type
|
|
|
|
= $statements_analyzer->node_data->getType($call_args[$type_offset]->value))
|
|
|
|
) {
|
2019-02-16 17:16:52 +01:00
|
|
|
return clone $call_arg_type;
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
return null;
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} elseif ($element_type_offset !== null) {
|
|
|
|
$codebase->methods->return_type_provider->registerClosure(
|
2019-02-27 23:06:44 +01:00
|
|
|
$meta_fq_classlike_name,
|
2019-02-16 17:16:52 +01:00
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-02-27 23:06:44 +01:00
|
|
|
* @return ?Type\Union
|
2019-02-16 17:16:52 +01:00
|
|
|
*/
|
|
|
|
function (
|
2019-11-25 17:44:54 +01:00
|
|
|
\Psalm\StatementsSource $statements_analyzer,
|
2019-02-27 23:06:44 +01:00
|
|
|
string $fq_classlike_name,
|
|
|
|
string $method_name,
|
2019-02-16 17:16:52 +01:00
|
|
|
array $call_args,
|
|
|
|
Context $_context,
|
|
|
|
CodeLocation $_code_location
|
|
|
|
) use (
|
|
|
|
$map,
|
2019-02-27 23:06:44 +01:00
|
|
|
$element_type_offset,
|
|
|
|
$meta_fq_classlike_name,
|
|
|
|
$meta_method_name
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
if ($meta_method_name !== $method_name
|
|
|
|
|| $meta_fq_classlike_name !== $fq_classlike_name
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (isset($call_args[$element_type_offset]->value)
|
|
|
|
&& ($call_arg_type
|
|
|
|
= $statements_analyzer->node_data->getType($call_args[$element_type_offset]->value))
|
|
|
|
&& $call_arg_type->hasArray()
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
|
|
|
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
$array_atomic_type = $call_arg_type->getAtomicTypes()['array'];
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
return $array_atomic_type->getGenericValueType();
|
|
|
|
}
|
2019-10-11 02:16:43 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($array_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
return $array_atomic_type->type_param;
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
return clone $array_atomic_type->type_params[1];
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
return null;
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($identifier instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $identifier->name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
&& $identifier->args
|
|
|
|
&& $identifier->args[0]->value instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
|
|
|
$function_id = implode('\\', $identifier->name->parts);
|
|
|
|
|
|
|
|
if ($map) {
|
|
|
|
$offset = $identifier->args[0]->value->value;
|
|
|
|
|
|
|
|
$codebase->functions->return_type_provider->registerClosure(
|
|
|
|
$function_id,
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
*/
|
|
|
|
function (
|
2019-04-12 06:44:10 +02:00
|
|
|
\Psalm\StatementsSource $statements_analyzer,
|
2019-02-16 17:16:52 +01:00
|
|
|
string $function_id,
|
|
|
|
array $call_args,
|
|
|
|
Context $_context,
|
|
|
|
CodeLocation $_code_location
|
|
|
|
) use (
|
|
|
|
$map,
|
|
|
|
$offset
|
|
|
|
) : Type\Union {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($call_args[$offset]->value)
|
|
|
|
&& ($call_arg_type
|
|
|
|
= $statements_analyzer->node_data->getType($call_args[$offset]->value))
|
2019-02-16 17:16:52 +01:00
|
|
|
&& $call_arg_type->isSingleStringLiteral()
|
|
|
|
) {
|
|
|
|
$offset_arg_value = $call_arg_type->getSingleStringLiteral()->value;
|
|
|
|
|
2019-02-16 18:16:34 +01:00
|
|
|
if ($mapped_type = $map[$offset_arg_value] ?? null) {
|
|
|
|
if ($mapped_type instanceof Type\Union) {
|
|
|
|
return clone $mapped_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($mapped_type = $map[''] ?? null) && is_string($mapped_type)) {
|
|
|
|
if (strpos($mapped_type, '@') !== false) {
|
|
|
|
$mapped_type = str_replace('@', $offset_arg_value, $mapped_type);
|
|
|
|
|
|
|
|
if (strpos($mapped_type, '.') === false) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TNamedObject($mapped_type)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $statements_analyzer->getCodebase()->functions->getStorage(
|
|
|
|
$statements_analyzer,
|
|
|
|
$function_id
|
|
|
|
);
|
|
|
|
|
|
|
|
return $storage->return_type ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} elseif ($type_offset !== null) {
|
|
|
|
$codebase->functions->return_type_provider->registerClosure(
|
|
|
|
$function_id,
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
*/
|
|
|
|
function (
|
2019-04-12 06:44:10 +02:00
|
|
|
\Psalm\StatementsSource $statements_analyzer,
|
2019-02-16 17:16:52 +01:00
|
|
|
string $function_id,
|
|
|
|
array $call_args,
|
|
|
|
Context $_context,
|
|
|
|
CodeLocation $_code_location
|
|
|
|
) use (
|
|
|
|
$map,
|
|
|
|
$type_offset
|
|
|
|
) : Type\Union {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
|
|
|
return Type::getMixed();
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (isset($call_args[$type_offset]->value)
|
|
|
|
&& ($call_arg_type
|
|
|
|
= $statements_analyzer->node_data->getType($call_args[$type_offset]->value))
|
|
|
|
) {
|
|
|
|
return clone $call_arg_type;
|
2019-04-12 06:44:10 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 18:16:34 +01:00
|
|
|
$storage = $statements_analyzer->getCodebase()->functions->getStorage(
|
|
|
|
$statements_analyzer,
|
2019-02-16 17:16:52 +01:00
|
|
|
$function_id
|
|
|
|
);
|
|
|
|
|
|
|
|
return $storage->return_type ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} elseif ($element_type_offset !== null) {
|
|
|
|
$codebase->functions->return_type_provider->registerClosure(
|
|
|
|
$function_id,
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
*/
|
|
|
|
function (
|
2019-04-12 06:44:10 +02:00
|
|
|
\Psalm\StatementsSource $statements_analyzer,
|
2019-02-16 17:16:52 +01:00
|
|
|
string $function_id,
|
|
|
|
array $call_args,
|
|
|
|
Context $_context,
|
|
|
|
CodeLocation $_code_location
|
|
|
|
) use (
|
|
|
|
$map,
|
|
|
|
$element_type_offset
|
|
|
|
) : Type\Union {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
2019-02-16 17:16:52 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (isset($call_args[$element_type_offset]->value)
|
|
|
|
&& ($call_arg_type
|
|
|
|
= $statements_analyzer->node_data->getType($call_args[$element_type_offset]->value))
|
|
|
|
&& $call_arg_type->hasArray()
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
|
|
|
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
$array_atomic_type = $call_arg_type->getAtomicTypes()['array'];
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
return $array_atomic_type->getGenericValueType();
|
|
|
|
}
|
2019-10-11 02:16:43 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($array_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
return $array_atomic_type->type_param;
|
2019-02-16 17:16:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
return clone $array_atomic_type->type_params[1];
|
2019-04-12 06:44:10 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 18:16:34 +01:00
|
|
|
$storage = $statements_analyzer->getCodebase()->functions->getStorage(
|
|
|
|
$statements_analyzer,
|
2019-02-16 17:16:52 +01:00
|
|
|
$function_id
|
|
|
|
);
|
|
|
|
|
|
|
|
return $storage->return_type ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|