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;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
use Psalm\Codebase;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\MethodAnalyzer;
|
2020-02-18 01:54:11 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2018-11-12 16:46:55 +01:00
|
|
|
|
use Psalm\Internal\Codebase\CallMap;
|
2020-03-15 18:44:00 +01:00
|
|
|
|
use Psalm\Internal\MethodIdentifier;
|
2019-08-14 06:47:57 +02:00
|
|
|
|
use Psalm\Internal\Taint\Sink;
|
|
|
|
|
use Psalm\Internal\Taint\Source;
|
2018-11-18 18:41:47 +01:00
|
|
|
|
use Psalm\Internal\Type\TypeCombination;
|
2019-11-04 03:27:40 +01:00
|
|
|
|
use Psalm\Internal\Type\TemplateResult;
|
|
|
|
|
use Psalm\Internal\Type\UnionTemplateHandler;
|
2016-12-04 01:11:30 +01:00
|
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use Psalm\Context;
|
2016-12-29 06:14:06 +01:00
|
|
|
|
use Psalm\Issue\ImplicitToStringCast;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\InvalidArgument;
|
2017-09-16 19:16:21 +02:00
|
|
|
|
use Psalm\Issue\InvalidPassByReference;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\InvalidScalarArgument;
|
|
|
|
|
use Psalm\Issue\MixedArgument;
|
2019-04-26 00:02:19 +02:00
|
|
|
|
use Psalm\Issue\MixedArgumentTypeCoercion;
|
2019-01-02 23:05:39 +01:00
|
|
|
|
use Psalm\Issue\NoValue;
|
2016-12-14 18:54:34 +01:00
|
|
|
|
use Psalm\Issue\NullArgument;
|
2017-10-23 17:47:00 +02:00
|
|
|
|
use Psalm\Issue\PossiblyFalseArgument;
|
2017-04-08 15:28:02 +02:00
|
|
|
|
use Psalm\Issue\PossiblyInvalidArgument;
|
2017-02-11 23:55:08 +01:00
|
|
|
|
use Psalm\Issue\PossiblyNullArgument;
|
2020-03-06 03:24:08 +01:00
|
|
|
|
use Psalm\Issue\PossiblyUndefinedVariable;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\TooFewArguments;
|
|
|
|
|
use Psalm\Issue\TooManyArguments;
|
2019-04-26 00:02:19 +02:00
|
|
|
|
use Psalm\Issue\ArgumentTypeCoercion;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\UndefinedFunction;
|
2019-07-24 23:24:23 +02:00
|
|
|
|
use Psalm\Issue\UndefinedVariable;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\IssueBuffer;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2018-05-12 00:35:02 +02:00
|
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
2018-03-05 23:10:52 +01:00
|
|
|
|
use Psalm\Type\Atomic\TClassString;
|
2017-01-15 01:06:58 +01:00
|
|
|
|
use Psalm\Type\Atomic\TCallable;
|
2018-11-09 16:56:27 +01:00
|
|
|
|
use Psalm\Type\Atomic\TEmpty;
|
2019-10-09 00:44:46 +02:00
|
|
|
|
use Psalm\Type\Atomic\TList;
|
2017-05-19 06:48:26 +02:00
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2018-11-09 16:56:27 +01:00
|
|
|
|
use Psalm\Type\Atomic\TNonEmptyArray;
|
2019-10-09 00:44:46 +02:00
|
|
|
|
use Psalm\Type\Atomic\TNonEmptyList;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function strtolower;
|
|
|
|
|
use function strpos;
|
|
|
|
|
use function explode;
|
|
|
|
|
use function count;
|
|
|
|
|
use function in_array;
|
|
|
|
|
use function array_reverse;
|
|
|
|
|
use function array_filter;
|
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 assert;
|
|
|
|
|
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;
|
2019-08-04 16:37:36 +02:00
|
|
|
|
use Psalm\Issue\TaintedInput;
|
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,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$method_name,
|
|
|
|
|
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-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
|
|
|
|
|
|| $method_storage->final)
|
|
|
|
|
) {
|
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()) {
|
|
|
|
|
$class_analyzer->getMethodMutations(strtolower($method_name), $context);
|
|
|
|
|
} 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-05-27 02:16:18 +02:00
|
|
|
|
*
|
2017-02-10 02:35:17 +01:00
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
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
|
2017-02-10 02:35:17 +01:00
|
|
|
|
) {
|
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;
|
|
|
|
|
|
|
|
|
|
if (self::checkFunctionArguments(
|
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) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2018-02-02 17:26:55 +01:00
|
|
|
|
if (self::checkFunctionLikeArgumentsMatch(
|
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)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-12-31 16:51:42 +01:00
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2017-01-01 23:39:39 +01:00
|
|
|
|
* @param array<int, FunctionLikeParameter>|null $function_params
|
2019-05-07 00:44:10 +02:00
|
|
|
|
* @param array<string, array<string, array{Type\Union, 1?:int}>>|null $generic_params
|
2017-10-28 19:56:29 +02:00
|
|
|
|
* @param string|null $method_id
|
2016-12-31 16:51:42 +01:00
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @return false|null
|
2016-11-01 19:14:35 +01:00
|
|
|
|
*/
|
2020-03-12 04:04:52 +01:00
|
|
|
|
public static function checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
array $args,
|
2019-08-23 16:59:59 +02:00
|
|
|
|
?array $function_params,
|
|
|
|
|
?string $method_id,
|
2019-05-07 00:44:10 +02:00
|
|
|
|
Context $context,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
?TemplateResult $template_result = null
|
2016-11-02 07:29:00 +01:00
|
|
|
|
) {
|
2017-01-16 18:39:38 +01:00
|
|
|
|
$last_param = $function_params
|
|
|
|
|
? $function_params[count($function_params) - 1]
|
|
|
|
|
: null;
|
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
// if this modifies the array type based on further args
|
2019-10-01 14:46:37 +02:00
|
|
|
|
if ($method_id
|
|
|
|
|
&& in_array($method_id, ['array_push', 'array_unshift'], true)
|
2019-10-01 14:45:36 +02:00
|
|
|
|
&& $function_params
|
2019-10-01 15:13:00 +02:00
|
|
|
|
&& isset($args[0])
|
|
|
|
|
&& isset($args[1])
|
2019-10-01 14:45:36 +02:00
|
|
|
|
) {
|
2019-12-29 16:29:03 +01:00
|
|
|
|
if (self::handleArrayAddition(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
$context,
|
|
|
|
|
$method_id === 'array_push'
|
|
|
|
|
) === false
|
|
|
|
|
) {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-01-16 18:39:38 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($method_id && $method_id === 'array_splice' && $function_params && count($args) > 1) {
|
|
|
|
|
if (self::handleArraySplice($statements_analyzer, $args, $context) === false) {
|
2017-10-28 19:56:29 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-02 17:18:56 +02:00
|
|
|
|
|
2020-02-08 18:17:57 +01:00
|
|
|
|
if ($method_id === 'array_map') {
|
2019-05-07 22:27:25 +02:00
|
|
|
|
$args = array_reverse($args, true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
|
if ($function_params !== null) {
|
2019-05-07 00:44:10 +02:00
|
|
|
|
$param = $argument_offset < count($function_params)
|
|
|
|
|
? $function_params[$argument_offset]
|
|
|
|
|
: ($last_param && $last_param->is_variadic ? $last_param : null);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2019-05-07 00:44:10 +02:00
|
|
|
|
$by_ref = $param && $param->by_ref;
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-05-07 00:44:10 +02:00
|
|
|
|
$by_ref_type = null;
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-05-07 00:44:10 +02:00
|
|
|
|
if ($by_ref && $param) {
|
|
|
|
|
$by_ref_type = $param->type ? clone $param->type : Type::getMixed();
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($by_ref
|
|
|
|
|
&& $by_ref_type
|
|
|
|
|
&& !($arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ConstFetch
|
2019-06-07 19:23:52 +02:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
|
2019-04-18 19:51:34 +02:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\StaticCall
|
2019-11-18 22:14:21 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\New_
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Assign
|
2019-03-28 03:13:06 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Array_
|
2020-03-05 04:30:53 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Ternary
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\BinaryOp
|
2019-02-03 22:21:37 +01:00
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
if (self::handleByRefFunctionArg(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$arg,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 05:52:27 +01:00
|
|
|
|
continue;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-04-20 23:14:38 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$toggled_class_exists = false;
|
2018-04-20 23:14:38 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($method_id === 'class_exists'
|
|
|
|
|
&& $argument_offset === 0
|
|
|
|
|
&& !$context->inside_class_exists
|
|
|
|
|
) {
|
|
|
|
|
$context->inside_class_exists = true;
|
|
|
|
|
$toggled_class_exists = true;
|
2017-10-28 19:56:29 +02:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2019-05-07 22:27:25 +02:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
2019-11-27 16:12:59 +01:00
|
|
|
|
if (($arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ArrowFunction)
|
2019-11-04 03:27:40 +01:00
|
|
|
|
&& $template_result
|
2020-04-07 06:13:56 +02:00
|
|
|
|
&& $template_result->upper_bounds
|
2019-05-07 00:44:10 +02:00
|
|
|
|
&& $param
|
|
|
|
|
&& $param->type
|
|
|
|
|
&& !$arg->value->getDocComment()
|
|
|
|
|
) {
|
2020-02-08 18:17:57 +01:00
|
|
|
|
if (($argument_offset === 1 && $method_id === 'array_filter' && count($args) === 2)
|
|
|
|
|
|| ($argument_offset === 0 && $method_id === 'array_map' && count($args) >= 2)
|
2019-05-07 22:27:25 +02:00
|
|
|
|
) {
|
2020-02-08 18:17:57 +01:00
|
|
|
|
$function_like_params = [];
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
foreach ($template_result->upper_bounds as $template_name => $_) {
|
2020-02-08 18:17:57 +01:00
|
|
|
|
$function_like_params[] = new \Psalm\Storage\FunctionLikeParameter(
|
|
|
|
|
'function',
|
|
|
|
|
false,
|
|
|
|
|
new Type\Union([
|
|
|
|
|
new Type\Atomic\TTemplateParam(
|
|
|
|
|
$template_name,
|
|
|
|
|
Type::getMixed(),
|
|
|
|
|
$method_id
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 22:27:25 +02:00
|
|
|
|
$replaced_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TCallable(
|
|
|
|
|
'callable',
|
2020-02-08 18:17:57 +01:00
|
|
|
|
array_reverse($function_like_params)
|
2019-05-07 22:27:25 +02:00
|
|
|
|
)
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$replaced_type = clone $param->type;
|
|
|
|
|
}
|
2019-05-07 00:44:10 +02:00
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$replace_template_result = new \Psalm\Internal\Type\TemplateResult(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result->upper_bounds,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
[]
|
|
|
|
|
);
|
2019-05-07 00:44:10 +02:00
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$replaced_type = \Psalm\Internal\Type\UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$replaced_type,
|
|
|
|
|
$replace_template_result,
|
2019-05-07 00:44:10 +02:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
null,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
null,
|
|
|
|
|
null,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
'fn-' . ($context->calling_method_id ?: $context->calling_function_id)
|
2019-05-07 00:44:10 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$replaced_type->replaceTemplateTypesWithArgTypes(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$replace_template_result,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$codebase
|
2019-05-07 00:44:10 +02:00
|
|
|
|
);
|
|
|
|
|
|
2019-11-04 20:48:02 +01:00
|
|
|
|
$closure_id = strtolower($statements_analyzer->getFilePath())
|
2019-05-07 00:44:10 +02:00
|
|
|
|
. ':' . $arg->value->getLine()
|
|
|
|
|
. ':' . (int)$arg->value->getAttribute('startFilePos')
|
|
|
|
|
. ':-:closure';
|
|
|
|
|
|
2020-02-17 22:33:28 +01:00
|
|
|
|
try {
|
|
|
|
|
$closure_storage = $codebase->getClosureStorage(
|
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
|
$closure_id
|
|
|
|
|
);
|
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-07 00:44:10 +02:00
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($replaced_type->getAtomicTypes() as $replaced_type_part) {
|
2019-05-07 00:44:10 +02:00
|
|
|
|
if ($replaced_type_part instanceof Type\Atomic\TCallable
|
2019-06-03 12:19:52 +02:00
|
|
|
|
|| $replaced_type_part instanceof Type\Atomic\TFn
|
2019-05-07 00:44:10 +02:00
|
|
|
|
) {
|
|
|
|
|
foreach ($closure_storage->params as $closure_param_offset => $param_storage) {
|
|
|
|
|
if (isset($replaced_type_part->params[$closure_param_offset]->type)
|
|
|
|
|
&& !$replaced_type_part->params[$closure_param_offset]->type->hasTemplate()
|
|
|
|
|
) {
|
2019-10-16 02:34:41 +02:00
|
|
|
|
if ($param_storage->type) {
|
|
|
|
|
if ($param_storage->type !== $param_storage->signature_type) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$type_match_found = TypeAnalyzer::isContainedBy(
|
|
|
|
|
$codebase,
|
|
|
|
|
$replaced_type_part->params[$closure_param_offset]->type,
|
|
|
|
|
$param_storage->type
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!$type_match_found) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 00:44:10 +02:00
|
|
|
|
$param_storage->type = $replaced_type_part->params[$closure_param_offset]->type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 02:47:55 +02:00
|
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
|
|
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-05-07 02:47:55 +02:00
|
|
|
|
if (!$was_inside_call) {
|
|
|
|
|
$context->inside_call = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 18:17:57 +01:00
|
|
|
|
if (($argument_offset === 0 && $method_id === 'array_filter' && count($args) === 2)
|
|
|
|
|
|| ($argument_offset > 0 && $method_id === 'array_map' && count($args) >= 2)
|
2019-05-07 22:27:25 +02:00
|
|
|
|
) {
|
|
|
|
|
$generic_param_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
|
Type::getArrayKey(),
|
|
|
|
|
new Type\Union([
|
|
|
|
|
new Type\Atomic\TTemplateParam(
|
2020-02-08 18:17:57 +01:00
|
|
|
|
'ArrayValue' . $argument_offset,
|
2019-12-29 00:37:55 +01:00
|
|
|
|
Type::getMixed(),
|
|
|
|
|
$method_id
|
2019-05-07 22:27:25 +02:00
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
])
|
|
|
|
|
]);
|
|
|
|
|
|
2020-02-08 18:17:57 +01:00
|
|
|
|
$template_types = ['ArrayValue' . $argument_offset => [$method_id => [Type::getMixed()]]];
|
2019-05-07 22:27:25 +02:00
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$replace_template_result = new \Psalm\Internal\Type\TemplateResult(
|
2019-05-07 22:27:25 +02:00
|
|
|
|
$template_types,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
\Psalm\Internal\Type\UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$generic_param_type,
|
|
|
|
|
$replace_template_result,
|
2019-05-07 22:27:25 +02:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data->getType($arg->value),
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
'fn-' . ($context->calling_method_id ?: $context->calling_function_id)
|
2019-05-07 22:27:25 +02:00
|
|
|
|
);
|
2019-11-04 03:27:40 +01:00
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
if ($replace_template_result->upper_bounds) {
|
2019-11-04 03:27:40 +01:00
|
|
|
|
if (!$template_result) {
|
|
|
|
|
$template_result = new TemplateResult([], []);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result->upper_bounds += $replace_template_result->upper_bounds;
|
2019-11-04 03:27:40 +01:00
|
|
|
|
}
|
2019-05-07 22:27:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 21:30:56 +01:00
|
|
|
|
if ($codebase->find_unused_variables
|
2019-02-03 22:21:37 +01:00
|
|
|
|
&& ($arg->value instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PreInc
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PreDec)
|
|
|
|
|
) {
|
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
|
$arg->value->var,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($var_id) {
|
|
|
|
|
$context->hasVariable($var_id, $statements_analyzer);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($toggled_class_exists) {
|
|
|
|
|
$context->inside_class_exists = false;
|
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (self::evaluateAribitraryParam(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$arg,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function evaluateAribitraryParam(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
PhpParser\Node\Arg $arg,
|
|
|
|
|
Context $context
|
|
|
|
|
) {
|
2020-03-06 03:10:29 +01:00
|
|
|
|
// there are a bunch of things we want to evaluate even when we don't
|
|
|
|
|
// know what function/method is being called
|
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\StaticCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\New_
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\BinaryOp
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Ternary
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Scalar\Encapsed
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PostInc
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PostDec
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PreInc
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PreDec
|
|
|
|
|
) {
|
|
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
|
$context->inside_call = true;
|
2019-08-13 19:15:23 +02:00
|
|
|
|
|
2020-03-06 03:10:29 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-13 19:15:23 +02:00
|
|
|
|
|
2020-03-06 03:10:29 +01:00
|
|
|
|
if (!$was_inside_call) {
|
|
|
|
|
$context->inside_call = false;
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|
&& $arg->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
) {
|
|
|
|
|
$var_id = '$' . $arg->value->name->name;
|
|
|
|
|
} else {
|
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
|
$arg->value,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($var_id) {
|
2020-03-06 03:10:29 +01:00
|
|
|
|
if (!$context->hasVariable($var_id, $statements_analyzer)
|
|
|
|
|
|| $context->vars_in_scope[$var_id]->isNull()
|
|
|
|
|
) {
|
2020-03-06 05:40:47 +01:00
|
|
|
|
if (!isset($context->vars_in_scope[$var_id])
|
|
|
|
|
&& $arg->value instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
) {
|
2020-03-06 03:24:08 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyUndefinedVariable(
|
|
|
|
|
'Variable ' . $var_id
|
|
|
|
|
. ' must be defined prior to use within an unknown function or method',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 03:10:29 +01:00
|
|
|
|
// we don't know if it exists, assume it's passed by reference
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
|
|
|
|
|
|
if (strpos($var_id, '-') === false
|
|
|
|
|
&& strpos($var_id, '[') === false
|
|
|
|
|
&& !$statements_analyzer->hasVariable($var_id)
|
|
|
|
|
) {
|
|
|
|
|
$location = new CodeLocation($statements_analyzer, $arg->value);
|
|
|
|
|
$statements_analyzer->registerVariable(
|
|
|
|
|
$var_id,
|
|
|
|
|
$location,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$statements_analyzer->registerVariableUses([$location->getHash() => $location]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$context->removeVarFromConflictingClauses(
|
|
|
|
|
$var_id,
|
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($context->vars_in_scope[$var_id]->getAtomicTypes() as $type) {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($type instanceof TArray && $type->type_params[1]->isEmpty()) {
|
|
|
|
|
$context->vars_in_scope[$var_id]->removeType('array');
|
|
|
|
|
$context->vars_in_scope[$var_id]->addType(
|
|
|
|
|
new TArray(
|
|
|
|
|
[Type::getArrayKey(), Type::getMixed()]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-24 22:12:07 +02:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-24 22:12:07 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param string|null $method_id
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function handleByRefFunctionArg(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
$method_id,
|
|
|
|
|
int $argument_offset,
|
|
|
|
|
PhpParser\Node\Arg $arg,
|
|
|
|
|
Context $context
|
|
|
|
|
) {
|
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
|
$arg->value,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$builtin_array_functions = [
|
2020-01-07 17:40:23 +01:00
|
|
|
|
'ksort', 'asort', 'krsort', 'arsort', 'natcasesort', 'natsort',
|
|
|
|
|
'reset', 'end', 'next', 'prev', 'array_pop', 'array_shift',
|
2019-02-03 22:21:37 +01:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (($var_id && isset($context->vars_in_scope[$var_id]))
|
|
|
|
|
|| ($method_id
|
|
|
|
|
&& in_array(
|
|
|
|
|
$method_id,
|
|
|
|
|
$builtin_array_functions,
|
|
|
|
|
true
|
|
|
|
|
))
|
|
|
|
|
) {
|
2020-03-22 00:23:24 +01:00
|
|
|
|
$was_inside_assignment = $context->inside_assignment;
|
|
|
|
|
$context->inside_assignment = true;
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
// if the variable is in scope, get or we're in a special array function,
|
|
|
|
|
// figure out its type before proceeding
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$arg->value,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-22 00:23:24 +01:00
|
|
|
|
|
|
|
|
|
$context->inside_assignment = $was_inside_assignment;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
// special handling for array sort
|
|
|
|
|
if ($argument_offset === 0
|
|
|
|
|
&& $method_id
|
|
|
|
|
&& in_array(
|
|
|
|
|
$method_id,
|
|
|
|
|
$builtin_array_functions,
|
|
|
|
|
true
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
if (in_array($method_id, ['array_pop', 'array_shift'], true)) {
|
|
|
|
|
self::handleByRefArrayAdjustment($statements_analyzer, $arg, $context);
|
|
|
|
|
|
|
|
|
|
return;
|
2018-08-14 17:51:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
// noops
|
|
|
|
|
if (in_array($method_id, ['reset', 'end', 'next', 'prev', 'ksort'], true)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
|
|
|
|
|
&& $arg_value_type->hasArray()
|
2018-08-09 04:33:31 +02:00
|
|
|
|
) {
|
2019-10-01 21:44:43 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-09 00:44:46 +02:00
|
|
|
|
* @var TArray|TList|ObjectLike
|
2019-10-01 21:44:43 +02:00
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$array_type = $arg_value_type->getAtomicTypes()['array'];
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
|
|
|
|
if ($array_type instanceof ObjectLike) {
|
|
|
|
|
$array_type = $array_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 00:44:46 +02:00
|
|
|
|
if ($array_type instanceof TList) {
|
|
|
|
|
$array_type = new TArray([Type::getInt(), $array_type->type_param]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 17:40:23 +01:00
|
|
|
|
$by_ref_type = new Type\Union([clone $array_type]);
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$arg->value,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$by_ref_type,
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$by_ref_type,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$context,
|
|
|
|
|
false
|
|
|
|
|
);
|
2018-08-14 17:51:17 +02:00
|
|
|
|
|
|
|
|
|
return;
|
2018-08-09 04:33:31 +02:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-08-09 04:33:31 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($method_id === 'socket_select') {
|
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$arg->value,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-07 05:52:27 +01:00
|
|
|
|
|
|
|
|
|
if (!$arg->value instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-08-14 17:51:17 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
|
* @param Context $context
|
|
|
|
|
*
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function handleArrayAddition(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
array $args,
|
2019-12-29 16:20:38 +01:00
|
|
|
|
Context $context,
|
|
|
|
|
bool $is_push
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) {
|
|
|
|
|
$array_arg = $args[0]->value;
|
|
|
|
|
|
2019-08-27 20:16:34 +02:00
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$array_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2018-08-09 04:33:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 05:51:36 +02:00
|
|
|
|
for ($i = 1; $i < count($args); $i++) {
|
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args[$i]->value,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (($array_arg_type = $statements_analyzer->node_data->getType($array_arg))
|
|
|
|
|
&& $array_arg_type->hasArray()
|
|
|
|
|
) {
|
2019-10-01 21:44:43 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-11 02:16:43 +02:00
|
|
|
|
* @var TArray|ObjectLike|TList
|
2019-10-01 21:44:43 +02:00
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$array_type = $array_arg_type->getAtomicTypes()['array'];
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-12-29 16:20:38 +01:00
|
|
|
|
$objectlike_list = null;
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($array_type instanceof ObjectLike) {
|
2019-12-29 16:20:38 +01:00
|
|
|
|
if ($array_type->is_list) {
|
|
|
|
|
$objectlike_list = clone $array_type;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$array_type = $array_type->getGenericArrayType();
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$by_ref_type = new Type\Union([clone $array_type]);
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
|
if ($argument_offset === 0) {
|
|
|
|
|
continue;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2017-01-16 18:39:38 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$arg->value,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (!($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
|
|
|
|
|
|| $arg_value_type->hasMixed()
|
|
|
|
|
) {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
new Type\Union([new TArray([Type::getInt(), Type::getMixed()])])
|
2018-11-21 18:38:43 +01:00
|
|
|
|
);
|
2019-02-03 22:21:37 +01:00
|
|
|
|
} elseif ($arg->unpack) {
|
2019-04-09 19:58:49 +02:00
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
clone $arg_value_type
|
2019-04-09 19:58:49 +02:00
|
|
|
|
);
|
2019-02-03 22:21:37 +01:00
|
|
|
|
} else {
|
2019-12-29 16:20:38 +01:00
|
|
|
|
if ($objectlike_list) {
|
|
|
|
|
if ($is_push) {
|
2019-12-29 16:29:03 +01:00
|
|
|
|
\array_push($objectlike_list->properties, $arg_value_type);
|
2019-12-29 16:20:38 +01:00
|
|
|
|
} else {
|
2019-12-29 16:29:03 +01:00
|
|
|
|
\array_unshift($objectlike_list->properties, $arg_value_type);
|
2019-12-29 16:20:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$by_ref_type = new Type\Union([$objectlike_list]);
|
|
|
|
|
} elseif ($array_type instanceof TList) {
|
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
new Type\Union(
|
|
|
|
|
[
|
|
|
|
|
new TNonEmptyList(clone $arg_value_type),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
new Type\Union(
|
|
|
|
|
[
|
|
|
|
|
new TNonEmptyArray(
|
|
|
|
|
[
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
clone $arg_value_type
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-20 05:46:22 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$array_arg,
|
|
|
|
|
$by_ref_type,
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$by_ref_type,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$context,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-11-20 03:36:09 +01:00
|
|
|
|
|
2019-08-13 20:53:31 +02:00
|
|
|
|
$context->inside_call = false;
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-02 17:18:56 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
|
* @param Context $context
|
|
|
|
|
*
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function handleArraySplice(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
array $args,
|
|
|
|
|
Context $context
|
|
|
|
|
) {
|
2019-08-13 19:15:23 +02:00
|
|
|
|
$context->inside_call = true;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$array_arg = $args[0]->value;
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$array_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$offset_arg = $args[1]->value;
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$offset_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (!isset($args[2])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$length_arg = $args[2]->value;
|
2018-04-19 18:16:00 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$length_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-04-19 18:16:00 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (!isset($args[3])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-06-17 05:40:25 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$replacement_arg = $args[3]->value;
|
2018-06-17 05:40:25 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$replacement_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-04-19 18:16:00 +02:00
|
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
|
$context->inside_call = false;
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$replacement_arg_type = $statements_analyzer->node_data->getType($replacement_arg);
|
|
|
|
|
|
|
|
|
|
if ($replacement_arg_type
|
|
|
|
|
&& !$replacement_arg_type->hasArray()
|
|
|
|
|
&& $replacement_arg_type->hasString()
|
|
|
|
|
&& $replacement_arg_type->isSingle()
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$replacement_arg_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TArray([Type::getInt(), $replacement_arg_type])
|
2019-02-03 22:21:37 +01:00
|
|
|
|
]);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($replacement_arg, $replacement_arg_type);
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-05-09 00:11:10 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (($array_arg_type = $statements_analyzer->node_data->getType($array_arg))
|
|
|
|
|
&& $array_arg_type->hasArray()
|
|
|
|
|
&& $replacement_arg_type
|
|
|
|
|
&& $replacement_arg_type->hasArray()
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) {
|
2019-10-01 21:44:43 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-11 02:16:43 +02:00
|
|
|
|
* @var TArray|ObjectLike|TList
|
2019-10-01 21:44:43 +02:00
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$array_type = $array_arg_type->getAtomicTypes()['array'];
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($array_type instanceof ObjectLike) {
|
2020-03-09 06:15:52 +01:00
|
|
|
|
if ($array_type->is_list) {
|
|
|
|
|
$array_type = new TNonEmptyList($array_type->getGenericValueType());
|
|
|
|
|
} else {
|
|
|
|
|
$array_type = $array_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($array_type instanceof TArray
|
|
|
|
|
&& $array_type->type_params[0]->hasInt()
|
|
|
|
|
&& !$array_type->type_params[0]->hasString()
|
|
|
|
|
) {
|
|
|
|
|
if ($array_type instanceof TNonEmptyArray) {
|
|
|
|
|
$array_type = new TNonEmptyList($array_type->type_params[1]);
|
|
|
|
|
} else {
|
|
|
|
|
$array_type = new TList($array_type->type_params[1]);
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2018-01-29 00:13:38 +01:00
|
|
|
|
|
2019-10-01 21:44:43 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-11 02:16:43 +02:00
|
|
|
|
* @var TArray|ObjectLike|TList
|
2019-10-01 21:44:43 +02:00
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$replacement_array_type = $replacement_arg_type->getAtomicTypes()['array'];
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
|
|
|
|
$by_ref_type = TypeCombination::combineTypes([$array_type, $replacement_array_type]);
|
|
|
|
|
|
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$array_arg,
|
|
|
|
|
$by_ref_type,
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$by_ref_type,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$context,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$array_type = Type::getArray();
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$array_arg,
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$array_type,
|
|
|
|
|
$array_type,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$context,
|
|
|
|
|
false
|
|
|
|
|
);
|
2016-12-07 20:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 18:38:43 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function handleByRefArrayAdjustment(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
PhpParser\Node\Arg $arg,
|
|
|
|
|
Context $context
|
|
|
|
|
) {
|
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
|
$arg->value,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
|
$context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer);
|
|
|
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
|
$array_type = clone $context->vars_in_scope[$var_id];
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$array_atomic_types = $array_type->getAtomicTypes();
|
2018-11-21 18:38:43 +01:00
|
|
|
|
|
|
|
|
|
foreach ($array_atomic_types as $array_atomic_type) {
|
|
|
|
|
if ($array_atomic_type instanceof ObjectLike) {
|
2019-10-09 16:36:55 +02:00
|
|
|
|
$array_atomic_type = $array_atomic_type->getGenericArrayType();
|
|
|
|
|
}
|
2018-11-21 18:38:43 +01:00
|
|
|
|
|
2019-10-09 16:36:55 +02:00
|
|
|
|
if ($array_atomic_type instanceof TNonEmptyArray) {
|
2018-11-21 18:38:43 +01:00
|
|
|
|
if (!$context->inside_loop && $array_atomic_type->count !== null) {
|
|
|
|
|
if ($array_atomic_type->count === 0) {
|
|
|
|
|
$array_atomic_type = new TArray(
|
|
|
|
|
[
|
|
|
|
|
new Type\Union([new TEmpty]),
|
|
|
|
|
new Type\Union([new TEmpty]),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$array_atomic_type->count--;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$array_atomic_type = new TArray($array_atomic_type->type_params);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 00:44:46 +02:00
|
|
|
|
$array_type->addType($array_atomic_type);
|
2019-10-09 16:36:55 +02:00
|
|
|
|
$context->removeDescendents($var_id, $array_type);
|
2019-10-09 00:44:46 +02:00
|
|
|
|
} elseif ($array_atomic_type instanceof TNonEmptyList) {
|
|
|
|
|
if (!$context->inside_loop && $array_atomic_type->count !== null) {
|
|
|
|
|
if ($array_atomic_type->count === 0) {
|
|
|
|
|
$array_atomic_type = new TArray(
|
|
|
|
|
[
|
|
|
|
|
new Type\Union([new TEmpty]),
|
|
|
|
|
new Type\Union([new TEmpty]),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$array_atomic_type->count--;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$array_atomic_type = new TList($array_atomic_type->type_param);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 18:38:43 +01:00
|
|
|
|
$array_type->addType($array_atomic_type);
|
2019-10-09 16:36:55 +02:00
|
|
|
|
$context->removeDescendents($var_id, $array_type);
|
2018-11-21 18:38:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $array_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-12-31 16:51:42 +01:00
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2020-03-15 18:44:00 +01:00
|
|
|
|
* @param string|MethodIdentifier|null $method_id
|
2017-02-10 02:35:17 +01:00
|
|
|
|
* @param array<int,FunctionLikeParameter> $function_params
|
2017-04-28 06:31:55 +02:00
|
|
|
|
* @param FunctionLikeStorage|null $function_storage
|
|
|
|
|
* @param ClassLikeStorage|null $class_storage
|
2016-12-31 16:51:42 +01:00
|
|
|
|
* @param CodeLocation $code_location
|
2017-09-03 00:15:52 +02:00
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-12-07 20:13:39 +01:00
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
2020-03-12 04:04:52 +01:00
|
|
|
|
public static function checkFunctionLikeArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-12-07 20:13:39 +01:00
|
|
|
|
array $args,
|
|
|
|
|
$method_id,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
array $function_params,
|
2017-04-28 06:31:55 +02:00
|
|
|
|
$function_storage,
|
|
|
|
|
$class_storage,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
?TemplateResult $class_template_result,
|
2017-02-27 17:07:44 +01:00
|
|
|
|
CodeLocation $code_location,
|
2017-09-03 00:15:52 +02:00
|
|
|
|
Context $context
|
2016-12-07 20:13:39 +01:00
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$in_call_map = $method_id ? CallMap::inCallMap((string) $method_id) : false;
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$cased_method_id = (string) $method_id;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
|
$is_variadic = false;
|
|
|
|
|
|
|
|
|
|
$fq_class_name = null;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
|
if ($method_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if (!$in_call_map && $method_id instanceof \Psalm\Internal\MethodIdentifier) {
|
|
|
|
|
$fq_class_name = $method_id->fq_class_name;
|
2020-01-12 17:33:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($function_storage) {
|
|
|
|
|
$is_variadic = $function_storage->variadic;
|
2020-02-15 02:54:26 +01:00
|
|
|
|
} elseif (is_string($method_id)) {
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$is_variadic = $codebase->functions->isVariadic(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2017-07-29 21:05:06 +02:00
|
|
|
|
strtolower($method_id),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getRootFilePath()
|
2017-07-29 21:05:06 +02:00
|
|
|
|
);
|
2016-12-07 20:13:39 +01:00
|
|
|
|
} else {
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$is_variadic = $codebase->methods->isVariadic($method_id);
|
2016-12-07 20:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($method_id instanceof \Psalm\Internal\MethodIdentifier) {
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$cased_method_id = $codebase->methods->getCasedMethodId($method_id);
|
2018-02-02 17:26:55 +01:00
|
|
|
|
} elseif ($function_storage) {
|
|
|
|
|
$cased_method_id = $function_storage->cased_name;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 16:55:31 +01:00
|
|
|
|
$calling_class_storage = $class_storage;
|
|
|
|
|
|
2019-04-12 06:44:10 +02:00
|
|
|
|
$static_fq_class_name = $fq_class_name;
|
|
|
|
|
$self_fq_class_name = $fq_class_name;
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($method_id instanceof \Psalm\Internal\MethodIdentifier) {
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
2018-02-01 07:52:20 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($declaring_method_id && (string)$declaring_method_id !== (string)$method_id) {
|
|
|
|
|
$self_fq_class_name = $declaring_method_id->fq_class_name;
|
2019-04-12 06:44:10 +02:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($self_fq_class_name);
|
2018-02-01 07:52:20 +01:00
|
|
|
|
}
|
2019-03-17 18:50:02 +01:00
|
|
|
|
|
|
|
|
|
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
|
|
|
|
|
|
|
|
|
|
if ($appearing_method_id && $declaring_method_id !== $appearing_method_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$self_fq_class_name = $appearing_method_id->fq_class_name;
|
2019-03-17 18:50:02 +01:00
|
|
|
|
}
|
2018-02-01 07:52:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
if ($function_params) {
|
|
|
|
|
foreach ($function_params as $function_param) {
|
|
|
|
|
$is_variadic = $is_variadic || $function_param->is_variadic;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$has_packed_var = false;
|
|
|
|
|
|
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
|
$has_packed_var = $has_packed_var || $arg->unpack;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 06:07:58 +01:00
|
|
|
|
$last_param = $function_params
|
|
|
|
|
? $function_params[count($function_params) - 1]
|
|
|
|
|
: null;
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result = null;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$class_generic_params = $class_template_result
|
2020-04-07 06:13:56 +02:00
|
|
|
|
? $class_template_result->upper_bounds
|
2019-11-06 17:20:51 +01:00
|
|
|
|
: [];
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if ($function_storage) {
|
2020-03-27 23:30:59 +01:00
|
|
|
|
$template_types = self::getTemplateTypesForCall(
|
2019-01-13 00:18:23 +01:00
|
|
|
|
$class_storage,
|
2020-03-27 23:30:59 +01:00
|
|
|
|
$calling_class_storage,
|
|
|
|
|
$function_storage->template_types ?: []
|
2019-01-13 00:18:23 +01:00
|
|
|
|
);
|
2019-01-20 00:11:39 +01:00
|
|
|
|
|
|
|
|
|
if ($template_types) {
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$template_result = $class_template_result;
|
2019-11-04 03:27:40 +01:00
|
|
|
|
|
|
|
|
|
if (!$template_result) {
|
|
|
|
|
$template_result = new TemplateResult($template_types, []);
|
|
|
|
|
} elseif (!$template_result->template_types) {
|
|
|
|
|
$template_result->template_types = $template_types;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-20 00:11:39 +01:00
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
|
$function_param = count($function_params) > $argument_offset
|
|
|
|
|
? $function_params[$argument_offset]
|
|
|
|
|
: ($last_param && $last_param->is_variadic ? $last_param : null);
|
|
|
|
|
|
|
|
|
|
if (!$function_param
|
|
|
|
|
|| !$function_param->type
|
|
|
|
|
) {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
continue;
|
2018-04-20 23:14:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type = $statements_analyzer->node_data->getType($arg->value);
|
|
|
|
|
|
|
|
|
|
if (!$arg_value_type) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$function_param->type,
|
|
|
|
|
$template_result,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2019-12-30 13:25:04 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id ?: $context->calling_function_id,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
false
|
|
|
|
|
);
|
2019-11-04 03:27:40 +01:00
|
|
|
|
|
2019-11-06 17:20:51 +01:00
|
|
|
|
if (!$class_template_result) {
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result->upper_bounds = [];
|
2019-11-04 03:27:40 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
|
|
2019-11-06 17:20:51 +01:00
|
|
|
|
foreach ($class_generic_params as $template_name => $type_map) {
|
2019-03-22 20:59:10 +01:00
|
|
|
|
foreach ($type_map as $class => $type) {
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$class_generic_params[$template_name][$class][0] = clone $type[0];
|
2019-03-22 20:59:10 +01:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2019-01-21 23:42:25 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$function_param_count = count($function_params);
|
2019-01-21 23:29:12 +01:00
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
|
$function_param = $function_param_count > $argument_offset
|
|
|
|
|
? $function_params[$argument_offset]
|
|
|
|
|
: ($last_param && $last_param->is_variadic ? $last_param : null);
|
|
|
|
|
|
|
|
|
|
if ($function_param
|
|
|
|
|
&& $function_param->by_ref
|
|
|
|
|
&& $method_id !== 'extract'
|
|
|
|
|
) {
|
|
|
|
|
if (self::handlePossiblyMatchingByRefParam(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$codebase,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
(string) $method_id,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$cased_method_id,
|
|
|
|
|
$last_param,
|
|
|
|
|
$function_params,
|
|
|
|
|
$function_storage,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$arg,
|
|
|
|
|
$context,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) === false) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return;
|
2018-04-07 21:16:46 +02:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-07-24 23:24:23 +02:00
|
|
|
|
if ($method_id === 'compact'
|
2019-11-25 17:44:54 +01:00
|
|
|
|
&& ($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
|
|
|
|
|
&& $arg_value_type->isSingleStringLiteral()
|
2019-07-24 23:24:23 +02:00
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$literal = $arg_value_type->getSingleStringLiteral();
|
2019-07-24 23:24:23 +02:00
|
|
|
|
|
2019-07-24 23:41:13 +02:00
|
|
|
|
if (!$context->hasVariable('$' . $literal->value, $statements_analyzer)) {
|
2019-07-24 23:24:23 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UndefinedVariable(
|
|
|
|
|
'Cannot find referenced variable $' . $literal->value,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (self::checkFunctionLikeArgumentMatches(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$cased_method_id,
|
2019-04-12 06:44:10 +02:00
|
|
|
|
$self_fq_class_name,
|
|
|
|
|
$static_fq_class_name,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
$code_location,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$function_param,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$arg,
|
|
|
|
|
$context,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$class_generic_params,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result,
|
2019-09-11 19:52:37 +02:00
|
|
|
|
$function_storage ? $function_storage->pure : false,
|
|
|
|
|
$in_call_map
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if ($method_id === 'array_map' || $method_id === 'array_filter') {
|
2017-10-07 16:33:19 +02:00
|
|
|
|
if ($method_id === 'array_map' && count($args) < 2) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooFewArguments(
|
|
|
|
|
'Too few arguments for ' . $method_id,
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$method_id
|
2017-10-07 16:33:19 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-10-07 16:33:19 +02:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2017-10-07 16:33:19 +02:00
|
|
|
|
}
|
|
|
|
|
} elseif ($method_id === 'array_filter' && count($args) < 1) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooFewArguments(
|
|
|
|
|
'Too few arguments for ' . $method_id,
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$method_id
|
2017-10-07 16:33:19 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-10-07 16:33:19 +02:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2017-10-07 16:33:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
|
if (self::checkArrayFunctionArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
$context,
|
2016-12-31 16:51:42 +01:00
|
|
|
|
$args,
|
2019-01-15 23:53:23 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
$context->check_functions
|
2016-12-31 16:51:42 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-30 18:06:49 +02:00
|
|
|
|
|
|
|
|
|
return null;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if (!$is_variadic
|
|
|
|
|
&& count($args) > count($function_params)
|
|
|
|
|
&& (!count($function_params) || $function_params[count($function_params) - 1]->name !== '...=')
|
2020-03-15 18:44:00 +01:00
|
|
|
|
&& ($in_call_map
|
|
|
|
|
|| !$function_storage instanceof \Psalm\Storage\MethodStorage
|
|
|
|
|
|| $function_storage->is_static
|
|
|
|
|
|| ($method_id instanceof MethodIdentifier
|
|
|
|
|
&& $method_id->method_name === '__construct'))
|
2017-02-10 02:35:17 +01:00
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooManyArguments(
|
2020-03-15 18:44:00 +01:00
|
|
|
|
'Too many arguments for ' . ($cased_method_id ?: $method_id)
|
2018-02-02 17:26:55 +01:00
|
|
|
|
. ' - expecting ' . count($function_params) . ' but saw ' . count($args),
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$code_location,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
(string) $method_id
|
2017-02-10 02:35:17 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-02-10 02:35:17 +01:00
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if (!$has_packed_var && count($args) < count($function_params)) {
|
2019-06-19 15:04:01 +02:00
|
|
|
|
if ($function_storage) {
|
|
|
|
|
$expected_param_count = $function_storage->required_param_count;
|
|
|
|
|
} else {
|
|
|
|
|
for ($i = 0, $j = count($function_params); $i < $j; ++$i) {
|
|
|
|
|
$param = $function_params[$i];
|
|
|
|
|
|
|
|
|
|
if ($param->is_optional || $param->is_variadic) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$expected_param_count = $i;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 23:26:09 +01:00
|
|
|
|
for ($i = count($args), $j = count($function_params); $i < $j; ++$i) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$param = $function_params[$i];
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2020-03-15 18:44:00 +01:00
|
|
|
|
if (!$param->is_optional
|
|
|
|
|
&& !$param->is_variadic
|
|
|
|
|
&& ($in_call_map
|
|
|
|
|
|| !$function_storage instanceof \Psalm\Storage\MethodStorage
|
|
|
|
|
|| $function_storage->is_static
|
|
|
|
|
|| ($method_id instanceof MethodIdentifier
|
|
|
|
|
&& $method_id->method_name === '__construct'))
|
|
|
|
|
) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooFewArguments(
|
2020-03-15 18:44:00 +01:00
|
|
|
|
'Too few arguments for ' . $cased_method_id
|
2019-06-19 15:04:01 +02:00
|
|
|
|
. ' - expecting ' . $expected_param_count . ' but saw ' . count($args),
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$code_location,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
(string) $method_id
|
2017-02-10 02:35:17 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-02-10 02:35:17 +01:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
|
|
|
|
break;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
2019-03-08 04:32:38 +01:00
|
|
|
|
|
|
|
|
|
if ($param->is_optional
|
|
|
|
|
&& $param->type
|
|
|
|
|
&& $param->default_type
|
|
|
|
|
&& !$param->is_variadic
|
2019-11-04 03:27:40 +01:00
|
|
|
|
&& $template_result
|
2019-03-08 04:32:38 +01:00
|
|
|
|
) {
|
2019-11-04 03:27:40 +01:00
|
|
|
|
UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$param->type,
|
|
|
|
|
$template_result,
|
2019-03-08 04:32:38 +01:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-03-08 04:32:38 +01:00
|
|
|
|
clone $param->default_type,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$i,
|
2019-12-30 13:25:04 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id ?: $context->calling_function_id,
|
2019-03-08 04:32:38 +01:00
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
/**
|
2020-02-15 02:54:26 +01:00
|
|
|
|
* @param ?string $self_fq_class_name
|
|
|
|
|
* @param ?string $static_fq_class_name
|
2019-11-06 17:20:51 +01:00
|
|
|
|
* @param array<string, array<string, array{Type\Union, 1?:int}>> $class_generic_params
|
2019-02-03 22:21:37 +01:00
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function checkFunctionLikeArgumentMatches(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
?string $cased_method_id,
|
|
|
|
|
?string $self_fq_class_name,
|
|
|
|
|
?string $static_fq_class_name,
|
|
|
|
|
CodeLocation $function_location,
|
|
|
|
|
?FunctionLikeParameter $function_param,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
int $argument_offset,
|
|
|
|
|
PhpParser\Node\Arg $arg,
|
|
|
|
|
Context $context,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
array $class_generic_params,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
?TemplateResult $template_result,
|
2019-09-11 19:52:37 +02:00
|
|
|
|
bool $function_is_pure,
|
|
|
|
|
bool $in_call_map
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) {
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type = $statements_analyzer->node_data->getType($arg->value);
|
|
|
|
|
|
|
|
|
|
if (!$arg_value_type) {
|
2019-03-03 21:11:09 +01:00
|
|
|
|
if ($function_param && !$function_param->by_ref) {
|
2019-03-23 14:50:47 +01: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->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
|
|
|
|
$param_type = $function_param->type;
|
|
|
|
|
|
|
|
|
|
if ($function_param->is_variadic
|
|
|
|
|
&& $param_type
|
|
|
|
|
&& $param_type->hasArray()
|
|
|
|
|
) {
|
2019-10-01 21:44:43 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-11 14:24:35 +02:00
|
|
|
|
* @var TList|TArray
|
2019-10-01 21:44:43 +02:00
|
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
|
$array_type = $param_type->getAtomicTypes()['array'];
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-10-11 14:24:35 +02:00
|
|
|
|
if ($array_type instanceof TList) {
|
|
|
|
|
$param_type = $array_type->type_param;
|
|
|
|
|
} else {
|
|
|
|
|
$param_type = $array_type->type_params[1];
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($param_type && !$param_type->hasMixed()) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
|
|
|
|
|
. ' cannot be mixed, expecting ' . $param_type,
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value),
|
|
|
|
|
$cased_method_id
|
2019-02-03 22:21:37 +01:00
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 00:33:33 +02:00
|
|
|
|
if (!$function_param) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if (self::checkFunctionLikeTypeMatches(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$codebase,
|
|
|
|
|
$cased_method_id,
|
2019-04-12 06:44:10 +02:00
|
|
|
|
$self_fq_class_name,
|
|
|
|
|
$static_fq_class_name,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
$function_location,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$function_param,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$argument_offset,
|
|
|
|
|
$arg,
|
|
|
|
|
$context,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$class_generic_params,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result,
|
2019-09-11 19:52:37 +02:00
|
|
|
|
$function_is_pure,
|
|
|
|
|
$in_call_map
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|null $method_id
|
|
|
|
|
* @param string|null $cased_method_id
|
|
|
|
|
* @param FunctionLikeParameter|null $last_param
|
|
|
|
|
* @param array<int, FunctionLikeParameter> $function_params
|
|
|
|
|
* @param FunctionLikeStorage|null $function_storage
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function handlePossiblyMatchingByRefParam(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
$method_id,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$last_param,
|
|
|
|
|
$function_params,
|
|
|
|
|
$function_storage,
|
|
|
|
|
int $argument_offset,
|
|
|
|
|
PhpParser\Node\Arg $arg,
|
|
|
|
|
Context $context,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
?TemplateResult $template_result
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) {
|
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Scalar
|
2019-02-21 19:26:37 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Cast
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
2020-03-05 04:30:53 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\BinaryOp
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Ternary
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|| (
|
|
|
|
|
(
|
|
|
|
|
$arg->value instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
|
2019-04-18 19:51:34 +02:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\StaticCall
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) && (
|
2019-11-25 17:44:54 +01:00
|
|
|
|
!($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
|
|
|
|
|
|| !$arg_value_type->by_ref
|
2019-02-03 22:21:37 +01:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidPassByReference(
|
|
|
|
|
'Parameter ' . ($argument_offset + 1) . ' of ' . $cased_method_id . ' expects a variable',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return false;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!in_array(
|
|
|
|
|
$method_id,
|
|
|
|
|
[
|
2020-01-07 17:40:23 +01:00
|
|
|
|
'ksort', 'asort', 'krsort', 'arsort', 'natcasesort', 'natsort',
|
|
|
|
|
'reset', 'end', 'next', 'prev', 'array_pop', 'array_shift',
|
2019-02-03 22:21:37 +01:00
|
|
|
|
'array_push', 'array_unshift', 'socket_select', 'array_splice',
|
|
|
|
|
],
|
|
|
|
|
true
|
|
|
|
|
)) {
|
|
|
|
|
$by_ref_type = null;
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$by_ref_out_type = null;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-02-21 00:53:42 +01:00
|
|
|
|
$check_null_ref = true;
|
|
|
|
|
|
2019-02-03 22:21:37 +01:00
|
|
|
|
if ($last_param) {
|
|
|
|
|
if ($argument_offset < count($function_params)) {
|
2019-04-09 19:58:49 +02:00
|
|
|
|
$function_param = $function_params[$argument_offset];
|
|
|
|
|
} else {
|
|
|
|
|
$function_param = $last_param;
|
|
|
|
|
}
|
2019-02-21 00:53:42 +01:00
|
|
|
|
|
2019-04-09 19:58:49 +02:00
|
|
|
|
$by_ref_type = $function_param->type;
|
2019-02-21 00:53:42 +01:00
|
|
|
|
|
2019-04-09 19:58:49 +02:00
|
|
|
|
if (isset($function_storage->param_out_types[$argument_offset])) {
|
|
|
|
|
$by_ref_out_type = $function_storage->param_out_types[$argument_offset];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($by_ref_type && $by_ref_type->isNullable()) {
|
|
|
|
|
$check_null_ref = false;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
if ($template_result && $by_ref_type) {
|
2019-05-25 00:17:48 +02:00
|
|
|
|
$original_by_ref_type = clone $by_ref_type;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$by_ref_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
2019-12-01 16:40:53 +01:00
|
|
|
|
clone $by_ref_type,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data->getType($arg->value),
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
'fn-' . ($context->calling_method_id ?: $context->calling_function_id)
|
2019-02-03 22:21:37 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
if ($template_result->upper_bounds) {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$original_by_ref_type->replaceTemplateTypesWithArgTypes(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result,
|
|
|
|
|
$codebase
|
2019-02-03 22:21:37 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$by_ref_type = $original_by_ref_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
|
|
2019-12-01 16:40:53 +01:00
|
|
|
|
if ($template_result && $by_ref_out_type) {
|
2019-12-01 16:43:51 +01:00
|
|
|
|
$original_by_ref_out_type = clone $by_ref_out_type;
|
2019-12-01 16:40:53 +01:00
|
|
|
|
|
|
|
|
|
$by_ref_out_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
clone $by_ref_out_type,
|
|
|
|
|
$template_result,
|
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-12-01 16:40:53 +01:00
|
|
|
|
$statements_analyzer->node_data->getType($arg->value),
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
'fn-' . ($context->calling_method_id ?: $context->calling_function_id)
|
2019-12-01 16:40:53 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
if ($template_result->upper_bounds) {
|
2019-12-01 16:40:53 +01:00
|
|
|
|
$original_by_ref_out_type->replaceTemplateTypesWithArgTypes(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result,
|
|
|
|
|
$codebase
|
2019-12-01 16:40:53 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$by_ref_out_type = $original_by_ref_out_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 19:58:49 +02:00
|
|
|
|
if ($by_ref_type && $function_param->is_variadic && $arg->unpack) {
|
|
|
|
|
$by_ref_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
]),
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$by_ref_type = $by_ref_type ?: Type::getMixed();
|
|
|
|
|
|
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$arg->value,
|
|
|
|
|
$by_ref_type,
|
2019-03-03 21:11:09 +01:00
|
|
|
|
$by_ref_out_type ?: $by_ref_type,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$context,
|
2019-02-20 23:43:12 +01:00
|
|
|
|
$method_id && (strpos($method_id, '::') !== false || !CallMap::inCallMap($method_id)),
|
2019-02-21 00:53:42 +01:00
|
|
|
|
$check_null_ref
|
2019-02-03 22:21:37 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-15 02:54:26 +01:00
|
|
|
|
* @param ?string $self_fq_class_name
|
|
|
|
|
* @param ?string $static_fq_class_name
|
2019-11-06 17:20:51 +01:00
|
|
|
|
* @param array<string, array<string, array{Type\Union, 1?:int}>> $class_generic_params
|
2019-03-22 20:59:10 +01:00
|
|
|
|
* @param array<string, array<string, array{Type\Union, 1?:int}>> $generic_params
|
|
|
|
|
* @param array<string, array<string, array{Type\Union}>> $template_types
|
2019-02-03 22:21:37 +01:00
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function checkFunctionLikeTypeMatches(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
Codebase $codebase,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
?string $cased_method_id,
|
|
|
|
|
?string $self_fq_class_name,
|
|
|
|
|
?string $static_fq_class_name,
|
|
|
|
|
CodeLocation $function_location,
|
|
|
|
|
FunctionLikeParameter $function_param,
|
2019-02-03 22:21:37 +01:00
|
|
|
|
Type\Union $arg_type,
|
|
|
|
|
int $argument_offset,
|
|
|
|
|
PhpParser\Node\Arg $arg,
|
|
|
|
|
Context $context,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
?array $class_generic_params,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
?TemplateResult $template_result,
|
2019-09-11 19:52:37 +02:00
|
|
|
|
bool $function_is_pure,
|
|
|
|
|
bool $in_call_map
|
2019-02-03 22:21:37 +01:00
|
|
|
|
) {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if (!$function_param->type) {
|
|
|
|
|
if (!$codebase->infer_types_from_usage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$param_type = Type::getMixed();
|
|
|
|
|
} else {
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$param_type = clone $function_param->type;
|
2019-05-31 07:47:35 +02:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2020-02-28 05:10:26 +01:00
|
|
|
|
$bindable_template_params = [];
|
|
|
|
|
|
|
|
|
|
if ($template_result) {
|
|
|
|
|
$bindable_template_params = $param_type->getTemplateTypes();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 17:20:51 +01:00
|
|
|
|
if ($class_generic_params) {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$empty_generic_params = [];
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$empty_template_result = new TemplateResult($class_generic_params, $empty_generic_params);
|
2019-11-04 03:27:40 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type = $statements_analyzer->node_data->getType($arg->value);
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$param_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$param_type,
|
|
|
|
|
$empty_template_result,
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2019-12-29 00:37:55 +01:00
|
|
|
|
$context->self ?: 'fn-' . $context->calling_function_id
|
2019-05-31 07:47:35 +02:00
|
|
|
|
);
|
2019-03-29 03:47:17 +01:00
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$arg_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$arg_type,
|
|
|
|
|
$empty_template_result,
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$arg_value_type,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2019-12-29 00:37:55 +01:00
|
|
|
|
$context->self ?: 'fn-' . $context->calling_function_id
|
2019-05-31 07:47:35 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
if ($template_result && $template_result->template_types) {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$arg_type_param = $arg_type;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($arg->unpack) {
|
2020-02-18 01:54:11 +01:00
|
|
|
|
$arg_type_param = null;
|
2019-10-09 00:44:46 +02:00
|
|
|
|
|
2020-02-18 01:54:11 +01:00
|
|
|
|
foreach ($arg_type->getAtomicTypes() as $arg_atomic_type) {
|
|
|
|
|
if ($arg_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
|
|| $arg_atomic_type instanceof Type\Atomic\TList
|
|
|
|
|
|| $arg_atomic_type instanceof Type\Atomic\ObjectLike
|
|
|
|
|
) {
|
|
|
|
|
if ($arg_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
$arg_type_param = $arg_atomic_type->getGenericValueType();
|
|
|
|
|
} elseif ($arg_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
|
$arg_type_param = $arg_atomic_type->type_param;
|
|
|
|
|
} else {
|
|
|
|
|
$arg_type_param = $arg_atomic_type->type_params[1];
|
|
|
|
|
}
|
|
|
|
|
} elseif ($arg_atomic_type instanceof Type\Atomic\TIterable) {
|
|
|
|
|
$arg_type_param = $arg_atomic_type->type_params[1];
|
|
|
|
|
} elseif ($arg_atomic_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
|
ForeachAnalyzer::getKeyValueParamsForTraversableObject(
|
|
|
|
|
$arg_atomic_type,
|
|
|
|
|
$codebase,
|
|
|
|
|
$key_type,
|
|
|
|
|
$arg_type_param
|
|
|
|
|
);
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2020-02-18 01:54:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$arg_type_param) {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$arg_type_param = Type::getMixed();
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$param_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$param_type,
|
|
|
|
|
$template_result,
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-06 17:20:51 +01:00
|
|
|
|
$arg_type_param,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$argument_offset,
|
2019-12-30 13:25:04 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id ?: $context->calling_function_id
|
2019-05-31 07:47:35 +02:00
|
|
|
|
);
|
2019-11-07 17:03:41 +01:00
|
|
|
|
|
|
|
|
|
foreach ($bindable_template_params as $template_type) {
|
|
|
|
|
if (!isset(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result->upper_bounds
|
2019-11-07 17:03:41 +01:00
|
|
|
|
[$template_type->param_name]
|
2019-12-29 00:37:55 +01:00
|
|
|
|
[$template_type->defining_class]
|
2020-04-07 06:13:56 +02:00
|
|
|
|
)
|
|
|
|
|
&& !isset(
|
|
|
|
|
$template_result->lower_bounds
|
|
|
|
|
[$template_type->param_name]
|
|
|
|
|
[$template_type->defining_class]
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$template_result->upper_bounds[$template_type->param_name][$template_type->defining_class] = [
|
2020-02-28 05:00:40 +01:00
|
|
|
|
clone $template_type->as,
|
|
|
|
|
0
|
2019-11-07 17:03:41 +01:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-31 07:47:35 +02:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if (!$context->check_variables) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-25 17:51:09 +02:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$parent_class = null;
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$classlike_storage = null;
|
|
|
|
|
$static_classlike_storage = null;
|
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($self_fq_class_name) {
|
|
|
|
|
$classlike_storage = $codebase->classlike_storage_provider->get($self_fq_class_name);
|
|
|
|
|
$parent_class = $classlike_storage->parent_class;
|
2020-02-15 04:24:39 +01:00
|
|
|
|
$static_classlike_storage = $classlike_storage;
|
2020-02-15 02:54:26 +01:00
|
|
|
|
|
|
|
|
|
if ($static_fq_class_name && $static_fq_class_name !== $self_fq_class_name) {
|
|
|
|
|
$static_classlike_storage = $codebase->classlike_storage_provider->get($static_fq_class_name);
|
|
|
|
|
}
|
2019-05-31 07:47:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fleshed_out_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
|
|
|
|
$param_type,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$classlike_storage ? $classlike_storage->name : null,
|
|
|
|
|
$static_classlike_storage ? $static_classlike_storage->name : null,
|
2020-03-25 14:18:49 +01:00
|
|
|
|
$parent_class,
|
|
|
|
|
true,
|
2020-03-29 19:37:37 +02:00
|
|
|
|
false,
|
2020-03-25 14:18:49 +01:00
|
|
|
|
$static_classlike_storage ? $static_classlike_storage->final : false
|
2019-05-31 07:47:35 +02:00
|
|
|
|
);
|
2019-05-25 17:51:09 +02:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$fleshed_out_signature_type = $function_param->signature_type
|
|
|
|
|
? ExpressionAnalyzer::fleshOutType(
|
2019-02-03 22:21:37 +01:00
|
|
|
|
$codebase,
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$function_param->signature_type,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$classlike_storage ? $classlike_storage->name : null,
|
|
|
|
|
$static_classlike_storage ? $static_classlike_storage->name : null,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$parent_class
|
2019-05-31 07:47:35 +02:00
|
|
|
|
)
|
|
|
|
|
: null;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$unpacked_atomic_array = null;
|
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($arg->unpack) {
|
|
|
|
|
if ($arg_type->hasMixed()) {
|
|
|
|
|
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->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
|
2020-04-12 07:26:11 +02:00
|
|
|
|
. ' cannot be ' . $arg_type->getId() . ', expecting array',
|
2019-05-31 07:47:35 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value),
|
|
|
|
|
$cased_method_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($arg_type->hasArray()) {
|
2019-10-01 21:44:43 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-09 00:44:46 +02:00
|
|
|
|
* @var Type\Atomic\TArray|Type\Atomic\TList|Type\Atomic\ObjectLike
|
2019-10-01 21:44:43 +02:00
|
|
|
|
*/
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$unpacked_atomic_array = $arg_type->getAtomicTypes()['array'];
|
2019-04-09 19:58:49 +02:00
|
|
|
|
|
2020-03-17 22:34:45 +01:00
|
|
|
|
if ($unpacked_atomic_array instanceof Type\Atomic\ObjectLike) {
|
2020-03-17 22:44:31 +01:00
|
|
|
|
if ($unpacked_atomic_array->is_list
|
|
|
|
|
&& isset($unpacked_atomic_array->properties[$argument_offset])
|
|
|
|
|
) {
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$arg_type = clone $unpacked_atomic_array->properties[$argument_offset];
|
2020-01-21 16:13:37 +01:00
|
|
|
|
} else {
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$arg_type = $unpacked_atomic_array->getGenericValueType();
|
2020-01-21 16:13:37 +01:00
|
|
|
|
}
|
2020-03-17 22:34:45 +01:00
|
|
|
|
} elseif ($unpacked_atomic_array instanceof Type\Atomic\TList) {
|
|
|
|
|
$arg_type = $unpacked_atomic_array->type_param;
|
2019-10-09 00:44:46 +02:00
|
|
|
|
} else {
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$arg_type = $unpacked_atomic_array->type_params[1];
|
2019-05-31 07:47:35 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($arg_type->getAtomicTypes() as $atomic_type) {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if (!$atomic_type->isIterable($codebase)) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
|
|
|
|
|
. ' expects array, ' . $atomic_type->getId() . ' provided',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value),
|
|
|
|
|
$cased_method_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2019-06-30 18:06:49 +02:00
|
|
|
|
|
|
|
|
|
continue;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
return;
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-31 07:47:35 +02:00
|
|
|
|
|
|
|
|
|
if (self::checkFunctionArgumentType(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$arg_type,
|
|
|
|
|
$fleshed_out_type,
|
|
|
|
|
$fleshed_out_signature_type,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value),
|
|
|
|
|
$arg->value,
|
|
|
|
|
$context,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
$function_param,
|
2019-08-04 16:37:36 +02:00
|
|
|
|
$arg->unpack,
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$unpacked_atomic_array,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
$function_is_pure,
|
2019-09-11 19:52:37 +02:00
|
|
|
|
$in_call_map,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
$function_location
|
2019-05-31 07:47:35 +02:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-02-03 22:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 00:18:23 +01:00
|
|
|
|
/**
|
2019-03-22 20:59:10 +01:00
|
|
|
|
* @return array<string, array<string, array{Type\Union}>>
|
2020-03-27 23:30:59 +01:00
|
|
|
|
* @param array<string, non-empty-array<string, array{Type\Union}>> $existing_template_types
|
2019-01-13 00:18:23 +01:00
|
|
|
|
*/
|
2020-03-28 22:18:21 +01:00
|
|
|
|
public static function getTemplateTypesForCall(
|
2020-03-27 23:30:59 +01:00
|
|
|
|
ClassLikeStorage $declaring_class_storage = null,
|
|
|
|
|
ClassLikeStorage $calling_class_storage = null,
|
|
|
|
|
array $existing_template_types = []
|
2019-01-13 00:18:23 +01:00
|
|
|
|
) : array {
|
2020-03-27 23:30:59 +01:00
|
|
|
|
$template_types = $existing_template_types;
|
2019-01-13 00:18:23 +01:00
|
|
|
|
|
2020-03-27 23:30:59 +01:00
|
|
|
|
if ($declaring_class_storage) {
|
2019-01-13 00:18:23 +01:00
|
|
|
|
if ($calling_class_storage
|
2020-03-27 23:30:59 +01:00
|
|
|
|
&& $declaring_class_storage !== $calling_class_storage
|
2019-01-13 00:18:23 +01:00
|
|
|
|
&& $calling_class_storage->template_type_extends
|
|
|
|
|
) {
|
2019-06-25 05:31:06 +02:00
|
|
|
|
foreach ($calling_class_storage->template_type_extends as $class_name => $type_map) {
|
2019-01-13 00:18:23 +01:00
|
|
|
|
foreach ($type_map as $template_name => $type) {
|
2020-03-27 23:30:59 +01:00
|
|
|
|
if (is_string($template_name) && $class_name === $declaring_class_storage->name) {
|
2019-03-16 16:15:25 +01:00
|
|
|
|
$output_type = null;
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($type->getAtomicTypes() as $atomic_type) {
|
2019-03-16 16:15:25 +01:00
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TTemplateParam
|
|
|
|
|
&& isset(
|
|
|
|
|
$calling_class_storage
|
|
|
|
|
->template_type_extends
|
2019-06-25 05:31:06 +02:00
|
|
|
|
[$atomic_type->defining_class]
|
|
|
|
|
[$atomic_type->param_name]
|
2019-03-16 16:15:25 +01:00
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$output_type_candidate = $calling_class_storage
|
2020-03-27 23:30:59 +01:00
|
|
|
|
->template_type_extends
|
|
|
|
|
[$atomic_type->defining_class]
|
|
|
|
|
[$atomic_type->param_name];
|
2020-03-15 16:14:06 +01:00
|
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
|
$output_type_candidate = $atomic_type->as;
|
2019-03-16 16:15:25 +01:00
|
|
|
|
} else {
|
|
|
|
|
$output_type_candidate = new Type\Union([$atomic_type]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$output_type) {
|
|
|
|
|
$output_type = $output_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$output_type = Type::combineUnionTypes(
|
|
|
|
|
$output_type_candidate,
|
|
|
|
|
$output_type
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-13 00:18:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 23:30:59 +01:00
|
|
|
|
$template_types[$template_name][$declaring_class_storage->name] = [$output_type];
|
2019-01-13 00:18:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 23:30:59 +01:00
|
|
|
|
} elseif ($declaring_class_storage->template_types) {
|
|
|
|
|
foreach ($declaring_class_storage->template_types as $template_name => $type_map) {
|
2020-03-25 04:40:01 +01:00
|
|
|
|
foreach ($type_map as $key => list($type)) {
|
|
|
|
|
$template_types[$template_name][$key] = [$type];
|
|
|
|
|
}
|
2019-01-13 00:18:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
|
foreach ($template_types as $key => $type_map) {
|
|
|
|
|
foreach ($type_map as $class => $type) {
|
|
|
|
|
$template_types[$key][$class][0] = clone $type[0];
|
|
|
|
|
}
|
2019-01-13 00:18:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $template_types;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-03-02 05:33:21 +01:00
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
|
* @param string $method_id
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-12-31 16:51:42 +01:00
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
protected static function checkArrayFunctionArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
Context $context,
|
2016-12-31 16:51:42 +01:00
|
|
|
|
array $args,
|
2019-01-15 23:53:23 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
bool $check_functions
|
2016-12-31 16:51:42 +01:00
|
|
|
|
) {
|
|
|
|
|
$closure_index = $method_id === 'array_map' ? 0 : 1;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
|
$array_arg_types = [];
|
|
|
|
|
|
|
|
|
|
foreach ($args as $i => $arg) {
|
|
|
|
|
if ($i === 0 && $method_id === 'array_map') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($i === 1 && $method_id === 'array_filter') {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 01:31:08 +02:00
|
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-09 00:44:46 +02:00
|
|
|
|
* @var ObjectLike|TArray|TList|null
|
2019-10-02 01:31:08 +02:00
|
|
|
|
*/
|
2020-01-14 03:27:09 +01:00
|
|
|
|
$array_arg_type = ($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
|
2020-01-04 18:20:26 +01:00
|
|
|
|
&& ($types = $arg_value_type->getAtomicTypes())
|
2019-10-01 21:44:43 +02:00
|
|
|
|
&& isset($types['array'])
|
|
|
|
|
? $types['array']
|
2016-12-31 16:51:42 +01:00
|
|
|
|
: null;
|
2017-04-08 00:30:15 +02:00
|
|
|
|
|
|
|
|
|
if ($array_arg_type instanceof ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
|
$array_arg_type = $array_arg_type->getGenericArrayType();
|
2017-04-08 00:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 00:44:46 +02:00
|
|
|
|
if ($array_arg_type instanceof TList) {
|
|
|
|
|
$array_arg_type = new TArray([Type::getInt(), $array_arg_type->type_param]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-08 00:30:15 +02:00
|
|
|
|
$array_arg_types[] = $array_arg_type;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
|
$closure_arg = isset($args[$closure_index]) ? $args[$closure_index] : null;
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$closure_arg_type = null;
|
|
|
|
|
|
|
|
|
|
if ($closure_arg) {
|
|
|
|
|
$closure_arg_type = $statements_analyzer->node_data->getType($closure_arg->value);
|
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
|
if ($closure_arg && $closure_arg_type) {
|
2017-04-03 18:36:49 +02:00
|
|
|
|
$min_closure_param_count = $max_closure_param_count = count($array_arg_types);
|
|
|
|
|
|
|
|
|
|
if ($method_id === 'array_filter') {
|
|
|
|
|
$max_closure_param_count = count($args) > 2 ? 2 : 1;
|
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($closure_arg_type->getAtomicTypes() as $closure_type) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
self::checkArrayFunctionClosureType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
$context,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
$closure_type,
|
|
|
|
|
$closure_arg,
|
|
|
|
|
$min_closure_param_count,
|
|
|
|
|
$max_closure_param_count,
|
2019-01-15 23:53:23 +01:00
|
|
|
|
$array_arg_types,
|
|
|
|
|
$check_functions
|
2019-06-30 18:06:49 +02:00
|
|
|
|
);
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param string $method_id
|
|
|
|
|
* @param int $min_closure_param_count
|
|
|
|
|
* @param int $max_closure_param_count [description]
|
|
|
|
|
* @param (TArray|null)[] $array_arg_types
|
|
|
|
|
*
|
2019-06-30 18:06:49 +02:00
|
|
|
|
* @return void
|
2018-03-02 05:33:21 +01:00
|
|
|
|
*/
|
|
|
|
|
private static function checkArrayFunctionClosureType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
Context $context,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
Type\Atomic $closure_type,
|
|
|
|
|
PhpParser\Node\Arg $closure_arg,
|
|
|
|
|
$min_closure_param_count,
|
|
|
|
|
$max_closure_param_count,
|
2019-01-15 23:53:23 +01:00
|
|
|
|
array $array_arg_types,
|
|
|
|
|
bool $check_functions
|
2018-03-02 05:33:21 +01:00
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2019-06-03 12:19:52 +02:00
|
|
|
|
if (!$closure_type instanceof Type\Atomic\TFn) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (!$closure_arg->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
&& !$closure_arg->value instanceof PhpParser\Node\Expr\Array_
|
2018-10-17 19:22:57 +02:00
|
|
|
|
&& !$closure_arg->value instanceof PhpParser\Node\Expr\BinaryOp\Concat
|
2018-03-02 05:33:21 +01:00
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$function_ids = self::getFunctionIdsFromCallableArg(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$closure_arg->value
|
|
|
|
|
);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$closure_types = [];
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
foreach ($function_ids as $function_id) {
|
2018-03-05 15:01:24 +01:00
|
|
|
|
$function_id = strtolower($function_id);
|
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (strpos($function_id, '::') !== false) {
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$function_id_parts = explode('&', $function_id);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
foreach ($function_id_parts as $function_id_part) {
|
|
|
|
|
list($callable_fq_class_name, $method_name) = explode('::', $function_id_part);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
switch ($callable_fq_class_name) {
|
|
|
|
|
case 'self':
|
|
|
|
|
case 'static':
|
|
|
|
|
case 'parent':
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$container_class = $statements_analyzer->getFQCLN();
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
if ($callable_fq_class_name === 'parent') {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$container_class = $statements_analyzer->getParentFQCLN();
|
2018-03-19 01:29:41 +01:00
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
if (!$container_class) {
|
|
|
|
|
continue 2;
|
|
|
|
|
}
|
2017-09-02 17:18:56 +02:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$callable_fq_class_name = $container_class;
|
|
|
|
|
}
|
2018-03-02 05:43:52 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
if (!$codebase->classOrInterfaceExists($callable_fq_class_name)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-03-02 05:52:11 +01:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$function_id_part = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$callable_fq_class_name,
|
|
|
|
|
$method_name
|
|
|
|
|
);
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
try {
|
|
|
|
|
$method_storage = $codebase->methods->getStorage($function_id_part);
|
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
|
// the method may not exist, but we're suppressing that issue
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 12:19:52 +02:00
|
|
|
|
$closure_types[] = new Type\Atomic\TFn(
|
2018-03-19 01:29:41 +01:00
|
|
|
|
'Closure',
|
|
|
|
|
$method_storage->params,
|
|
|
|
|
$method_storage->return_type ?: Type::getMixed()
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-03-02 05:33:21 +01:00
|
|
|
|
} else {
|
2019-01-15 23:53:23 +01:00
|
|
|
|
if (!$check_functions) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 23:54:50 +01:00
|
|
|
|
if (!$codebase->functions->functionExists($statements_analyzer, $function_id)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$function_storage = $codebase->functions->getStorage(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$function_id
|
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-31 00:51:59 +02:00
|
|
|
|
if (CallMap::inCallMap($function_id)) {
|
2019-06-15 22:10:48 +02:00
|
|
|
|
$callmap_callables = CallMap::getCallablesFromCallMap($function_id);
|
2018-03-31 00:51:59 +02:00
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
|
if ($callmap_callables === null) {
|
2018-03-31 00:51:59 +02:00
|
|
|
|
throw new \UnexpectedValueException('This should not happen');
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
|
$passing_callmap_callables = [];
|
2018-03-31 00:51:59 +02:00
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
|
foreach ($callmap_callables as $callmap_callable) {
|
2018-03-31 00:51:59 +02:00
|
|
|
|
$required_param_count = 0;
|
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
|
assert($callmap_callable->params !== null);
|
|
|
|
|
|
|
|
|
|
foreach ($callmap_callable->params as $i => $param) {
|
2018-03-31 00:51:59 +02:00
|
|
|
|
if (!$param->is_optional && !$param->is_variadic) {
|
|
|
|
|
$required_param_count = $i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($required_param_count <= $max_closure_param_count) {
|
2019-06-15 22:10:48 +02:00
|
|
|
|
$passing_callmap_callables[] = $callmap_callable;
|
2018-03-31 00:51:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
|
if ($passing_callmap_callables) {
|
|
|
|
|
foreach ($passing_callmap_callables as $passing_callmap_callable) {
|
|
|
|
|
$closure_types[] = $passing_callmap_callable;
|
2018-03-31 00:51:59 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-15 22:10:48 +02:00
|
|
|
|
$closure_types[] = $callmap_callables[0];
|
2018-03-31 00:51:59 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-03 12:19:52 +02:00
|
|
|
|
$closure_types[] = new Type\Atomic\TFn(
|
2018-03-31 00:51:59 +02:00
|
|
|
|
'Closure',
|
|
|
|
|
$function_storage->params,
|
|
|
|
|
$function_storage->return_type ?: Type::getMixed()
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$closure_types = [$closure_type];
|
|
|
|
|
}
|
2017-04-08 15:47:03 +02:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
foreach ($closure_types as $closure_type) {
|
2018-03-27 04:13:10 +02:00
|
|
|
|
if ($closure_type->params === null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 18:06:49 +02:00
|
|
|
|
self::checkArrayFunctionClosureTypeArgs(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
$context,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
$closure_type,
|
|
|
|
|
$closure_arg,
|
|
|
|
|
$min_closure_param_count,
|
|
|
|
|
$max_closure_param_count,
|
|
|
|
|
$array_arg_types
|
2019-06-30 18:06:49 +02:00
|
|
|
|
);
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-15 22:10:48 +02:00
|
|
|
|
* @param Type\Atomic\TFn|Type\Atomic\TCallable $closure_type
|
2018-03-02 05:33:21 +01:00
|
|
|
|
* @param string $method_id
|
|
|
|
|
* @param int $min_closure_param_count
|
2019-06-15 22:10:48 +02:00
|
|
|
|
* @param int $max_closure_param_count
|
2018-03-02 05:33:21 +01:00
|
|
|
|
* @param (TArray|null)[] $array_arg_types
|
|
|
|
|
*
|
2019-06-30 18:06:49 +02:00
|
|
|
|
* @return void
|
2018-03-02 05:33:21 +01:00
|
|
|
|
*/
|
|
|
|
|
private static function checkArrayFunctionClosureTypeArgs(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
Context $context,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$method_id,
|
2019-06-15 22:10:48 +02:00
|
|
|
|
Type\Atomic $closure_type,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
PhpParser\Node\Arg $closure_arg,
|
|
|
|
|
$min_closure_param_count,
|
|
|
|
|
$max_closure_param_count,
|
|
|
|
|
array $array_arg_types
|
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-03-02 05:33:21 +01:00
|
|
|
|
|
|
|
|
|
$closure_params = $closure_type->params;
|
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
|
if ($closure_params === null) {
|
|
|
|
|
throw new \UnexpectedValueException('Closure params should not be null here');
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$required_param_count = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($closure_params as $i => $param) {
|
2018-03-21 14:04:07 +01:00
|
|
|
|
if (!$param->is_optional && !$param->is_variadic) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$required_param_count = $i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 16:29:19 +01:00
|
|
|
|
if (count($closure_params) < $min_closure_param_count) {
|
|
|
|
|
$argument_text = $min_closure_param_count === 1 ? 'one argument' : $min_closure_param_count . ' arguments';
|
2018-03-02 06:07:19 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooManyArguments(
|
2018-03-02 06:07:19 +01:00
|
|
|
|
'The callable passed to ' . $method_id . ' will be called with ' . $argument_text . ', expecting '
|
2018-03-02 06:03:07 +01:00
|
|
|
|
. $required_param_count,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
2019-06-30 18:06:49 +02:00
|
|
|
|
|
|
|
|
|
return;
|
2018-03-02 05:33:21 +01:00
|
|
|
|
} elseif ($required_param_count > $max_closure_param_count) {
|
2018-03-05 16:29:19 +01:00
|
|
|
|
$argument_text = $max_closure_param_count === 1 ? 'one argument' : $max_closure_param_count . ' arguments';
|
2018-03-02 06:07:19 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooFewArguments(
|
2018-03-02 06:07:19 +01:00
|
|
|
|
'The callable passed to ' . $method_id . ' will be called with ' . $argument_text . ', expecting '
|
2018-03-02 06:03:07 +01:00
|
|
|
|
. $required_param_count,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
2019-06-30 18:06:49 +02:00
|
|
|
|
|
|
|
|
|
return;
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// abandon attempt to validate closure params if we have an extra arg for ARRAY_FILTER
|
|
|
|
|
if ($method_id === 'array_filter' && $max_closure_param_count > 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 23:13:46 +01:00
|
|
|
|
foreach ($closure_params as $i => $closure_param) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (!isset($array_arg_types[$i])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_arg_type = $array_arg_types[$i];
|
|
|
|
|
|
|
|
|
|
$input_type = $array_arg_type->type_params[1];
|
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
|
if ($input_type->hasMixed()) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$closure_param_type = $closure_param->type;
|
|
|
|
|
|
|
|
|
|
if (!$closure_param_type) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 23:13:46 +01:00
|
|
|
|
if ($method_id === 'array_map'
|
|
|
|
|
&& $i === 0
|
|
|
|
|
&& $closure_type->return_type
|
|
|
|
|
&& $closure_param_type->hasTemplate()
|
|
|
|
|
) {
|
2020-01-30 23:22:40 +01:00
|
|
|
|
$closure_param_type = clone $closure_param_type;
|
|
|
|
|
$closure_type->return_type = clone $closure_type->return_type;
|
|
|
|
|
|
2020-01-30 23:13:46 +01:00
|
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult(
|
|
|
|
|
[],
|
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ($closure_param_type->getTemplateTypes() as $template_type) {
|
|
|
|
|
$template_result->template_types[$template_type->param_name] = [
|
|
|
|
|
($template_type->defining_class) => [$template_type->as]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$closure_param_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
|
$closure_param_type,
|
|
|
|
|
$template_result,
|
|
|
|
|
$codebase,
|
2020-02-29 21:43:35 +01:00
|
|
|
|
$statements_analyzer,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
$input_type,
|
2020-03-01 18:27:18 +01:00
|
|
|
|
$i,
|
2020-01-30 23:13:46 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id ?: $context->calling_function_id
|
2020-01-30 23:13:46 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$closure_type->return_type->replaceTemplateTypesWithArgTypes(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result,
|
|
|
|
|
$codebase
|
2020-01-30 23:13:46 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results = new \Psalm\Internal\Analyzer\TypeComparisonResult();
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$type_match_found = TypeAnalyzer::isContainedBy(
|
|
|
|
|
$codebase,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$input_type,
|
|
|
|
|
$closure_param_type,
|
2020-01-31 19:58:56 +01:00
|
|
|
|
$input_type->ignore_nullable_issues,
|
|
|
|
|
$input_type->ignore_falsable_issues,
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results
|
2018-03-02 05:33:21 +01:00
|
|
|
|
);
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if ($union_comparison_results->type_coerced) {
|
|
|
|
|
if ($union_comparison_results->type_coerced_from_mixed) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new MixedArgumentTypeCoercion(
|
2018-03-02 05:33:21 +01:00
|
|
|
|
'First parameter of closure passed to function ' . $method_id . ' expects ' .
|
2018-05-20 23:43:02 +02:00
|
|
|
|
$closure_param_type->getId() . ', parent type ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
|
|
|
|
// keep soldiering on
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new ArgumentTypeCoercion(
|
2018-03-02 05:33:21 +01:00
|
|
|
|
'First parameter of closure passed to function ' . $method_id . ' expects ' .
|
2018-05-20 23:43:02 +02:00
|
|
|
|
$closure_param_type->getId() . ', parent type ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
|
|
|
|
// keep soldiering on
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if (!$union_comparison_results->type_coerced && !$type_match_found) {
|
2018-11-16 17:04:45 +01:00
|
|
|
|
$types_can_be_identical = TypeAnalyzer::canExpressionTypesBeIdentical(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$input_type,
|
|
|
|
|
$closure_param_type
|
|
|
|
|
);
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if ($union_comparison_results->scalar_type_match_found) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidScalarArgument(
|
|
|
|
|
'First parameter of closure passed to function ' . $method_id . ' expects ' .
|
2018-12-08 20:10:06 +01:00
|
|
|
|
$closure_param_type->getId() . ', ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
2019-02-14 22:55:54 +01:00
|
|
|
|
// fall through
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif ($types_can_be_identical) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyInvalidArgument(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'First parameter of closure passed to function ' . $method_id . ' expects '
|
|
|
|
|
. $closure_param_type->getId() . ', possibly different type '
|
|
|
|
|
. $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
2018-11-28 17:45:54 +01:00
|
|
|
|
// fall through
|
2018-03-02 05:33:21 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (IssueBuffer::accepts(
|
|
|
|
|
new InvalidArgument(
|
|
|
|
|
'First parameter of closure passed to function ' . $method_id . ' expects ' .
|
2018-05-20 23:43:02 +02:00
|
|
|
|
$closure_param_type->getId() . ', ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg),
|
|
|
|
|
$method_id
|
2018-03-02 05:33:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-02 05:33:21 +01:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-17 22:34:45 +01:00
|
|
|
|
* @param Type\Atomic\ObjectLike|Type\Atomic\TArray|Type\Atomic\TList $unpacked_atomic_array
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @return null|false
|
2016-11-01 19:14:35 +01:00
|
|
|
|
*/
|
2016-12-09 18:48:02 +01:00
|
|
|
|
public static function checkFunctionArgumentType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-01 19:14:35 +01:00
|
|
|
|
Type\Union $input_type,
|
|
|
|
|
Type\Union $param_type,
|
2019-05-21 17:51:41 +02:00
|
|
|
|
?Type\Union $signature_param_type,
|
2019-08-04 16:37:36 +02:00
|
|
|
|
?string $cased_method_id,
|
2019-05-31 07:47:35 +02:00
|
|
|
|
int $argument_offset,
|
2017-08-12 00:30:58 +02:00
|
|
|
|
CodeLocation $code_location,
|
2017-12-11 03:14:30 +01:00
|
|
|
|
PhpParser\Node\Expr $input_expr,
|
2017-12-15 22:48:06 +01:00
|
|
|
|
Context $context,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
FunctionLikeParameter $function_param,
|
|
|
|
|
bool $unpack,
|
2020-03-17 22:34:45 +01:00
|
|
|
|
?Type\Atomic $unpacked_atomic_array,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
bool $function_is_pure,
|
2019-09-11 19:52:37 +02:00
|
|
|
|
bool $in_call_map,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
CodeLocation $function_location
|
2016-11-01 19:14:35 +01:00
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2017-09-03 00:15:52 +02:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($param_type->hasMixed()) {
|
|
|
|
|
if ($codebase->infer_types_from_usage
|
|
|
|
|
&& !$input_type->hasMixed()
|
|
|
|
|
&& !$param_type->from_docblock
|
2019-12-10 20:58:26 +01:00
|
|
|
|
&& !$param_type->had_template
|
2019-05-31 07:47:35 +02:00
|
|
|
|
&& $cased_method_id
|
|
|
|
|
&& strpos($cased_method_id, '::')
|
|
|
|
|
&& !strpos($cased_method_id, '__')
|
2019-02-02 20:16:49 +01:00
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_parts = explode('::', $cased_method_id);
|
2019-05-31 07:47:35 +02:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier($method_parts[0], strtolower($method_parts[1]));
|
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
2019-05-31 16:37:26 +02:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($declaring_method_id) {
|
|
|
|
|
$id_lc = strtolower((string) $declaring_method_id);
|
2019-05-31 17:55:24 +02:00
|
|
|
|
if (!isset($codebase->analyzer->possible_method_param_types[$id_lc][$argument_offset])) {
|
|
|
|
|
$codebase->analyzer->possible_method_param_types[$id_lc][$argument_offset]
|
|
|
|
|
= clone $input_type;
|
2019-05-31 16:37:26 +02:00
|
|
|
|
} else {
|
2019-05-31 17:55:24 +02:00
|
|
|
|
$codebase->analyzer->possible_method_param_types[$id_lc][$argument_offset]
|
|
|
|
|
= Type::combineUnionTypes(
|
|
|
|
|
$codebase->analyzer->possible_method_param_types[$id_lc][$argument_offset],
|
|
|
|
|
clone $input_type,
|
|
|
|
|
$codebase
|
|
|
|
|
);
|
2019-05-31 16:37:26 +02:00
|
|
|
|
}
|
2019-05-31 07:47:35 +02:00
|
|
|
|
}
|
2017-09-03 00:15:52 +02:00
|
|
|
|
}
|
2019-05-31 07:47:35 +02:00
|
|
|
|
|
2019-08-06 16:33:21 +02:00
|
|
|
|
if ($cased_method_id) {
|
|
|
|
|
self::processTaintedness(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$code_location,
|
|
|
|
|
$function_location,
|
|
|
|
|
$function_param,
|
|
|
|
|
$input_type,
|
|
|
|
|
$function_is_pure
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
return null;
|
2017-09-03 00:15:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$method_identifier = $cased_method_id ? ' of ' . $cased_method_id : '';
|
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
|
if ($input_type->hasMixed()) {
|
2019-03-23 14:50:47 +01: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->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgument(
|
2020-04-12 07:26:11 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier
|
|
|
|
|
. ' cannot be ' . $input_type->getId() . ', expecting ' .
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$param_type,
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2016-11-01 19:14:35 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-11-01 19:14:35 +01:00
|
|
|
|
)) {
|
2018-06-28 23:05:50 +02:00
|
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 05:26:04 +02:00
|
|
|
|
if ($input_type->isMixed()) {
|
|
|
|
|
if (!$function_param->by_ref
|
|
|
|
|
&& !($function_param->is_variadic xor $unpack)
|
|
|
|
|
&& $cased_method_id !== 'echo'
|
2019-11-08 13:01:34 +01:00
|
|
|
|
&& $cased_method_id !== 'print'
|
2019-09-11 19:52:37 +02:00
|
|
|
|
&& (!$in_call_map || $context->strict_types)
|
2019-08-23 05:26:04 +02:00
|
|
|
|
) {
|
|
|
|
|
self::coerceValueAfterGatekeeperArgument(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$input_type,
|
|
|
|
|
false,
|
|
|
|
|
$input_expr,
|
|
|
|
|
$param_type,
|
|
|
|
|
$signature_param_type,
|
|
|
|
|
$context,
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$unpack,
|
|
|
|
|
$unpacked_atomic_array
|
2019-08-23 05:26:04 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-05-21 17:51:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 16:33:21 +02:00
|
|
|
|
if ($cased_method_id) {
|
|
|
|
|
self::processTaintedness(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$code_location,
|
|
|
|
|
$function_location,
|
|
|
|
|
$function_param,
|
|
|
|
|
$input_type,
|
|
|
|
|
$function_is_pure
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 05:26:04 +02:00
|
|
|
|
if ($input_type->isMixed()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-02 23:05:39 +01:00
|
|
|
|
if ($input_type->isNever()) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new NoValue(
|
|
|
|
|
'This function or method call never returns output',
|
|
|
|
|
$code_location
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2019-01-02 23:05:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:50:47 +01: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->incrementNonMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$param_type = TypeAnalyzer::simplifyUnionType(
|
|
|
|
|
$codebase,
|
2017-07-29 21:05:06 +02:00
|
|
|
|
$param_type
|
|
|
|
|
);
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results = new \Psalm\Internal\Analyzer\TypeComparisonResult();
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$type_match_found = TypeAnalyzer::isContainedBy(
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$codebase,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$input_type,
|
|
|
|
|
$param_type,
|
2016-12-11 19:48:11 +01:00
|
|
|
|
true,
|
2017-10-23 17:47:00 +02:00
|
|
|
|
true,
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results
|
2016-11-02 07:29:00 +01:00
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2019-07-10 18:12:51 +02:00
|
|
|
|
$replace_input_type = false;
|
|
|
|
|
|
|
|
|
|
if ($union_comparison_results->replacement_union_type) {
|
|
|
|
|
$replace_input_type = true;
|
|
|
|
|
$input_type = $union_comparison_results->replacement_union_type;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 16:33:21 +02:00
|
|
|
|
if ($cased_method_id) {
|
|
|
|
|
$old_input_type = $input_type;
|
|
|
|
|
|
|
|
|
|
self::processTaintedness(
|
|
|
|
|
$statements_analyzer,
|
2019-08-06 00:33:33 +02:00
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
2019-08-06 16:33:21 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$function_location,
|
|
|
|
|
$function_param,
|
|
|
|
|
$input_type,
|
|
|
|
|
$function_is_pure
|
2019-08-06 00:33:33 +02:00
|
|
|
|
);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
|
2019-08-06 16:33:21 +02:00
|
|
|
|
if ($old_input_type !== $input_type) {
|
2019-08-04 16:37:36 +02:00
|
|
|
|
$replace_input_type = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:45:40 +02:00
|
|
|
|
if ($type_match_found
|
|
|
|
|
&& $param_type->hasCallableType()
|
|
|
|
|
) {
|
|
|
|
|
$potential_method_ids = [];
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($input_type->getAtomicTypes() as $input_type_part) {
|
2019-04-17 21:45:40 +02:00
|
|
|
|
if ($input_type_part instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
$potential_method_id = TypeAnalyzer::getCallableMethodIdFromObjectLike(
|
2019-04-24 04:31:38 +02:00
|
|
|
|
$input_type_part,
|
2019-04-27 23:38:24 +02:00
|
|
|
|
$codebase,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2019-04-27 23:38:24 +02:00
|
|
|
|
$statements_analyzer->getFilePath()
|
2019-04-17 21:45:40 +02:00
|
|
|
|
);
|
|
|
|
|
|
2019-11-14 13:57:22 +01:00
|
|
|
|
if ($potential_method_id && $potential_method_id !== 'not-callable') {
|
2019-04-17 21:45:40 +02:00
|
|
|
|
$potential_method_ids[] = $potential_method_id;
|
|
|
|
|
}
|
2020-02-15 02:54:26 +01:00
|
|
|
|
} elseif ($input_type_part instanceof Type\Atomic\TLiteralString
|
|
|
|
|
&& strpos($input_type_part->value, '::')
|
|
|
|
|
) {
|
|
|
|
|
$parts = explode('::', $input_type_part->value);
|
|
|
|
|
$potential_method_ids[] = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$parts[0],
|
|
|
|
|
strtolower($parts[1])
|
|
|
|
|
);
|
2019-04-17 21:45:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($potential_method_ids as $potential_method_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$codebase->methods->methodExists(
|
|
|
|
|
$potential_method_id,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2020-02-15 02:54:26 +01:00
|
|
|
|
null,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$statements_analyzer->getFilePath()
|
|
|
|
|
);
|
2019-04-17 21:45:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-19 21:45:26 +01:00
|
|
|
|
if ($context->strict_types
|
|
|
|
|
&& !$input_type->hasArray()
|
|
|
|
|
&& !$param_type->from_docblock
|
|
|
|
|
&& $cased_method_id !== 'echo'
|
2019-11-08 13:01:34 +01:00
|
|
|
|
&& $cased_method_id !== 'print'
|
2019-05-29 20:22:15 +02:00
|
|
|
|
&& $cased_method_id !== 'sprintf'
|
2019-03-19 21:45:26 +01:00
|
|
|
|
) {
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results->scalar_type_match_found = false;
|
2018-08-28 23:42:39 +02:00
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if ($union_comparison_results->to_string_cast) {
|
|
|
|
|
$union_comparison_results->to_string_cast = false;
|
2018-08-28 23:42:39 +02:00
|
|
|
|
$type_match_found = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 05:26:04 +02:00
|
|
|
|
if ($union_comparison_results->type_coerced && !$input_type->hasMixed()) {
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if ($union_comparison_results->type_coerced_from_mixed) {
|
2017-11-19 19:42:48 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new MixedArgumentTypeCoercion(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', parent type ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2017-11-19 19:42:48 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-11-19 19:42:48 +01:00
|
|
|
|
)) {
|
2017-12-14 17:33:18 +01:00
|
|
|
|
// keep soldiering on
|
2017-11-19 19:42:48 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new ArgumentTypeCoercion(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', parent type ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2017-11-19 19:42:48 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-11-19 19:42:48 +01:00
|
|
|
|
)) {
|
2017-12-14 17:33:18 +01:00
|
|
|
|
// keep soldiering on
|
2017-11-19 19:42:48 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 13:01:34 +01:00
|
|
|
|
if ($union_comparison_results->to_string_cast && $cased_method_id !== 'echo' && $cased_method_id !== 'print') {
|
2016-12-29 06:14:06 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new ImplicitToStringCast(
|
2017-01-13 18:40:01 +01:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' .
|
2018-12-08 20:10:06 +01:00
|
|
|
|
$param_type->getId() . ', ' . $input_type->getId() . ' provided with a __toString method',
|
2016-12-29 06:14:06 +01:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-12-29 06:14:06 +01:00
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if (!$type_match_found && !$union_comparison_results->type_coerced) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$types_can_be_identical = TypeAnalyzer::canBeContainedBy(
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$codebase,
|
2018-04-27 23:10:57 +02:00
|
|
|
|
$input_type,
|
2017-04-08 15:28:02 +02:00
|
|
|
|
$param_type,
|
2018-04-27 23:10:57 +02:00
|
|
|
|
true,
|
|
|
|
|
true
|
2017-04-08 15:28:02 +02:00
|
|
|
|
);
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if ($union_comparison_results->scalar_type_match_found) {
|
2019-11-08 13:01:34 +01:00
|
|
|
|
if ($cased_method_id !== 'echo' && $cased_method_id !== 'print') {
|
2016-12-09 18:53:22 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidScalarArgument(
|
2017-01-13 18:40:01 +01:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' .
|
2019-01-05 06:15:53 +01:00
|
|
|
|
$param_type->getId() . ', ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2016-12-09 18:53:22 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-12-09 18:53:22 +01:00
|
|
|
|
)) {
|
2019-02-14 22:55:54 +01:00
|
|
|
|
// fall through
|
2016-12-09 18:53:22 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2017-04-08 15:43:58 +02:00
|
|
|
|
} elseif ($types_can_be_identical) {
|
2017-04-08 15:28:02 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyInvalidArgument(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', possibly different type ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2017-04-08 15:28:02 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-04-08 15:28:02 +02:00
|
|
|
|
)) {
|
2019-02-14 22:55:54 +01:00
|
|
|
|
// fall through
|
2017-04-08 15:28:02 +02:00
|
|
|
|
}
|
2019-03-19 21:12:32 +01:00
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', ' . $input_type->getId() . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2019-03-19 21:12:32 +01:00
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2019-02-14 22:55:54 +01:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($input_expr instanceof PhpParser\Node\Scalar\String_
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|| $input_expr instanceof PhpParser\Node\Expr\Array_
|
2018-10-17 19:22:57 +02:00
|
|
|
|
|| $input_expr instanceof PhpParser\Node\Expr\BinaryOp\Concat
|
2017-08-15 01:30:11 +02:00
|
|
|
|
) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($param_type->getAtomicTypes() as $param_type_part) {
|
2018-03-05 22:06:06 +01:00
|
|
|
|
if ($param_type_part instanceof TClassString
|
|
|
|
|
&& $input_expr instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-03-05 22:06:06 +01:00
|
|
|
|
$input_expr->value,
|
|
|
|
|
$code_location,
|
2020-02-11 22:39:33 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-05 22:06:06 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return;
|
2018-03-05 22:06:06 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif ($param_type_part instanceof TArray
|
|
|
|
|
&& $input_expr instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($param_type_part->type_params[1]->getAtomicTypes() as $param_array_type_part) {
|
2018-05-20 06:27:53 +02:00
|
|
|
|
if ($param_array_type_part instanceof TClassString) {
|
|
|
|
|
foreach ($input_expr->items as $item) {
|
|
|
|
|
if ($item && $item->value instanceof PhpParser\Node\Scalar\String_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-05-20 06:27:53 +02:00
|
|
|
|
$item->value->value,
|
|
|
|
|
$code_location,
|
2020-02-11 22:39:33 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-20 06:27:53 +02:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return;
|
2018-05-20 06:27:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-05 22:06:06 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($param_type_part instanceof TCallable) {
|
2017-08-15 01:30:11 +02:00
|
|
|
|
$function_ids = self::getFunctionIdsFromCallableArg(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-08-15 01:30:11 +02:00
|
|
|
|
$input_expr
|
|
|
|
|
);
|
2017-08-12 00:30:58 +02:00
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
|
foreach ($function_ids as $function_id) {
|
|
|
|
|
if (strpos($function_id, '::') !== false) {
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$function_id_parts = explode('&', $function_id);
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$non_existent_method_ids = [];
|
|
|
|
|
$has_valid_method = false;
|
2017-08-12 00:48:58 +02:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
foreach ($function_id_parts as $function_id_part) {
|
|
|
|
|
list($callable_fq_class_name, $method_name) = explode('::', $function_id_part);
|
2018-03-02 05:33:21 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
switch ($callable_fq_class_name) {
|
|
|
|
|
case 'self':
|
|
|
|
|
case 'static':
|
|
|
|
|
case 'parent':
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$container_class = $statements_analyzer->getFQCLN();
|
2018-03-02 05:33:21 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
if ($callable_fq_class_name === 'parent') {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$container_class = $statements_analyzer->getParentFQCLN();
|
2018-03-19 01:29:41 +01:00
|
|
|
|
}
|
2018-03-02 05:33:21 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
if (!$container_class) {
|
|
|
|
|
continue 2;
|
|
|
|
|
}
|
2018-03-02 05:52:11 +01:00
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$callable_fq_class_name = $container_class;
|
|
|
|
|
}
|
2018-03-02 05:33:21 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$callable_fq_class_name,
|
|
|
|
|
$code_location,
|
2020-02-11 22:39:33 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-19 01:29:41 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return;
|
2018-03-19 01:29:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$function_id_part = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$callable_fq_class_name,
|
|
|
|
|
strtolower($method_name)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$call_method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
|
$callable_fq_class_name,
|
|
|
|
|
'__call'
|
|
|
|
|
);
|
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
if (!$codebase->classOrInterfaceExists($callable_fq_class_name)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if (!$codebase->methods->methodExists($function_id_part)
|
|
|
|
|
&& !$codebase->methods->methodExists($call_method_id)
|
2018-03-21 05:13:57 +01:00
|
|
|
|
) {
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$non_existent_method_ids[] = $function_id_part;
|
|
|
|
|
} else {
|
|
|
|
|
$has_valid_method = true;
|
|
|
|
|
}
|
2018-03-02 05:43:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 13:58:05 +01:00
|
|
|
|
if (!$has_valid_method && !$param_type->hasString() && !$param_type->hasArray()) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (MethodAnalyzer::checkMethodExists(
|
|
|
|
|
$codebase,
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$non_existent_method_ids[0],
|
|
|
|
|
$code_location,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-19 01:29:41 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return;
|
2018-03-19 01:29:41 +01:00
|
|
|
|
}
|
2017-08-12 00:48:58 +02:00
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
} else {
|
2018-03-20 13:58:05 +01:00
|
|
|
|
if (!$param_type->hasString() && !$param_type->hasArray() && self::checkFunctionExists(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-01-03 03:23:48 +01:00
|
|
|
|
$function_id,
|
2018-02-25 17:13:00 +01:00
|
|
|
|
$code_location,
|
|
|
|
|
false
|
2018-01-03 03:23:48 +01:00
|
|
|
|
) === false
|
2017-08-12 00:48:58 +02:00
|
|
|
|
) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
return;
|
2017-08-12 00:48:58 +02:00
|
|
|
|
}
|
2017-08-12 00:30:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2019-11-08 13:01:34 +01:00
|
|
|
|
if (!$param_type->isNullable() && $cased_method_id !== 'echo' && $cased_method_id !== 'print') {
|
2018-05-20 19:14:31 +02:00
|
|
|
|
if ($input_type->isNull()) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new NullArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be null, ' .
|
2018-12-08 20:10:06 +01:00
|
|
|
|
'null value provided to parameter with type ' . $param_type->getId(),
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2018-05-20 19:14:31 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-20 19:14:31 +02:00
|
|
|
|
)) {
|
2019-06-30 18:06:49 +02:00
|
|
|
|
// fall through
|
2018-05-20 19:14:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($input_type->isNullable() && !$input_type->ignore_nullable_issues) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyNullArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be null, possibly ' .
|
|
|
|
|
'null value provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2018-05-20 19:14:31 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-20 19:14:31 +02:00
|
|
|
|
)) {
|
2018-11-28 17:45:54 +01:00
|
|
|
|
// fall through
|
2018-05-20 19:14:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($input_type->isFalsable()
|
|
|
|
|
&& !$param_type->hasBool()
|
|
|
|
|
&& !$param_type->hasScalar()
|
|
|
|
|
&& !$input_type->ignore_falsable_issues
|
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyFalseArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be false, possibly ' .
|
|
|
|
|
'false value provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$cased_method_id
|
2018-05-20 19:14:31 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-20 19:14:31 +02:00
|
|
|
|
)) {
|
2018-11-28 17:45:54 +01:00
|
|
|
|
// fall through
|
2018-05-20 19:14:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 05:26:04 +02:00
|
|
|
|
if (($type_match_found || $input_type->hasMixed())
|
2019-08-06 00:33:33 +02:00
|
|
|
|
&& !$function_param->by_ref
|
|
|
|
|
&& !($function_param->is_variadic xor $unpack)
|
2019-01-11 20:46:03 +01:00
|
|
|
|
&& $cased_method_id !== 'echo'
|
2019-11-08 13:01:34 +01:00
|
|
|
|
&& $cased_method_id !== 'print'
|
2019-09-11 19:52:37 +02:00
|
|
|
|
&& (!$in_call_map || $context->strict_types)
|
2017-12-15 22:48:06 +01:00
|
|
|
|
) {
|
2019-05-21 17:51:41 +02:00
|
|
|
|
self::coerceValueAfterGatekeeperArgument(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$input_type,
|
2019-07-10 18:12:51 +02:00
|
|
|
|
$replace_input_type,
|
2017-12-11 03:14:30 +01:00
|
|
|
|
$input_expr,
|
2019-05-21 17:51:41 +02:00
|
|
|
|
$param_type,
|
|
|
|
|
$signature_param_type,
|
|
|
|
|
$context,
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$unpack,
|
|
|
|
|
$unpacked_atomic_array
|
2017-12-11 03:14:30 +01:00
|
|
|
|
);
|
2019-05-21 17:51:41 +02:00
|
|
|
|
}
|
2017-12-11 03:14:30 +01:00
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-11 03:14:30 +01:00
|
|
|
|
|
2019-08-06 16:33:21 +02:00
|
|
|
|
private static function processTaintedness(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
string $cased_method_id,
|
|
|
|
|
int $argument_offset,
|
|
|
|
|
CodeLocation $code_location,
|
|
|
|
|
CodeLocation $function_location,
|
|
|
|
|
FunctionLikeParameter $function_param,
|
|
|
|
|
Type\Union &$input_type,
|
|
|
|
|
bool $function_is_pure
|
|
|
|
|
) : void {
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
|
|
if (!$codebase->taint) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$method_sink = Sink::getForMethodArgument(
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$cased_method_id,
|
2019-08-06 16:33:21 +02:00
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$code_location
|
|
|
|
|
);
|
|
|
|
|
|
2019-08-14 15:52:59 +02:00
|
|
|
|
$child_sink = null;
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
2019-10-14 23:10:30 +02:00
|
|
|
|
if (($function_param->sink || ($child_sink = $codebase->taint->hasPreviousSink($method_sink, $suffixes)))
|
2019-10-14 02:10:31 +02:00
|
|
|
|
&& !in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
2019-08-06 16:33:21 +02:00
|
|
|
|
&& $input_type->sources
|
|
|
|
|
) {
|
|
|
|
|
$all_possible_sinks = [];
|
|
|
|
|
|
|
|
|
|
foreach ($input_type->sources as $source) {
|
|
|
|
|
if ($codebase->taint->hasExistingSink($source)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$base_sink = new Sink(
|
|
|
|
|
$source->id,
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$source->label,
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$source->code_location
|
|
|
|
|
);
|
|
|
|
|
|
2019-08-14 15:52:59 +02:00
|
|
|
|
$base_sink->children = [$child_sink ?: $method_sink];
|
2019-08-14 06:47:57 +02:00
|
|
|
|
|
|
|
|
|
$all_possible_sinks[] = $base_sink;
|
2019-08-06 16:33:21 +02:00
|
|
|
|
|
|
|
|
|
if (strpos($source->id, '::') && strpos($source->id, '#')) {
|
|
|
|
|
list($fq_classlike_name, $method_name) = explode('::', $source->id);
|
2019-10-19 23:59:10 +02:00
|
|
|
|
list(, $cased_method_name) = explode('::', $source->label);
|
2019-08-06 16:33:21 +02:00
|
|
|
|
|
|
|
|
|
$method_name_parts = explode('#', $method_name);
|
2019-10-19 23:59:10 +02:00
|
|
|
|
list($cased_method_name) = explode('#', $cased_method_name);
|
2019-08-06 16:33:21 +02:00
|
|
|
|
|
|
|
|
|
$method_name = strtolower($method_name_parts[0]);
|
|
|
|
|
|
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_classlike_name);
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
foreach ($class_storage->dependent_classlikes as $dependent_classlike_lc => $_) {
|
|
|
|
|
$dependent_classlike_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
|
$dependent_classlike_lc
|
|
|
|
|
);
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$new_sink = Sink::getForMethodArgument(
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$dependent_classlike_lc . '::' . $method_name,
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$dependent_classlike_storage->name . '::' . $cased_method_name,
|
2019-08-06 16:33:21 +02:00
|
|
|
|
(int) $method_name_parts[1] - 1,
|
|
|
|
|
$code_location,
|
|
|
|
|
null
|
|
|
|
|
);
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
2019-08-14 15:52:59 +02:00
|
|
|
|
$new_sink->children = [$child_sink ?: $method_sink];
|
2019-08-14 06:47:57 +02:00
|
|
|
|
|
2019-08-14 15:52:59 +02:00
|
|
|
|
$new_sink->taint = $child_sink ? $child_sink->taint : $function_param->sink;
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
|
|
|
|
$all_possible_sinks[] = $new_sink;
|
2019-08-06 16:33:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($class_storage->overridden_method_ids[$method_name])) {
|
|
|
|
|
foreach ($class_storage->overridden_method_ids[$method_name] as $parent_method_id) {
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$new_sink = Sink::getForMethodArgument(
|
2020-02-15 02:54:26 +01:00
|
|
|
|
(string) $parent_method_id,
|
|
|
|
|
$codebase->methods->getCasedMethodId($parent_method_id),
|
2019-08-06 16:33:21 +02:00
|
|
|
|
(int) $method_name_parts[1] - 1,
|
|
|
|
|
$code_location,
|
|
|
|
|
null
|
|
|
|
|
);
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
2019-08-14 15:52:59 +02:00
|
|
|
|
$new_sink->taint = $child_sink ? $child_sink->taint : $function_param->sink;
|
|
|
|
|
$new_sink->children = [$child_sink ?: $method_sink];
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
|
|
|
|
$all_possible_sinks[] = $new_sink;
|
2019-08-06 16:33:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$codebase->taint->addSinks(
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$all_possible_sinks
|
2019-08-06 16:33:21 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 05:16:05 +02:00
|
|
|
|
if ($function_param->sink
|
|
|
|
|
&& $input_type->tainted
|
|
|
|
|
&& ($function_param->sink & $input_type->tainted)
|
|
|
|
|
&& $input_type->sources
|
|
|
|
|
) {
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$method_sink = Sink::getForMethodArgument(
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$cased_method_id,
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$code_location
|
|
|
|
|
);
|
|
|
|
|
|
2019-08-14 17:47:58 +02:00
|
|
|
|
$existing_sink = $codebase->taint->hasExistingSink($method_sink);
|
|
|
|
|
|
2019-08-08 20:17:28 +02:00
|
|
|
|
foreach ($input_type->sources as $input_source) {
|
2019-08-14 17:47:58 +02:00
|
|
|
|
$existing_source = $codebase->taint->hasExistingSource($input_source);
|
|
|
|
|
|
2019-08-08 20:17:28 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TaintedInput(
|
2019-08-14 17:47:58 +02:00
|
|
|
|
'in path ' . $codebase->taint->getPredecessorPath($existing_source ?: $input_source)
|
|
|
|
|
. ' out path ' . $codebase->taint->getSuccessorPath($existing_sink ?: $method_sink),
|
2019-08-08 20:17:28 +02:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
2019-08-06 16:33:21 +02:00
|
|
|
|
}
|
|
|
|
|
} elseif ($input_type->sources) {
|
|
|
|
|
if ($function_is_pure) {
|
|
|
|
|
$codebase->taint->addSpecialization(
|
|
|
|
|
strtolower($cased_method_id . '#' . ($argument_offset + 1)),
|
2019-08-06 23:29:44 +02:00
|
|
|
|
$function_location->file_name . ':' . $function_location->raw_file_start
|
2019-08-06 16:33:21 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($input_type->sources as $type_source) {
|
2019-08-14 15:52:59 +02:00
|
|
|
|
if (($previous_source = $codebase->taint->hasPreviousSource($type_source)) || $input_type->tainted) {
|
2019-08-14 06:47:57 +02:00
|
|
|
|
if ($function_is_pure) {
|
|
|
|
|
$method_source = Source::getForMethodArgument(
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$cased_method_id,
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$code_location,
|
|
|
|
|
$function_location
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$method_source = Source::getForMethodArgument(
|
2019-10-19 23:59:10 +02:00
|
|
|
|
$cased_method_id,
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
|
|
|
|
$code_location
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-14 23:10:30 +02:00
|
|
|
|
$method_source->taint = $input_type->tainted ?: 0;
|
|
|
|
|
|
2019-08-14 15:52:59 +02:00
|
|
|
|
$method_source->parents = [$previous_source ?: $type_source];
|
2019-08-14 06:47:57 +02:00
|
|
|
|
|
2019-08-06 16:33:21 +02:00
|
|
|
|
$codebase->taint->addSources(
|
2019-08-14 06:47:57 +02:00
|
|
|
|
[$method_source]
|
2019-08-06 16:33:21 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($input_type->tainted) {
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
'sources should exist for tainted var in '
|
|
|
|
|
. $code_location->getShortSummary()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($function_param->assert_untainted) {
|
|
|
|
|
$input_type = clone $input_type;
|
|
|
|
|
$input_type->tainted = null;
|
|
|
|
|
$input_type->sources = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 22:34:45 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param Type\Atomic\ObjectLike|Type\Atomic\TArray|Type\Atomic\TList $unpacked_atomic_array
|
|
|
|
|
*/
|
2019-05-21 17:51:41 +02:00
|
|
|
|
private static function coerceValueAfterGatekeeperArgument(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
Type\Union $input_type,
|
2019-07-10 18:12:51 +02:00
|
|
|
|
bool $input_type_changed,
|
2019-05-21 17:51:41 +02:00
|
|
|
|
PhpParser\Node\Expr $input_expr,
|
|
|
|
|
Type\Union $param_type,
|
|
|
|
|
?Type\Union $signature_param_type,
|
|
|
|
|
Context $context,
|
2020-03-17 22:34:45 +01:00
|
|
|
|
bool $unpack,
|
|
|
|
|
?Type\Atomic $unpacked_atomic_array
|
2019-05-21 17:51:41 +02:00
|
|
|
|
) : void {
|
2019-05-21 18:11:17 +02:00
|
|
|
|
if ($param_type->hasMixed()) {
|
2019-05-21 17:51:41 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-08 19:01:39 +01:00
|
|
|
|
|
2019-07-10 18:12:51 +02:00
|
|
|
|
if (!$input_type_changed && $param_type->from_docblock && !$input_type->hasMixed()) {
|
2019-06-26 06:14:06 +02:00
|
|
|
|
$input_type = clone $input_type;
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($param_type->getAtomicTypes() as $param_atomic_type) {
|
2019-05-21 18:11:17 +02:00
|
|
|
|
if ($param_atomic_type instanceof Type\Atomic\TGenericObject) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($input_type->getAtomicTypes() as $input_atomic_type) {
|
2019-05-21 18:11:17 +02:00
|
|
|
|
if ($input_atomic_type instanceof Type\Atomic\TGenericObject
|
|
|
|
|
&& $input_atomic_type->value === $param_atomic_type->value
|
|
|
|
|
) {
|
|
|
|
|
foreach ($input_atomic_type->type_params as $i => $type_param) {
|
2019-07-10 20:03:13 +02:00
|
|
|
|
if ($type_param->isEmpty() && isset($param_atomic_type->type_params[$i])) {
|
2019-05-21 18:11:17 +02:00
|
|
|
|
$input_type_changed = true;
|
2019-06-26 06:14:06 +02:00
|
|
|
|
|
2019-10-09 16:04:34 +02:00
|
|
|
|
/** @psalm-suppress PropertyTypeCoercion */
|
2019-05-21 18:11:17 +02:00
|
|
|
|
$input_atomic_type->type_params[$i] = clone $param_atomic_type->type_params[$i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$input_type_changed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
|
$input_expr,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
2017-12-11 03:14:30 +01:00
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
if ($var_id) {
|
2019-08-04 16:37:36 +02:00
|
|
|
|
$was_cloned = false;
|
2019-06-26 06:14:06 +02:00
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
if ($input_type->isNullable() && !$param_type->isNullable()) {
|
2019-08-04 16:37:36 +02:00
|
|
|
|
$input_type = clone $input_type;
|
|
|
|
|
$was_cloned = true;
|
2019-05-21 17:51:41 +02:00
|
|
|
|
$input_type->removeType('null');
|
|
|
|
|
}
|
2017-12-18 05:22:26 +01:00
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
if ($input_type->getId() === $param_type->getId()) {
|
2019-08-04 16:37:36 +02:00
|
|
|
|
if (!$was_cloned) {
|
|
|
|
|
$was_cloned = true;
|
|
|
|
|
$input_type = clone $input_type;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
$input_type->from_docblock = false;
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($input_type->getAtomicTypes() as $atomic_type) {
|
2019-05-21 17:51:41 +02:00
|
|
|
|
$atomic_type->from_docblock = false;
|
2019-04-09 19:58:49 +02:00
|
|
|
|
}
|
2019-06-26 06:14:06 +02:00
|
|
|
|
} elseif ($input_type->hasMixed() && $signature_param_type) {
|
2019-08-04 16:37:36 +02:00
|
|
|
|
$was_cloned = true;
|
2019-05-21 17:51:41 +02:00
|
|
|
|
$input_type = clone $signature_param_type;
|
2019-05-21 18:59:06 +02:00
|
|
|
|
|
|
|
|
|
if ($input_type->isNullable()) {
|
|
|
|
|
$input_type->ignore_nullable_issues = true;
|
|
|
|
|
}
|
2019-05-21 17:51:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 21:58:05 +01:00
|
|
|
|
if ($context->inside_conditional) {
|
|
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 16:37:36 +02:00
|
|
|
|
if ($was_cloned) {
|
|
|
|
|
$context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer);
|
|
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
|
if ($unpack) {
|
2020-03-17 22:34:45 +01:00
|
|
|
|
if ($unpacked_atomic_array instanceof Type\Atomic\TList) {
|
|
|
|
|
$unpacked_atomic_array = clone $unpacked_atomic_array;
|
|
|
|
|
$unpacked_atomic_array->type_param = $input_type;
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = new Type\Union([$unpacked_atomic_array]);
|
|
|
|
|
} elseif ($unpacked_atomic_array instanceof Type\Atomic\TArray) {
|
|
|
|
|
$unpacked_atomic_array = clone $unpacked_atomic_array;
|
|
|
|
|
/** @psalm-suppress PropertyTypeCoercion */
|
|
|
|
|
$unpacked_atomic_array->type_params[1] = $input_type;
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = new Type\Union([$unpacked_atomic_array]);
|
|
|
|
|
} elseif ($unpacked_atomic_array instanceof Type\Atomic\ObjectLike
|
|
|
|
|
&& $unpacked_atomic_array->is_list
|
|
|
|
|
) {
|
|
|
|
|
$unpacked_atomic_array = $unpacked_atomic_array->getList();
|
|
|
|
|
$unpacked_atomic_array->type_param = $input_type;
|
2017-12-11 03:14:30 +01:00
|
|
|
|
|
2020-03-17 22:34:45 +01:00
|
|
|
|
$context->vars_in_scope[$var_id] = new Type\Union([$unpacked_atomic_array]);
|
|
|
|
|
} else {
|
|
|
|
|
$context->vars_in_scope[$var_id] = new Type\Union([
|
|
|
|
|
new TArray([
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
$input_type
|
|
|
|
|
]),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$context->vars_in_scope[$var_id] = $input_type;
|
|
|
|
|
}
|
2019-05-21 17:51:41 +02:00
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$method_ids[] = $method_id;
|
2017-08-15 01:30:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $method_ids;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-01 19:14:35 +01:00
|
|
|
|
* @param 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
|
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
|
protected 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)) {
|
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;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$arg_var_id = ExpressionAnalyzer::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(
|
2019-12-08 22:35:56 +01:00
|
|
|
|
\spl_object_id($conditional),
|
|
|
|
|
$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(
|
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
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
|
// 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,
|
2019-12-29 00:37:55 +01:00
|
|
|
|
($statements_analyzer->getTemplateTypeMap() ?: []) + $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-02-09 05:01:45 +01:00
|
|
|
|
if ($first_appearance = $statements_analyzer->getFirstAppearance($var_id)) {
|
|
|
|
|
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-04-08 18:37:38 +02:00
|
|
|
|
$union_comparison_result = new \Psalm\Internal\Analyzer\TypeComparisonResult();
|
|
|
|
|
|
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-04-07 06:13:56 +02:00
|
|
|
|
if (!TypeAnalyzer::isContainedBy(
|
|
|
|
|
$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-04-13 04:19:49 +02:00
|
|
|
|
'Type ' . $lower_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $upper_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-04-13 04:19:49 +02:00
|
|
|
|
'Type ' . $lower_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $upper_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-04-13 04:19:49 +02:00
|
|
|
|
'Type ' . $lower_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $upper_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-04-13 04:19:49 +02:00
|
|
|
|
'Type ' . $lower_bound_type->getId() . ' should be a subtype of '
|
|
|
|
|
. $upper_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
|
|
|
|
}
|