2016-11-01 19:14:35 +01:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-07-22 01:40:35 +02:00
|
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2019-11-04 03:27:40 +01:00
|
|
|
|
use Psalm\Internal\Type\TemplateResult;
|
2016-12-04 01:11:30 +01:00
|
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use Psalm\Context;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\InvalidArgument;
|
|
|
|
|
use Psalm\Issue\InvalidScalarArgument;
|
2019-04-26 00:02:19 +02:00
|
|
|
|
use Psalm\Issue\MixedArgumentTypeCoercion;
|
|
|
|
|
use Psalm\Issue\ArgumentTypeCoercion;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\UndefinedFunction;
|
|
|
|
|
use Psalm\IssueBuffer;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Type;
|
2017-05-19 06:48:26 +02:00
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function strtolower;
|
|
|
|
|
use function strpos;
|
|
|
|
|
use function count;
|
|
|
|
|
use function in_array;
|
2019-09-06 03:00:02 +02:00
|
|
|
|
use function is_null;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function is_string;
|
|
|
|
|
use function preg_match;
|
|
|
|
|
use function preg_replace;
|
2019-09-06 03:00:02 +02:00
|
|
|
|
use function str_replace;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function is_int;
|
|
|
|
|
use function substr;
|
|
|
|
|
use function array_merge;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
|
class CallAnalyzer
|
2016-11-01 19:14:35 +01:00
|
|
|
|
{
|
2017-02-10 02:35:17 +01:00
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
|
* @param FunctionLikeAnalyzer $source
|
2017-02-10 02:35:17 +01:00
|
|
|
|
* @param string $method_name
|
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2017-04-06 20:53:45 +02:00
|
|
|
|
* @return void
|
2017-02-10 02:35:17 +01:00
|
|
|
|
*/
|
|
|
|
|
public static function collectSpecialInformation(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
FunctionLikeAnalyzer $source,
|
2020-07-26 20:46:52 +02:00
|
|
|
|
string $method_name,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
Context $context
|
|
|
|
|
) {
|
|
|
|
|
$fq_class_name = (string)$source->getFQCLN();
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$project_analyzer = $source->getFileAnalyzer()->project_analyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase = $source->getCodebase();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if ($context->collect_mutations &&
|
|
|
|
|
$context->self &&
|
|
|
|
|
(
|
|
|
|
|
$context->self === $fq_class_name ||
|
2018-02-01 06:50:01 +01:00
|
|
|
|
$codebase->classExtends(
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$context->self,
|
|
|
|
|
$fq_class_name
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
strtolower($method_name)
|
|
|
|
|
);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ((string) $method_id !== $source->getId()) {
|
2018-01-24 19:38:53 +01:00
|
|
|
|
if ($context->collect_initializations) {
|
2020-02-18 18:53:54 +01:00
|
|
|
|
if (isset($context->initialized_methods[(string) $method_id])) {
|
2018-01-24 19:38:53 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($context->initialized_methods === null) {
|
|
|
|
|
$context->initialized_methods = [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:53:54 +01:00
|
|
|
|
$context->initialized_methods[(string) $method_id] = true;
|
2018-01-24 19:38:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 21:07:26 +01:00
|
|
|
|
$project_analyzer->getMethodMutations(
|
|
|
|
|
$method_id,
|
|
|
|
|
$context,
|
|
|
|
|
$source->getRootFilePath(),
|
|
|
|
|
$source->getRootFileName()
|
|
|
|
|
);
|
2017-12-23 01:01:59 +01:00
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
} elseif ($context->collect_initializations &&
|
|
|
|
|
$context->self &&
|
|
|
|
|
(
|
2019-03-20 00:43:12 +01:00
|
|
|
|
$context->self === $fq_class_name
|
|
|
|
|
|| $codebase->classlikes->classExtends(
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$context->self,
|
|
|
|
|
$fq_class_name
|
|
|
|
|
)
|
2017-02-22 06:51:34 +01:00
|
|
|
|
) &&
|
|
|
|
|
$source->getMethodName() !== $method_name
|
2017-02-10 02:35:17 +01:00
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, strtolower($method_name));
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2019-01-09 14:35:53 +01:00
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if (isset($context->vars_in_scope['$this'])) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($context->vars_in_scope['$this']->getAtomicTypes() as $atomic_type) {
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if ($atomic_type instanceof TNamedObject) {
|
|
|
|
|
if ($fq_class_name === $atomic_type->value) {
|
|
|
|
|
$alt_declaring_method_id = $declaring_method_id;
|
|
|
|
|
} else {
|
2019-01-09 14:35:53 +01:00
|
|
|
|
$fq_class_name = $atomic_type->value;
|
2019-03-01 23:30:55 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
strtolower($method_name)
|
|
|
|
|
);
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
$alt_declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
}
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if ($alt_declaring_method_id) {
|
|
|
|
|
$declaring_method_id = $alt_declaring_method_id;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if (!$atomic_type->extra_types) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
foreach ($atomic_type->extra_types as $intersection_type) {
|
|
|
|
|
if ($intersection_type instanceof TNamedObject) {
|
|
|
|
|
$fq_class_name = $intersection_type->value;
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
strtolower($method_name)
|
|
|
|
|
);
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
$alt_declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if ($alt_declaring_method_id) {
|
|
|
|
|
$declaring_method_id = $alt_declaring_method_id;
|
|
|
|
|
break 2;
|
2019-01-09 14:35:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-01 23:30:55 +01:00
|
|
|
|
}
|
2019-01-09 14:35:53 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if (!$declaring_method_id) {
|
|
|
|
|
// can happen for __call
|
|
|
|
|
return;
|
2019-01-09 14:35:53 +01:00
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if (isset($context->initialized_methods[(string) $declaring_method_id])) {
|
2018-01-24 19:11:23 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($context->initialized_methods === null) {
|
|
|
|
|
$context->initialized_methods = [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$context->initialized_methods[(string) $declaring_method_id] = true;
|
2018-01-24 19:11:23 +01:00
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$method_storage = $codebase->methods->getStorage($declaring_method_id);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$class_analyzer = $source->getSource();
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2020-07-26 20:46:52 +02:00
|
|
|
|
$is_final = $method_storage->final;
|
|
|
|
|
|
|
|
|
|
if ($method_name !== $declaring_method_id->method_name) {
|
|
|
|
|
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
|
|
|
|
|
|
|
|
|
|
if ($appearing_method_id) {
|
|
|
|
|
$appearing_class_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
|
$appearing_method_id->fq_class_name
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isset($appearing_class_storage->trait_final_map[strtolower($method_name)])) {
|
|
|
|
|
$is_final = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 06:04:47 +02:00
|
|
|
|
if ($class_analyzer instanceof ClassLikeAnalyzer
|
|
|
|
|
&& !$method_storage->is_static
|
|
|
|
|
&& ($context->collect_nonprivate_initializations
|
|
|
|
|
|| $method_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
|
2020-07-26 20:46:52 +02:00
|
|
|
|
|| $is_final)
|
2020-04-21 06:04:47 +02:00
|
|
|
|
) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$local_vars_in_scope = [];
|
|
|
|
|
$local_vars_possibly_in_scope = [];
|
|
|
|
|
|
2018-01-28 18:01:51 +01:00
|
|
|
|
foreach ($context->vars_in_scope as $var => $_) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
|
$local_vars_in_scope[$var] = $context->vars_in_scope[$var];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 18:01:51 +01:00
|
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $_) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
|
$local_vars_possibly_in_scope[$var] = $context->vars_possibly_in_scope[$var];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$old_calling_method_id = $context->calling_method_id;
|
2020-01-03 17:29:45 +01:00
|
|
|
|
|
2019-03-01 23:30:55 +01:00
|
|
|
|
if ($fq_class_name === $source->getFQCLN()) {
|
2020-07-26 21:21:05 +02:00
|
|
|
|
$class_analyzer->getMethodMutations($declaring_method_id->method_name, $context);
|
2019-03-01 23:30:55 +01:00
|
|
|
|
} else {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$declaring_fq_class_name = $declaring_method_id->fq_class_name;
|
2019-03-01 23:30:55 +01:00
|
|
|
|
|
|
|
|
|
$old_self = $context->self;
|
|
|
|
|
$context->self = $declaring_fq_class_name;
|
2019-03-02 21:07:26 +01:00
|
|
|
|
$project_analyzer->getMethodMutations(
|
|
|
|
|
$declaring_method_id,
|
|
|
|
|
$context,
|
|
|
|
|
$source->getRootFilePath(),
|
|
|
|
|
$source->getRootFileName()
|
|
|
|
|
);
|
2019-03-01 23:30:55 +01:00
|
|
|
|
$context->self = $old_self;
|
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id = $old_calling_method_id;
|
2020-01-03 17:29:45 +01:00
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
foreach ($local_vars_in_scope as $var => $type) {
|
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 18:43:19 +01:00
|
|
|
|
foreach ($local_vars_possibly_in_scope as $var => $_) {
|
2017-12-03 00:28:18 +01:00
|
|
|
|
$context->vars_possibly_in_scope[$var] = true;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
|
* @param Context $context
|
|
|
|
|
* @param CodeLocation $code_location
|
2020-02-15 02:54:26 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2017-02-10 02:35:17 +01:00
|
|
|
|
*/
|
|
|
|
|
protected static function checkMethodArgs(
|
2020-02-15 02:54:26 +01:00
|
|
|
|
?\Psalm\Internal\MethodIdentifier $method_id,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
array $args,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
?TemplateResult $class_template_result,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
Context $context,
|
|
|
|
|
CodeLocation $code_location,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer
|
2020-05-18 21:13:27 +02:00
|
|
|
|
) : bool {
|
2018-11-11 18:19:53 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2017-01-02 02:10:28 +01:00
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$method_params = $method_id
|
2019-03-29 18:26:13 +01:00
|
|
|
|
? $codebase->methods->getMethodParams($method_id, $statements_analyzer, $args, $context)
|
2017-02-10 02:35:17 +01:00
|
|
|
|
: null;
|
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
if (Call\ArgumentsAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$args,
|
|
|
|
|
$method_params,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
(string) $method_id,
|
2019-05-07 00:44:10 +02:00
|
|
|
|
$context,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$class_template_result
|
2017-02-10 02:35:17 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$method_id || $method_params === null) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
return true;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$fq_class_name = $method_id->fq_class_name;
|
|
|
|
|
$method_name = $method_id->method_name;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$fq_class_name = strtolower($codebase->classlikes->getUnAliasedName($fq_class_name));
|
2018-12-21 17:32:44 +01:00
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2017-11-02 20:07:39 +01:00
|
|
|
|
|
|
|
|
|
$method_storage = null;
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if (isset($class_storage->declaring_method_ids[$method_name])) {
|
|
|
|
|
$declaring_method_id = $class_storage->declaring_method_ids[$method_name];
|
2017-11-02 20:07:39 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$declaring_fq_class_name = $declaring_method_id->fq_class_name;
|
|
|
|
|
$declaring_method_name = $declaring_method_id->method_name;
|
2017-11-02 20:07:39 +01:00
|
|
|
|
|
|
|
|
|
if ($declaring_fq_class_name !== $fq_class_name) {
|
2018-11-11 18:19:53 +01:00
|
|
|
|
$declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_fq_class_name);
|
2017-11-02 20:07:39 +01:00
|
|
|
|
} else {
|
|
|
|
|
$declaring_class_storage = $class_storage;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if (!isset($declaring_class_storage->methods[$declaring_method_name])) {
|
2017-11-02 20:07:39 +01:00
|
|
|
|
throw new \UnexpectedValueException('Storage should not be empty here');
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_storage = $declaring_class_storage->methods[$declaring_method_name];
|
2018-06-22 07:13:49 +02:00
|
|
|
|
|
2019-03-25 16:25:43 +01:00
|
|
|
|
if ($declaring_class_storage->user_defined
|
|
|
|
|
&& !$method_storage->has_docblock_param_types
|
|
|
|
|
&& isset($declaring_class_storage->documenting_method_ids[$method_name])
|
|
|
|
|
) {
|
|
|
|
|
$documenting_method_id = $declaring_class_storage->documenting_method_ids[$method_name];
|
|
|
|
|
|
|
|
|
|
$documenting_method_storage = $codebase->methods->getStorage($documenting_method_id);
|
|
|
|
|
|
|
|
|
|
if ($documenting_method_storage->template_types) {
|
|
|
|
|
$method_storage = $documenting_method_storage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-29 00:50:29 +01:00
|
|
|
|
if (!$context->isSuppressingExceptions($statements_analyzer)) {
|
2019-03-29 00:43:14 +01:00
|
|
|
|
$context->mergeFunctionExceptions($method_storage, $code_location);
|
2018-06-22 07:13:49 +02:00
|
|
|
|
}
|
2017-11-02 20:07:39 +01:00
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
if (Call\ArgumentsAnalyzer::checkArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-02-27 17:07:44 +01:00
|
|
|
|
$args,
|
|
|
|
|
$method_id,
|
|
|
|
|
$method_params,
|
|
|
|
|
$method_storage,
|
|
|
|
|
$class_storage,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$class_template_result,
|
2017-02-27 17:07:44 +01:00
|
|
|
|
$code_location,
|
2017-09-03 00:15:52 +02:00
|
|
|
|
$context
|
2017-02-27 17:07:44 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
if ($class_template_result) {
|
|
|
|
|
self::checkTemplateResult(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$class_template_result,
|
|
|
|
|
$code_location,
|
|
|
|
|
strtolower((string) $method_id)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
return true;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-19 04:57:00 +02:00
|
|
|
|
* @return array<string, array<string, array{Type\Union}>>
|
|
|
|
|
* @param array<string, non-empty-array<string, array{Type\Union}>> $existing_template_types
|
|
|
|
|
*/
|
|
|
|
|
public static function getTemplateTypesForCall(
|
2020-07-05 18:05:25 +02:00
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
|
?ClassLikeStorage $declaring_class_storage,
|
|
|
|
|
?string $appearing_class_name,
|
|
|
|
|
?ClassLikeStorage $calling_class_storage,
|
2020-05-19 04:57:00 +02:00
|
|
|
|
array $existing_template_types = []
|
|
|
|
|
) : array {
|
|
|
|
|
$template_types = $existing_template_types;
|
2020-02-15 02:54:26 +01:00
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
if ($declaring_class_storage) {
|
|
|
|
|
if ($calling_class_storage
|
|
|
|
|
&& $declaring_class_storage !== $calling_class_storage
|
|
|
|
|
&& $calling_class_storage->template_type_extends
|
|
|
|
|
) {
|
|
|
|
|
foreach ($calling_class_storage->template_type_extends as $class_name => $type_map) {
|
|
|
|
|
foreach ($type_map as $template_name => $type) {
|
|
|
|
|
if (is_string($template_name) && $class_name === $declaring_class_storage->name) {
|
|
|
|
|
$output_type = null;
|
2018-03-19 01:29:41 +01:00
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
foreach ($type->getAtomicTypes() as $atomic_type) {
|
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TTemplateParam
|
|
|
|
|
&& isset(
|
|
|
|
|
$calling_class_storage
|
|
|
|
|
->template_type_extends
|
|
|
|
|
[$atomic_type->defining_class]
|
|
|
|
|
[$atomic_type->param_name]
|
|
|
|
|
)
|
2018-03-21 05:13:57 +01:00
|
|
|
|
) {
|
2020-05-19 04:57:00 +02:00
|
|
|
|
$output_type_candidate = $calling_class_storage
|
|
|
|
|
->template_type_extends
|
|
|
|
|
[$atomic_type->defining_class]
|
|
|
|
|
[$atomic_type->param_name];
|
|
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
|
$output_type_candidate = $atomic_type->as;
|
2018-03-19 01:29:41 +01:00
|
|
|
|
} else {
|
2020-05-19 04:57:00 +02:00
|
|
|
|
$output_type_candidate = new Type\Union([$atomic_type]);
|
2018-03-19 01:29:41 +01:00
|
|
|
|
}
|
2018-03-02 05:43:52 +01:00
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
if (!$output_type) {
|
|
|
|
|
$output_type = $output_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$output_type = Type::combineUnionTypes(
|
|
|
|
|
$output_type_candidate,
|
|
|
|
|
$output_type
|
|
|
|
|
);
|
2018-03-19 01:29:41 +01:00
|
|
|
|
}
|
2017-08-12 00:48:58 +02:00
|
|
|
|
}
|
2019-08-06 16:33:21 +02:00
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
$template_types[$template_name][$declaring_class_storage->name] = [$output_type];
|
2019-08-06 16:33:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-19 04:57:00 +02:00
|
|
|
|
} elseif ($declaring_class_storage->template_types) {
|
|
|
|
|
foreach ($declaring_class_storage->template_types as $template_name => $type_map) {
|
|
|
|
|
foreach ($type_map as $key => list($type)) {
|
|
|
|
|
$template_types[$template_name][$key] = [$type];
|
2019-08-14 06:47:57 +02:00
|
|
|
|
}
|
2019-08-06 16:33:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 17:51:41 +02:00
|
|
|
|
}
|
2018-02-08 19:01:39 +01:00
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
foreach ($template_types as $key => $type_map) {
|
|
|
|
|
foreach ($type_map as $class => $type) {
|
2020-07-05 18:05:25 +02:00
|
|
|
|
$template_types[$key][$class][0] = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
|
|
|
|
$codebase,
|
|
|
|
|
$type[0],
|
|
|
|
|
$appearing_class_name,
|
|
|
|
|
$calling_class_storage ? $calling_class_storage->name : null,
|
|
|
|
|
null,
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
$calling_class_storage ? $calling_class_storage->final : false
|
|
|
|
|
);
|
2019-05-21 18:11:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
|
return $template_types;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
|
/**
|
2018-10-17 19:22:57 +02:00
|
|
|
|
* @param PhpParser\Node\Scalar\String_|PhpParser\Node\Expr\Array_|PhpParser\Node\Expr\BinaryOp\Concat
|
|
|
|
|
* $callable_arg
|
2017-08-15 01:30:11 +02:00
|
|
|
|
*
|
2020-05-15 16:18:05 +02:00
|
|
|
|
* @return non-empty-string[]
|
|
|
|
|
*
|
|
|
|
|
* @psalm-suppress LessSpecificReturnStatement
|
|
|
|
|
* @psalm-suppress MoreSpecificReturnType
|
2017-08-15 01:30:11 +02:00
|
|
|
|
*/
|
|
|
|
|
public static function getFunctionIdsFromCallableArg(
|
2018-03-02 05:33:21 +01:00
|
|
|
|
\Psalm\FileSource $file_source,
|
2017-08-15 01:30:11 +02:00
|
|
|
|
$callable_arg
|
|
|
|
|
) {
|
2018-10-17 19:22:57 +02:00
|
|
|
|
if ($callable_arg instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
|
|
|
|
if ($callable_arg->left instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $callable_arg->left->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $callable_arg->left->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($callable_arg->left->name->name) === 'class'
|
|
|
|
|
&& !in_array(strtolower($callable_arg->left->class->parts[0]), ['self', 'static', 'parent'])
|
|
|
|
|
&& $callable_arg->right instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
&& preg_match('/^::[A-Za-z0-9]+$/', $callable_arg->right->value)
|
|
|
|
|
) {
|
2018-10-17 20:37:32 +02:00
|
|
|
|
return [
|
|
|
|
|
(string) $callable_arg->left->class->getAttribute('resolvedName') . $callable_arg->right->value
|
|
|
|
|
];
|
2018-10-17 19:22:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
|
if ($callable_arg instanceof PhpParser\Node\Scalar\String_) {
|
2019-08-12 21:04:43 +02:00
|
|
|
|
$potential_id = preg_replace('/^\\\/', '', $callable_arg->value);
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^[A-Za-z0-9_]+(\\\[A-Za-z0-9_]+)*(::[A-Za-z0-9_]+)?$/', $potential_id)) {
|
|
|
|
|
return [$potential_id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
2017-08-15 01:30:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count($callable_arg->items) !== 2) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-24 22:22:50 +01:00
|
|
|
|
/** @psalm-suppress PossiblyNullPropertyFetch */
|
2020-02-24 22:19:33 +01:00
|
|
|
|
if ($callable_arg->items[0]->key || $callable_arg->items[1]->key) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-14 00:33:32 +01:00
|
|
|
|
if (!isset($callable_arg->items[0]) || !isset($callable_arg->items[1])) {
|
|
|
|
|
throw new \UnexpectedValueException('These should never be unset');
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
|
$class_arg = $callable_arg->items[0]->value;
|
|
|
|
|
$method_name_arg = $callable_arg->items[1]->value;
|
|
|
|
|
|
|
|
|
|
if (!$method_name_arg instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($class_arg instanceof PhpParser\Node\Scalar\String_) {
|
2017-08-30 19:45:41 +02:00
|
|
|
|
return [preg_replace('/^\\\/', '', $class_arg->value) . '::' . $method_name_arg->value];
|
2017-08-15 01:30:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($class_arg instanceof PhpParser\Node\Expr\ClassConstFetch
|
2018-04-17 18:16:25 +02:00
|
|
|
|
&& $class_arg->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($class_arg->name->name) === 'class'
|
2017-08-15 01:30:11 +02:00
|
|
|
|
&& $class_arg->class instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2017-08-15 01:30:11 +02:00
|
|
|
|
$class_arg->class,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$file_source->getAliases()
|
2017-08-15 01:30:11 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [$fq_class_name . '::' . $method_name_arg->value];
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$class_arg_type = null;
|
|
|
|
|
|
|
|
|
|
if (!$file_source instanceof StatementsAnalyzer
|
|
|
|
|
|| !($class_arg_type = $file_source->node_data->getType($class_arg))
|
|
|
|
|
) {
|
2017-08-15 01:30:11 +02:00
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$method_ids = [];
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($class_arg_type->getAtomicTypes() as $type_part) {
|
2017-08-15 01:30:11 +02:00
|
|
|
|
if ($type_part instanceof TNamedObject) {
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$method_id = $type_part->value . '::' . $method_name_arg->value;
|
|
|
|
|
|
|
|
|
|
if ($type_part->extra_types) {
|
|
|
|
|
foreach ($type_part->extra_types as $extra_type) {
|
2019-06-19 18:00:07 +02:00
|
|
|
|
if ($extra_type instanceof Type\Atomic\TTemplateParam
|
|
|
|
|
|| $extra_type instanceof Type\Atomic\TObjectWithProperties
|
|
|
|
|
) {
|
2018-11-02 18:08:56 +01:00
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get a generic param here');
|
|
|
|
|
}
|
2019-06-19 18:00:07 +02:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$method_id .= '&' . $extra_type->value . '::' . $method_name_arg->value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 19:05:34 +02:00
|
|
|
|
$method_ids[] = '$' . $method_id;
|
2017-08-15 01:30:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $method_ids;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
/**
|
2020-05-15 16:18:05 +02:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
|
|
|
|
* @param non-empty-string $function_id
|
2016-12-04 01:11:30 +01:00
|
|
|
|
* @param CodeLocation $code_location
|
2018-02-25 17:13:00 +01:00
|
|
|
|
* @param bool $can_be_in_root_scope if true, the function can be shortened to the root version
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-11-01 19:14:35 +01:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-05-19 04:57:00 +02:00
|
|
|
|
public static function checkFunctionExists(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2017-01-15 18:34:23 +01:00
|
|
|
|
&$function_id,
|
2018-02-25 17:13:00 +01:00
|
|
|
|
CodeLocation $code_location,
|
|
|
|
|
$can_be_in_root_scope
|
2016-11-02 07:29:00 +01:00
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
|
$cased_function_id = $function_id;
|
|
|
|
|
$function_id = strtolower($function_id);
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (!$codebase->functions->functionExists($statements_analyzer, $function_id)) {
|
2020-05-15 16:18:05 +02:00
|
|
|
|
/** @var non-empty-lowercase-string */
|
2017-01-15 18:34:23 +01:00
|
|
|
|
$root_function_id = preg_replace('/.*\\\/', '', $function_id);
|
|
|
|
|
|
2018-02-25 17:13:00 +01:00
|
|
|
|
if ($can_be_in_root_scope
|
|
|
|
|
&& $function_id !== $root_function_id
|
2018-11-11 18:01:14 +01:00
|
|
|
|
&& $codebase->functions->functionExists($statements_analyzer, $root_function_id)
|
2017-01-15 18:34:23 +01:00
|
|
|
|
) {
|
|
|
|
|
$function_id = $root_function_id;
|
|
|
|
|
} else {
|
2017-08-15 01:30:11 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UndefinedFunction(
|
|
|
|
|
'Function ' . $cased_function_id . ' does not exist',
|
2018-11-30 05:19:22 +01:00
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
2017-08-15 01:30:11 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-08-15 01:30:11 +02:00
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
2017-08-12 01:05:04 +02:00
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-02-21 17:32:52 +01:00
|
|
|
|
|
2018-02-23 21:39:33 +01:00
|
|
|
|
/**
|
2018-11-14 18:25:17 +01:00
|
|
|
|
* @param PhpParser\Node\Identifier|PhpParser\Node\Name $expr
|
2018-02-23 21:39:33 +01:00
|
|
|
|
* @param \Psalm\Storage\Assertion[] $assertions
|
2019-09-06 03:00:02 +02:00
|
|
|
|
* @param string $thisName
|
2018-02-23 21:39:33 +01:00
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
|
* @param Context $context
|
2019-03-22 20:59:10 +01:00
|
|
|
|
* @param array<string, array<string, array{Type\Union}>> $template_type_map,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-02-23 21:39:33 +01:00
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function applyAssertionsToContext(
|
2018-11-14 18:25:17 +01:00
|
|
|
|
$expr,
|
2019-09-06 03:00:02 +02:00
|
|
|
|
?string $thisName,
|
2018-02-23 21:39:33 +01:00
|
|
|
|
array $assertions,
|
|
|
|
|
array $args,
|
2019-01-23 05:42:54 +01:00
|
|
|
|
array $template_type_map,
|
2018-02-23 21:39:33 +01:00
|
|
|
|
Context $context,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer
|
2018-02-23 21:39:33 +01:00
|
|
|
|
) {
|
|
|
|
|
$type_assertions = [];
|
|
|
|
|
|
2018-11-14 19:12:31 +01:00
|
|
|
|
$asserted_keys = [];
|
|
|
|
|
|
2018-02-23 21:39:33 +01:00
|
|
|
|
foreach ($assertions as $assertion) {
|
2018-10-30 14:20:34 +01:00
|
|
|
|
$assertion_var_id = null;
|
|
|
|
|
|
2019-01-05 20:50:11 +01:00
|
|
|
|
$arg_value = null;
|
|
|
|
|
|
2018-02-23 21:39:33 +01:00
|
|
|
|
if (is_int($assertion->var_id)) {
|
|
|
|
|
if (!isset($args[$assertion->var_id])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$arg_value = $args[$assertion->var_id]->value;
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$arg_var_id = ExpressionIdentifier::getArrayVarId($arg_value, null, $statements_analyzer);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
|
|
|
|
if ($arg_var_id) {
|
2018-10-30 14:20:34 +01:00
|
|
|
|
$assertion_var_id = $arg_var_id;
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
2020-04-09 14:15:07 +02:00
|
|
|
|
} elseif ($assertion->var_id === '$this' && !is_null($thisName)) {
|
|
|
|
|
$assertion_var_id = $thisName;
|
2019-09-06 03:00:02 +02:00
|
|
|
|
} elseif (strpos($assertion->var_id, '$this->') === 0 && !is_null($thisName)) {
|
|
|
|
|
$assertion_var_id = $thisName . str_replace('$this->', '->', $assertion->var_id);
|
2020-04-10 05:03:53 +02:00
|
|
|
|
} elseif (isset($context->vars_in_scope[$assertion->var_id])) {
|
|
|
|
|
$assertion_var_id = $assertion->var_id;
|
2018-10-30 14:20:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($assertion_var_id) {
|
2018-11-16 00:50:08 +01:00
|
|
|
|
$rule = $assertion->rule[0][0];
|
2018-10-30 14:20:34 +01:00
|
|
|
|
|
2018-11-16 00:50:08 +01:00
|
|
|
|
$prefix = '';
|
|
|
|
|
if ($rule[0] === '!') {
|
|
|
|
|
$prefix .= '!';
|
|
|
|
|
$rule = substr($rule, 1);
|
|
|
|
|
}
|
2018-11-16 16:13:52 +01:00
|
|
|
|
if ($rule[0] === '=') {
|
|
|
|
|
$prefix .= '=';
|
2018-11-16 00:50:08 +01:00
|
|
|
|
$rule = substr($rule, 1);
|
|
|
|
|
}
|
|
|
|
|
if ($rule[0] === '~') {
|
|
|
|
|
$prefix .= '~';
|
|
|
|
|
$rule = substr($rule, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 07:40:19 +02:00
|
|
|
|
if (isset($template_type_map[$rule])) {
|
|
|
|
|
foreach ($template_type_map[$rule] as $template_map) {
|
|
|
|
|
if ($template_map[0]->hasMixed()) {
|
|
|
|
|
continue 2;
|
|
|
|
|
}
|
2018-11-16 06:56:57 +01:00
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$replacement_atomic_types = $template_map[0]->getAtomicTypes();
|
2018-11-16 00:50:08 +01:00
|
|
|
|
|
2019-07-21 07:40:19 +02:00
|
|
|
|
if (count($replacement_atomic_types) > 1) {
|
2018-11-16 00:50:08 +01:00
|
|
|
|
continue 2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 07:40:19 +02:00
|
|
|
|
$ored_type_assertions = [];
|
|
|
|
|
|
|
|
|
|
foreach ($replacement_atomic_types as $replacement_atomic_type) {
|
|
|
|
|
if ($replacement_atomic_type instanceof Type\Atomic\TMixed) {
|
|
|
|
|
continue 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($replacement_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
|
|| $replacement_atomic_type instanceof Type\Atomic\ObjectLike
|
|
|
|
|
) {
|
|
|
|
|
$ored_type_assertions[] = $prefix . 'array';
|
|
|
|
|
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
|
$ored_type_assertions[] = $prefix . $replacement_atomic_type->value;
|
|
|
|
|
} elseif ($replacement_atomic_type instanceof Type\Atomic\Scalar) {
|
|
|
|
|
$ored_type_assertions[] = $prefix . $replacement_atomic_type->getId();
|
|
|
|
|
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNull) {
|
|
|
|
|
$ored_type_assertions[] = $prefix . 'null';
|
|
|
|
|
} elseif ($replacement_atomic_type instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
|
$ored_type_assertions[] = $prefix . $replacement_atomic_type->param_name;
|
|
|
|
|
}
|
2018-10-30 14:20:34 +01:00
|
|
|
|
}
|
2018-11-16 00:50:08 +01:00
|
|
|
|
|
2019-07-21 07:40:19 +02:00
|
|
|
|
if ($ored_type_assertions) {
|
|
|
|
|
$type_assertions[$assertion_var_id] = [$ored_type_assertions];
|
|
|
|
|
}
|
2018-11-16 17:50:07 +01:00
|
|
|
|
}
|
2018-10-30 14:20:34 +01:00
|
|
|
|
} else {
|
2019-12-09 23:21:58 +01:00
|
|
|
|
if (isset($type_assertions[$assertion_var_id])) {
|
|
|
|
|
$type_assertions[$assertion_var_id] = array_merge(
|
|
|
|
|
$type_assertions[$assertion_var_id],
|
|
|
|
|
$assertion->rule
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$type_assertions[$assertion_var_id] = $assertion->rule;
|
|
|
|
|
}
|
2018-10-30 14:20:34 +01:00
|
|
|
|
}
|
2019-08-29 17:53:36 +02:00
|
|
|
|
} elseif ($arg_value && ($assertion->rule === [['!falsy']] || $assertion->rule === [['true']])) {
|
|
|
|
|
if ($assertion->rule === [['true']]) {
|
2019-12-08 22:35:56 +01:00
|
|
|
|
$conditional = new PhpParser\Node\Expr\BinaryOp\Identical(
|
|
|
|
|
$arg_value,
|
|
|
|
|
new PhpParser\Node\Expr\ConstFetch(new PhpParser\Node\Name('true'))
|
|
|
|
|
);
|
|
|
|
|
|
2019-08-29 17:53:36 +02:00
|
|
|
|
$assert_clauses = \Psalm\Type\Algebra::getFormula(
|
2020-08-26 21:35:29 +02:00
|
|
|
|
\mt_rand(0, 1000000),
|
|
|
|
|
\mt_rand(0, 1000000),
|
2019-12-08 22:35:56 +01:00
|
|
|
|
$conditional,
|
2019-12-28 21:56:19 +01:00
|
|
|
|
$context->self,
|
2019-08-29 17:53:36 +02:00
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$statements_analyzer->getCodebase()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$assert_clauses = \Psalm\Type\Algebra::getFormula(
|
2020-08-26 21:35:29 +02:00
|
|
|
|
\spl_object_id($arg_value),
|
2019-12-08 22:35:56 +01:00
|
|
|
|
\spl_object_id($arg_value),
|
2019-08-29 17:53:36 +02:00
|
|
|
|
$arg_value,
|
2019-12-28 21:56:19 +01:00
|
|
|
|
$context->self,
|
2019-08-29 17:53:36 +02:00
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$statements_analyzer->getCodebase()
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-05 20:50:11 +01:00
|
|
|
|
|
|
|
|
|
$simplified_clauses = \Psalm\Type\Algebra::simplifyCNF(
|
|
|
|
|
array_merge($context->clauses, $assert_clauses)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$assert_type_assertions = \Psalm\Type\Algebra::getTruthsFromFormula(
|
|
|
|
|
$simplified_clauses
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-21 18:55:11 +02:00
|
|
|
|
$type_assertions = array_merge($type_assertions, $assert_type_assertions);
|
|
|
|
|
} elseif ($arg_value && $assertion->rule === [['falsy']]) {
|
|
|
|
|
$assert_clauses = \Psalm\Type\Algebra::negateFormula(
|
|
|
|
|
\Psalm\Type\Algebra::getFormula(
|
2020-08-26 21:35:29 +02:00
|
|
|
|
\spl_object_id($arg_value),
|
2020-07-21 18:55:11 +02:00
|
|
|
|
\spl_object_id($arg_value),
|
|
|
|
|
$arg_value,
|
|
|
|
|
$context->self,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$statements_analyzer->getCodebase()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$simplified_clauses = \Psalm\Type\Algebra::simplifyCNF(
|
|
|
|
|
array_merge($context->clauses, $assert_clauses)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$assert_type_assertions = \Psalm\Type\Algebra::getTruthsFromFormula(
|
|
|
|
|
$simplified_clauses
|
|
|
|
|
);
|
|
|
|
|
|
2019-01-05 20:50:11 +01:00
|
|
|
|
$type_assertions = array_merge($type_assertions, $assert_type_assertions);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 07:23:35 +01:00
|
|
|
|
$changed_var_ids = [];
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
2018-11-14 18:25:17 +01:00
|
|
|
|
foreach ($type_assertions as $var_id => $_) {
|
|
|
|
|
$asserted_keys[$var_id] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
|
if ($type_assertions) {
|
2020-05-31 01:11:41 +02:00
|
|
|
|
foreach (($statements_analyzer->getTemplateTypeMap() ?: []) as $template_name => $map) {
|
|
|
|
|
foreach ($map as $ref => list($type)) {
|
|
|
|
|
$template_type_map[$template_name][$ref] = [
|
|
|
|
|
new Type\Union([
|
|
|
|
|
new Type\Atomic\TTemplateParam(
|
|
|
|
|
$template_name,
|
|
|
|
|
$type,
|
|
|
|
|
$ref
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
|
// while in an and, we allow scope to boil over to support
|
|
|
|
|
// statements of the form if ($x && $x->foo())
|
|
|
|
|
$op_vars_in_scope = \Psalm\Type\Reconciler::reconcileKeyedTypes(
|
2019-12-08 22:35:56 +01:00
|
|
|
|
$type_assertions,
|
2018-12-17 21:23:56 +01:00
|
|
|
|
$type_assertions,
|
|
|
|
|
$context->vars_in_scope,
|
2019-12-07 07:23:35 +01:00
|
|
|
|
$changed_var_ids,
|
2018-12-17 21:23:56 +01:00
|
|
|
|
$asserted_keys,
|
|
|
|
|
$statements_analyzer,
|
2020-05-31 01:11:41 +02:00
|
|
|
|
$template_type_map,
|
2018-12-17 21:23:56 +01:00
|
|
|
|
$context->inside_loop,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $expr)
|
|
|
|
|
);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
2019-12-07 07:23:35 +01:00
|
|
|
|
foreach ($changed_var_ids as $var_id => $_) {
|
|
|
|
|
if (isset($op_vars_in_scope[$var_id])) {
|
2020-05-02 20:55:21 +02:00
|
|
|
|
$first_appearance = $statements_analyzer->getFirstAppearance($var_id);
|
|
|
|
|
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
2020-05-11 03:09:48 +02:00
|
|
|
|
if ($first_appearance
|
|
|
|
|
&& isset($context->vars_in_scope[$var_id])
|
|
|
|
|
&& $context->vars_in_scope[$var_id]->hasMixed()
|
|
|
|
|
) {
|
2020-05-02 20:55:21 +02:00
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
|
) {
|
|
|
|
|
$codebase->analyzer->decrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 05:01:45 +01:00
|
|
|
|
IssueBuffer::remove(
|
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
|
'MixedAssignment',
|
|
|
|
|
$first_appearance->raw_file_start
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 07:23:35 +01:00
|
|
|
|
$op_vars_in_scope[$var_id]->from_docblock = true;
|
2019-05-16 00:41:26 +02:00
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($op_vars_in_scope[$var_id]->getAtomicTypes() as $changed_atomic_type) {
|
2019-05-16 00:41:26 +02:00
|
|
|
|
$changed_atomic_type->from_docblock = true;
|
|
|
|
|
|
|
|
|
|
if ($changed_atomic_type instanceof Type\Atomic\TNamedObject
|
|
|
|
|
&& $changed_atomic_type->extra_types
|
|
|
|
|
) {
|
|
|
|
|
foreach ($changed_atomic_type->extra_types as $extra_type) {
|
|
|
|
|
$extra_type->from_docblock = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-17 21:23:56 +01:00
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
|
$context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
2020-04-07 06:13:56 +02:00
|
|
|
|
|
|
|
|
|
public static function checkTemplateResult(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
TemplateResult $template_result,
|
|
|
|
|
CodeLocation $code_location,
|
|
|
|
|
?string $function_id
|
|
|
|
|
) : void {
|
|
|
|
|
if ($template_result->upper_bounds && $template_result->lower_bounds) {
|
|
|
|
|
foreach ($template_result->lower_bounds as $template_name => $defining_map) {
|
|
|
|
|
foreach ($defining_map as $defining_id => list($lower_bound_type)) {
|
|
|
|
|
if (isset($template_result->upper_bounds[$template_name][$defining_id])) {
|
|
|
|
|
$upper_bound_type = $template_result->upper_bounds[$template_name][$defining_id][0];
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
$union_comparison_result = new \Psalm\Internal\Type\Comparator\TypeComparisonResult();
|
2020-04-08 18:37:38 +02:00
|
|
|
|
|
2020-04-29 20:57:57 +02:00
|
|
|
|
if (count($template_result->lower_bounds_unintersectable_types) > 1) {
|
|
|
|
|
$upper_bound_type = $template_result->lower_bounds_unintersectable_types[0];
|
|
|
|
|
$lower_bound_type = $template_result->lower_bounds_unintersectable_types[1];
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$statements_analyzer->getCodebase(),
|
|
|
|
|
$upper_bound_type,
|
2020-04-08 18:37:38 +02:00
|
|
|
|
$lower_bound_type,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
$union_comparison_result
|
2020-04-07 06:13:56 +02:00
|
|
|
|
)) {
|
2020-04-08 18:37:38 +02:00
|
|
|
|
if ($union_comparison_result->type_coerced) {
|
|
|
|
|
if ($union_comparison_result->type_coerced_from_mixed) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgumentTypeCoercion(
|
2020-05-24 15:42:46 +02:00
|
|
|
|
'Type ' . $upper_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $lower_bound_type->getId(),
|
2020-04-08 18:37:38 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new ArgumentTypeCoercion(
|
2020-05-24 15:42:46 +02:00
|
|
|
|
'Type ' . $upper_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $lower_bound_type->getId(),
|
2020-04-08 18:37:38 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($union_comparison_result->scalar_type_match_found) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidScalarArgument(
|
2020-05-24 15:42:46 +02:00
|
|
|
|
'Type ' . $upper_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $lower_bound_type->getId(),
|
2020-04-08 18:37:38 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidArgument(
|
2020-05-24 15:42:46 +02:00
|
|
|
|
'Type ' . $upper_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $lower_bound_type->getId(),
|
2020-04-08 18:37:38 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
2020-04-07 06:13:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$template_result->upper_bounds[$template_name][$defining_id][0] = clone $lower_bound_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|