2016-11-01 19:14:35 +01:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\MethodAnalyzer;
|
|
|
|
|
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;
|
2018-11-18 18:41:47 +01:00
|
|
|
|
use Psalm\Internal\Type\TypeCombination;
|
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;
|
2017-11-19 19:42:48 +01:00
|
|
|
|
use Psalm\Issue\MixedTypeCoercion;
|
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;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
use Psalm\Issue\TooFewArguments;
|
|
|
|
|
use Psalm\Issue\TooManyArguments;
|
|
|
|
|
use Psalm\Issue\TypeCoercion;
|
|
|
|
|
use Psalm\Issue\UndefinedFunction;
|
|
|
|
|
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;
|
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;
|
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
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($method_name);
|
|
|
|
|
|
2017-12-23 01:01:59 +01:00
|
|
|
|
if ($method_id !== $source->getMethodId()) {
|
2018-01-24 19:38:53 +01:00
|
|
|
|
if ($context->collect_initializations) {
|
|
|
|
|
if (isset($context->initialized_methods[$method_id])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($context->initialized_methods === null) {
|
|
|
|
|
$context->initialized_methods = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->initialized_methods[$method_id] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$project_analyzer->getMethodMutations($method_id, $context);
|
2017-12-23 01:01:59 +01:00
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
} elseif ($context->collect_initializations &&
|
|
|
|
|
$context->self &&
|
|
|
|
|
(
|
|
|
|
|
$context->self === $fq_class_name ||
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$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
|
|
|
|
) {
|
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($method_name);
|
|
|
|
|
|
2019-01-09 14:35:53 +01:00
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
|
|
if (!$declaring_method_id) {
|
|
|
|
|
if (isset($context->vars_in_scope['$this'])) {
|
|
|
|
|
foreach ($context->vars_in_scope['$this']->getTypes() as $atomic_type) {
|
|
|
|
|
if ($atomic_type instanceof TNamedObject) {
|
|
|
|
|
$fq_class_name = $atomic_type->value;
|
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($method_name);
|
|
|
|
|
|
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
|
|
if ($declaring_method_id) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$atomic_type->extra_types) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($atomic_type->extra_types as $intersection_type) {
|
|
|
|
|
if ($intersection_type instanceof TNamedObject) {
|
|
|
|
|
$fq_class_name = $intersection_type->value;
|
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($method_name);
|
|
|
|
|
|
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
|
|
if ($declaring_method_id) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$declaring_method_id) {
|
|
|
|
|
throw new \UnexpectedValueException('Could not find declaring method for ' . $method_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
2018-01-24 19:11:23 +01:00
|
|
|
|
if (isset($context->initialized_methods[$declaring_method_id])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($context->initialized_methods === null) {
|
|
|
|
|
$context->initialized_methods = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->initialized_methods[$declaring_method_id] = true;
|
|
|
|
|
|
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
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if ($class_analyzer instanceof ClassLikeAnalyzer &&
|
2018-11-06 03:57:36 +01:00
|
|
|
|
($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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$class_analyzer->getMethodMutations(strtolower($method_name), $context);
|
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 string|null $method_id
|
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2019-01-10 18:13:49 +01:00
|
|
|
|
* @param array<string, array{Type\Union, ?string}>|null &$generic_params
|
2017-02-10 02:35:17 +01:00
|
|
|
|
* @param Context $context
|
|
|
|
|
* @param CodeLocation $code_location
|
2018-11-11 18:01:14 +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(
|
|
|
|
|
$method_id,
|
|
|
|
|
array $args,
|
2017-04-28 06:31:55 +02:00
|
|
|
|
&$generic_params,
|
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
|
2018-11-11 18:19:53 +01:00
|
|
|
|
? FunctionLikeAnalyzer::getMethodParamsById($codebase, $method_id, $args)
|
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,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$method_id,
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$method_id || $method_params === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list($fq_class_name, $method_name) = explode('::', $method_id);
|
|
|
|
|
|
2018-12-21 17:32:44 +01:00
|
|
|
|
$fq_class_name = $codebase->classlikes->getUnAliasedName($fq_class_name);
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (isset($class_storage->declaring_method_ids[strtolower($method_name)])) {
|
|
|
|
|
$declaring_method_id = $class_storage->declaring_method_ids[strtolower($method_name)];
|
|
|
|
|
|
|
|
|
|
list($declaring_fq_class_name, $declaring_method_name) = explode('::', $declaring_method_id);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($declaring_class_storage->methods[strtolower($declaring_method_name)])) {
|
|
|
|
|
throw new \UnexpectedValueException('Storage should not be empty here');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$method_storage = $declaring_class_storage->methods[strtolower($declaring_method_name)];
|
2018-06-22 07:13:49 +02:00
|
|
|
|
|
|
|
|
|
if ($context->collect_exceptions) {
|
|
|
|
|
$context->possibly_thrown_exceptions += $method_storage->throws;
|
|
|
|
|
}
|
2017-11-02 20:07:39 +01:00
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
|
|
|
|
if (!$class_storage->user_defined) {
|
|
|
|
|
// check again after we've processed args
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$method_params = FunctionLikeAnalyzer::getMethodParamsById(
|
2018-11-11 18:19:53 +01:00
|
|
|
|
$codebase,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$method_id,
|
2017-07-29 21:05:06 +02:00
|
|
|
|
$args
|
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,
|
|
|
|
|
$generic_params,
|
|
|
|
|
$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
|
|
|
|
}
|
|
|
|
|
|
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
|
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
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
|
protected static function checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
array $args,
|
2017-05-05 03:57:26 +02:00
|
|
|
|
$function_params,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$method_id,
|
2016-12-07 20:13:39 +01:00
|
|
|
|
Context $context
|
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
|
|
|
|
|
if ($method_id && in_array($method_id, ['array_push', 'array_unshift'], true) && $function_params) {
|
|
|
|
|
$array_arg = $args[0]->value;
|
2017-01-16 18:39:38 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$array_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
if (isset($array_arg->inferredType) && $array_arg->inferredType->hasArray()) {
|
|
|
|
|
/** @var TArray|ObjectLike */
|
2018-01-09 21:05:48 +01:00
|
|
|
|
$array_type = $array_arg->inferredType->getTypes()['array'];
|
2017-09-02 17:18:56 +02:00
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
if ($array_type instanceof ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
|
$array_type = $array_type->getGenericArrayType();
|
2017-10-28 19:56:29 +02:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$by_ref_type = new Type\Union([clone $array_type]);
|
|
|
|
|
|
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
|
if ($argument_offset === 0) {
|
|
|
|
|
continue;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$arg->value,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
|
if (!isset($arg->value->inferredType) || $arg->value->inferredType->hasMixed()) {
|
2018-04-20 23:14:38 +02:00
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
new Type\Union([new TArray([Type::getInt(), Type::getMixed()])])
|
|
|
|
|
);
|
|
|
|
|
} elseif ($arg->unpack) {
|
|
|
|
|
if ($arg->value->inferredType->hasArray()) {
|
|
|
|
|
/** @var Type\Atomic\TArray|Type\Atomic\ObjectLike */
|
|
|
|
|
$array_atomic_type = $arg->value->inferredType->getTypes()['array'];
|
|
|
|
|
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
$array_atomic_type = $array_atomic_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
new Type\Union(
|
2017-10-28 19:56:29 +02:00
|
|
|
|
[
|
2018-04-20 23:14:38 +02:00
|
|
|
|
new TArray(
|
|
|
|
|
[
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
clone $array_atomic_type->type_params[1]
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
new Type\Union(
|
|
|
|
|
[
|
|
|
|
|
new TArray(
|
|
|
|
|
[
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
clone $arg->value->inferredType
|
2017-10-28 19:56:29 +02:00
|
|
|
|
]
|
2018-04-20 23:14:38 +02:00
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$array_arg,
|
|
|
|
|
$by_ref_type,
|
2017-10-28 21:33:29 +02:00
|
|
|
|
$context,
|
|
|
|
|
false
|
2017-10-28 19:56:29 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 04:33:31 +02:00
|
|
|
|
if ($method_id && $method_id === 'array_splice' && $function_params && count($args) > 1) {
|
|
|
|
|
$array_arg = $args[0]->value;
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$array_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$offset_arg = $args[1]->value;
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$offset_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 22:12:07 +02:00
|
|
|
|
if (!isset($args[2])) {
|
2018-08-09 04:33:31 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$length_arg = $args[2]->value;
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$length_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 22:12:07 +02:00
|
|
|
|
if (!isset($args[3])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$replacement_arg = $args[3]->value;
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$replacement_arg,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-14 17:51:17 +02:00
|
|
|
|
if (isset($replacement_arg->inferredType)
|
|
|
|
|
&& !$replacement_arg->inferredType->hasArray()
|
|
|
|
|
&& $replacement_arg->inferredType->hasString()
|
|
|
|
|
&& $replacement_arg->inferredType->isSingle()
|
|
|
|
|
) {
|
|
|
|
|
$replacement_arg->inferredType = new Type\Union([
|
|
|
|
|
new Type\Atomic\TArray([Type::getInt(), $replacement_arg->inferredType])
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 04:33:31 +02:00
|
|
|
|
if (isset($array_arg->inferredType)
|
|
|
|
|
&& $array_arg->inferredType->hasArray()
|
|
|
|
|
&& isset($replacement_arg->inferredType)
|
|
|
|
|
&& $replacement_arg->inferredType->hasArray()
|
|
|
|
|
) {
|
|
|
|
|
/** @var TArray|ObjectLike */
|
|
|
|
|
$array_type = $array_arg->inferredType->getTypes()['array'];
|
|
|
|
|
|
|
|
|
|
if ($array_type instanceof ObjectLike) {
|
|
|
|
|
$array_type = $array_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var TArray|ObjectLike */
|
|
|
|
|
$replacement_array_type = $replacement_arg->inferredType->getTypes()['array'];
|
|
|
|
|
|
|
|
|
|
if ($replacement_array_type instanceof ObjectLike) {
|
|
|
|
|
$replacement_array_type = $replacement_array_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 18:41:47 +01:00
|
|
|
|
$by_ref_type = TypeCombination::combineTypes([$array_type, $replacement_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,
|
2018-08-09 04:33:31 +02:00
|
|
|
|
$array_arg,
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
$context,
|
|
|
|
|
false
|
|
|
|
|
);
|
2018-08-14 17:51:17 +02:00
|
|
|
|
|
|
|
|
|
return;
|
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,
|
2018-08-14 17:51:17 +02:00
|
|
|
|
$array_arg,
|
|
|
|
|
Type::getArray(),
|
|
|
|
|
$context,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
|
2018-08-09 04:33:31 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
|
if ($function_params !== null) {
|
|
|
|
|
$by_ref = $argument_offset < count($function_params)
|
|
|
|
|
? $function_params[$argument_offset]->by_ref
|
|
|
|
|
: $last_param && $last_param->is_variadic && $last_param->by_ref;
|
|
|
|
|
|
|
|
|
|
$by_ref_type = null;
|
|
|
|
|
|
|
|
|
|
if ($by_ref && $last_param) {
|
|
|
|
|
if ($argument_offset < count($function_params)) {
|
|
|
|
|
$by_ref_type = $function_params[$argument_offset]->type;
|
|
|
|
|
} else {
|
|
|
|
|
$by_ref_type = $last_param->type;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
|
|
|
|
$by_ref_type = $by_ref_type ? clone $by_ref_type : Type::getMixed();
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2017-01-16 18:39:38 +01:00
|
|
|
|
|
2018-01-12 05:18:13 +01:00
|
|
|
|
if ($by_ref
|
|
|
|
|
&& $by_ref_type
|
2018-06-27 05:11:16 +02:00
|
|
|
|
&& !($arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ConstFetch
|
2018-01-12 05:18:13 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
|
2019-01-11 20:57:21 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Assign
|
2018-01-12 05:18:13 +01:00
|
|
|
|
)
|
|
|
|
|
) {
|
2018-11-21 18:38:43 +01:00
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
|
$arg->value,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$builtin_array_functions = [
|
|
|
|
|
'shuffle', 'sort', 'rsort', 'usort', 'ksort', 'asort',
|
|
|
|
|
'krsort', 'arsort', 'natcasesort', 'natsort', 'reset',
|
|
|
|
|
'end', 'next', 'prev', 'array_pop', 'array_shift',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (($var_id && isset($context->vars_in_scope[$var_id]))
|
|
|
|
|
|| ($method_id
|
|
|
|
|
&& in_array(
|
|
|
|
|
$method_id,
|
|
|
|
|
$builtin_array_functions,
|
|
|
|
|
true
|
|
|
|
|
))
|
2017-10-28 19:56:29 +02:00
|
|
|
|
) {
|
2018-11-21 18:38:43 +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,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$arg->value,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-11-21 18:38:43 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-11-21 18:38:43 +01:00
|
|
|
|
// special handling for array sort
|
|
|
|
|
if ($argument_offset === 0
|
|
|
|
|
&& $method_id
|
|
|
|
|
&& in_array(
|
|
|
|
|
$method_id,
|
|
|
|
|
$builtin_array_functions,
|
|
|
|
|
true
|
|
|
|
|
)
|
|
|
|
|
) {
|
2017-11-20 03:36:09 +01:00
|
|
|
|
if (in_array($method_id, ['array_pop', 'array_shift'], true)) {
|
2018-11-21 18:38:43 +01:00
|
|
|
|
self::handleByRefArrayAdjustment($statements_analyzer, $arg, $context);
|
2017-11-20 05:46:22 +01:00
|
|
|
|
|
|
|
|
|
continue;
|
2017-11-20 03:36:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
// noops
|
2018-09-04 19:14:44 +02:00
|
|
|
|
if (in_array($method_id, ['reset', 'end', 'next', 'prev', 'ksort'], true)) {
|
2017-10-28 19:56:29 +02:00
|
|
|
|
continue;
|
2017-09-02 17:18:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
|
if (isset($arg->value->inferredType)
|
|
|
|
|
&& $arg->value->inferredType->hasArray()
|
|
|
|
|
) {
|
|
|
|
|
/** @var TArray|ObjectLike */
|
2018-01-09 21:05:48 +01:00
|
|
|
|
$array_type = $arg->value->inferredType->getTypes()['array'];
|
2017-10-28 19:56:29 +02:00
|
|
|
|
|
|
|
|
|
if ($array_type instanceof ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
|
$array_type = $array_type->getGenericArrayType();
|
2017-10-28 19:56:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_array($method_id, ['shuffle', 'sort', 'rsort', 'usort'], true)) {
|
2017-11-09 05:14:27 +01:00
|
|
|
|
$tvalue = $array_type->type_params[1];
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$by_ref_type = new Type\Union([new TArray([Type::getInt(), clone $tvalue])]);
|
|
|
|
|
} else {
|
|
|
|
|
$by_ref_type = new Type\Union([clone $array_type]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$arg->value,
|
|
|
|
|
$by_ref_type,
|
2017-10-28 21:33:29 +02:00
|
|
|
|
$context,
|
|
|
|
|
false
|
2017-10-28 19:56:29 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-01-16 18:39:38 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2017-10-28 22:26:45 +02:00
|
|
|
|
if ($method_id === 'socket_select') {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-10-28 22:26:45 +02:00
|
|
|
|
$arg->value,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
} else {
|
2018-04-19 18:16:00 +02:00
|
|
|
|
$toggled_class_exists = false;
|
|
|
|
|
|
|
|
|
|
if ($method_id === 'class_exists'
|
|
|
|
|
&& $argument_offset === 0
|
|
|
|
|
&& !$context->inside_class_exists
|
|
|
|
|
) {
|
|
|
|
|
$context->inside_class_exists = true;
|
|
|
|
|
$toggled_class_exists = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2018-06-17 05:40:25 +02:00
|
|
|
|
|
|
|
|
|
if ($context->collect_references
|
|
|
|
|
&& ($arg->value instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PreInc
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\PreDec)
|
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2018-06-17 05:40:25 +02:00
|
|
|
|
$arg->value->var,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
2018-06-17 05:40:25 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$context->hasVariable($var_id, $statements_analyzer);
|
2018-06-17 05:40:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 18:16:00 +02:00
|
|
|
|
|
|
|
|
|
if ($toggled_class_exists) {
|
|
|
|
|
$context->inside_class_exists = false;
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
} else {
|
2019-01-07 18:24:44 +01:00
|
|
|
|
// there are a bunch of things we want to evaluate anyway
|
2018-06-27 05:11:16 +02:00
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|
2019-01-07 18:24:44 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
|
2018-05-09 00:11:10 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|
&& $arg->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
) {
|
|
|
|
|
$var_id = '$' . $arg->value->name->name;
|
2017-10-28 19:56:29 +02:00
|
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$arg->value,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
2017-10-28 19:56:29 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
|
if ($var_id) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (!$context->hasVariable($var_id, $statements_analyzer)
|
2018-02-12 02:56:34 +01:00
|
|
|
|
|| $context->vars_in_scope[$var_id]->isNull()
|
2018-01-29 00:13:38 +01:00
|
|
|
|
) {
|
2018-02-12 02:56:34 +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
|
2018-11-11 18:01:14 +01:00
|
|
|
|
&& !$statements_analyzer->hasVariable($var_id)
|
2018-02-12 02:56:34 +01:00
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$location = new CodeLocation($statements_analyzer, $arg->value);
|
|
|
|
|
$statements_analyzer->registerVariable(
|
2018-02-12 02:56:34 +01:00
|
|
|
|
$var_id,
|
|
|
|
|
$location,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->registerVariableUses([$location->getHash() => $location]);
|
2018-02-12 02:56:34 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$context->removeVarFromConflictingClauses(
|
2017-10-28 19:56:29 +02:00
|
|
|
|
$var_id,
|
2018-02-12 02:56:34 +01:00
|
|
|
|
$context->vars_in_scope[$var_id],
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer
|
2017-10-28 19:56:29 +02:00
|
|
|
|
);
|
2018-01-29 00:13:38 +01:00
|
|
|
|
|
2018-05-29 11:08:56 +02:00
|
|
|
|
foreach ($context->vars_in_scope[$var_id]->getTypes() as $type) {
|
2018-02-12 02:56:34 +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(
|
2019-01-05 06:15:53 +01:00
|
|
|
|
[Type::getArrayKey(), Type::getMixed()]
|
2018-02-12 02:56:34 +01:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
$array_atomic_types = $array_type->getTypes();
|
|
|
|
|
|
|
|
|
|
foreach ($array_atomic_types as $array_atomic_type) {
|
|
|
|
|
if ($array_atomic_type instanceof ObjectLike) {
|
|
|
|
|
$generic_array_type = $array_atomic_type->getGenericArrayType();
|
|
|
|
|
|
|
|
|
|
if ($generic_array_type instanceof TNonEmptyArray) {
|
|
|
|
|
if (!$context->inside_loop && $generic_array_type->count !== null) {
|
|
|
|
|
if ($generic_array_type->count === 0) {
|
|
|
|
|
$generic_array_type = new TArray(
|
|
|
|
|
[
|
|
|
|
|
new Type\Union([new TEmpty]),
|
|
|
|
|
new Type\Union([new TEmpty]),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$generic_array_type->count--;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$generic_array_type = new TArray($generic_array_type->type_params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_type->addType($generic_array_type);
|
|
|
|
|
} elseif ($array_atomic_type instanceof TNonEmptyArray) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_type->addType($array_atomic_type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
* @param string|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
|
2019-01-10 18:13:49 +01:00
|
|
|
|
* @param array<string, array{Type\Union, ?string}>|null $generic_params
|
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
|
|
|
|
|
*/
|
2018-02-02 17:26:55 +01:00
|
|
|
|
protected 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,
|
|
|
|
|
&$generic_params,
|
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
|
|
|
|
) {
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$in_call_map = $method_id ? CallMap::inCallMap($method_id) : false;
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
$cased_method_id = $method_id;
|
|
|
|
|
|
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) {
|
|
|
|
|
if ($in_call_map || !strpos($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 {
|
|
|
|
|
$fq_class_name = explode('::', $method_id)[0];
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$is_variadic = $codebase->methods->isVariadic($method_id);
|
2016-12-07 20:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
if ($method_id && strpos($method_id, '::') && !$in_call_map) {
|
2018-02-04 00:52:35 +01: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;
|
|
|
|
|
|
2018-02-01 07:52:20 +01:00
|
|
|
|
if ($method_id && strpos($method_id, '::')) {
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
2018-02-01 07:52:20 +01:00
|
|
|
|
|
|
|
|
|
if ($declaring_method_id && $declaring_method_id !== $method_id) {
|
|
|
|
|
list($fq_class_name) = explode('::', $declaring_method_id);
|
2018-11-11 18:19:53 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
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;
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$template_types = null;
|
|
|
|
|
|
|
|
|
|
if ($function_storage) {
|
2019-01-13 00:18:23 +01:00
|
|
|
|
$template_types = self::getTemplateTypesForFunction(
|
|
|
|
|
$function_storage,
|
|
|
|
|
$class_storage,
|
|
|
|
|
$calling_class_storage
|
|
|
|
|
);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 18:16:09 +01:00
|
|
|
|
$existing_generic_params = $generic_params ?: [];
|
|
|
|
|
|
|
|
|
|
foreach ($existing_generic_params as $key => $type) {
|
2019-01-10 18:13:49 +01:00
|
|
|
|
$existing_generic_params[$key][0] = clone $type[0];
|
2019-01-06 18:16:09 +01:00
|
|
|
|
}
|
2018-06-09 16:14:18 +02:00
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
2017-09-16 19:16:21 +02:00
|
|
|
|
$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->by_ref
|
2018-03-21 16:14:05 +01:00
|
|
|
|
&& $method_id !== 'extract'
|
2017-11-20 06:12:17 +01:00
|
|
|
|
) {
|
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Scalar
|
2017-12-22 15:21:23 +01:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\Array_
|
2017-09-16 19:28:33 +02:00
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
2018-01-12 05:18:13 +01:00
|
|
|
|
|| (
|
|
|
|
|
(
|
|
|
|
|
$arg->value instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
) && (
|
|
|
|
|
!isset($arg->value->inferredType)
|
|
|
|
|
|| !$arg->value->inferredType->by_ref
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-11-20 06:12:17 +01:00
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidPassByReference(
|
|
|
|
|
'Parameter ' . ($argument_offset + 1) . ' of ' . $method_id . ' expects a variable',
|
2019-01-12 15:13:54 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
2017-11-20 06:12:17 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-11-20 06:12:17 +01:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
2017-09-16 19:16:21 +02:00
|
|
|
|
}
|
2017-01-17 06:07:58 +01:00
|
|
|
|
|
2017-11-20 06:12:17 +01:00
|
|
|
|
if (!in_array(
|
|
|
|
|
$method_id,
|
|
|
|
|
[
|
|
|
|
|
'shuffle', 'sort', 'rsort', 'usort', 'ksort', 'asort',
|
|
|
|
|
'krsort', 'arsort', 'natcasesort', 'natsort', 'reset',
|
|
|
|
|
'end', 'next', 'prev', 'array_pop', 'array_shift',
|
2018-08-09 04:33:31 +02:00
|
|
|
|
'array_push', 'array_unshift', 'socket_select', 'array_splice',
|
2017-11-20 06:12:17 +01:00
|
|
|
|
],
|
|
|
|
|
true
|
|
|
|
|
)) {
|
|
|
|
|
$by_ref_type = null;
|
|
|
|
|
|
|
|
|
|
if ($last_param) {
|
|
|
|
|
if ($argument_offset < count($function_params)) {
|
|
|
|
|
$by_ref_type = $function_params[$argument_offset]->type;
|
|
|
|
|
} else {
|
|
|
|
|
$by_ref_type = $last_param->type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($template_types && $by_ref_type) {
|
|
|
|
|
if ($generic_params === null) {
|
|
|
|
|
$generic_params = [];
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 18:38:43 +01:00
|
|
|
|
$original_by_ref_type = $by_ref_type;
|
|
|
|
|
|
2017-11-20 06:12:17 +01:00
|
|
|
|
$by_ref_type = clone $by_ref_type;
|
|
|
|
|
|
2018-11-21 18:38:43 +01:00
|
|
|
|
$by_ref_type->replaceTemplateTypesWithStandins(
|
|
|
|
|
$template_types,
|
|
|
|
|
$generic_params,
|
|
|
|
|
$codebase,
|
|
|
|
|
isset($arg->value->inferredType)
|
|
|
|
|
? clone $arg->value->inferredType
|
|
|
|
|
: null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($generic_params) {
|
|
|
|
|
$original_by_ref_type->replaceTemplateTypesWithArgTypes(
|
|
|
|
|
$generic_params
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$by_ref_type = $original_by_ref_type;
|
|
|
|
|
}
|
2017-11-20 06:12:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$by_ref_type = $by_ref_type ?: Type::getMixed();
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
ExpressionAnalyzer::assignByRefParam(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2017-11-20 06:12:17 +01:00
|
|
|
|
$arg->value,
|
|
|
|
|
$by_ref_type,
|
|
|
|
|
$context,
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$method_id && (strpos($method_id, '::') !== false || !CallMap::inCallMap($method_id))
|
2017-11-20 06:12:17 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2017-09-16 19:16:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($arg->value->inferredType)) {
|
2017-09-02 17:18:56 +02:00
|
|
|
|
if ($function_param && $function_param->type) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$param_type = clone $function_param->type;
|
2017-01-17 06:07:58 +01:00
|
|
|
|
|
|
|
|
|
if ($function_param->is_variadic) {
|
2018-01-09 21:05:48 +01:00
|
|
|
|
if (!$param_type->hasArray()) {
|
2017-11-20 06:12:17 +01:00
|
|
|
|
continue;
|
2017-01-17 06:07:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
|
$array_atomic_type = $param_type->getTypes()['array'];
|
|
|
|
|
|
|
|
|
|
if (!$array_atomic_type instanceof TArray) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$param_type = clone $array_atomic_type->type_params[1];
|
2017-01-17 06:07:58 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if ($function_storage) {
|
2019-01-06 18:16:09 +01:00
|
|
|
|
if ($existing_generic_params) {
|
2018-12-13 06:09:01 +01:00
|
|
|
|
$empty_generic_params = [];
|
2017-12-03 19:22:06 +01:00
|
|
|
|
|
2018-12-13 06:09:01 +01:00
|
|
|
|
$param_type->replaceTemplateTypesWithStandins(
|
2019-01-06 18:16:09 +01:00
|
|
|
|
$existing_generic_params,
|
2018-12-13 06:09:01 +01:00
|
|
|
|
$empty_generic_params,
|
|
|
|
|
$codebase,
|
|
|
|
|
$arg->value->inferredType
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-02-27 22:35:24 +01:00
|
|
|
|
|
2018-12-13 06:09:01 +01:00
|
|
|
|
if ($template_types) {
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if ($generic_params === null) {
|
|
|
|
|
$generic_params = [];
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 06:09:01 +01:00
|
|
|
|
$arg_type = $arg->value->inferredType;
|
2018-06-20 16:40:50 +02:00
|
|
|
|
|
2018-12-13 06:09:01 +01:00
|
|
|
|
if ($arg->unpack) {
|
|
|
|
|
if ($arg->value->inferredType->hasArray()) {
|
|
|
|
|
/** @var Type\Atomic\TArray|Type\Atomic\ObjectLike */
|
|
|
|
|
$array_atomic_type = $arg->value->inferredType->getTypes()['array'];
|
2018-06-20 16:40:50 +02:00
|
|
|
|
|
2018-12-13 06:09:01 +01:00
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
$array_atomic_type = $array_atomic_type->getGenericArrayType();
|
2018-06-20 16:40:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 06:09:01 +01:00
|
|
|
|
$arg_type = $array_atomic_type->type_params[1];
|
|
|
|
|
} else {
|
|
|
|
|
$arg_type = Type::getMixed();
|
|
|
|
|
}
|
2018-06-09 16:14:18 +02:00
|
|
|
|
}
|
2018-12-13 06:09:01 +01:00
|
|
|
|
|
|
|
|
|
$param_type->replaceTemplateTypesWithStandins(
|
|
|
|
|
$template_types,
|
|
|
|
|
$generic_params,
|
|
|
|
|
$codebase,
|
|
|
|
|
$arg_type
|
|
|
|
|
);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 23:14:38 +02:00
|
|
|
|
if (!$context->check_variables) {
|
2016-11-01 19:14:35 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$fleshed_out_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
2017-02-10 04:57:23 +01:00
|
|
|
|
$param_type,
|
|
|
|
|
$fq_class_name,
|
2018-01-26 19:51:00 +01:00
|
|
|
|
$fq_class_name
|
2017-02-10 04:57:23 +01:00
|
|
|
|
);
|
|
|
|
|
|
2018-04-20 23:14:38 +02:00
|
|
|
|
if ($arg->unpack) {
|
|
|
|
|
if ($arg->value->inferredType->hasArray()) {
|
|
|
|
|
/** @var Type\Atomic\TArray|Type\Atomic\ObjectLike */
|
|
|
|
|
$array_atomic_type = $arg->value->inferredType->getTypes()['array'];
|
|
|
|
|
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
$array_atomic_type = $array_atomic_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self::checkFunctionArgumentType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-04-20 23:14:38 +02:00
|
|
|
|
$array_atomic_type->type_params[1],
|
|
|
|
|
$fleshed_out_type,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value),
|
2018-04-20 23:14:38 +02:00
|
|
|
|
$arg->value,
|
|
|
|
|
$context,
|
2018-06-28 23:05:50 +02:00
|
|
|
|
$function_param->by_ref,
|
|
|
|
|
$function_param->is_variadic
|
2018-04-20 23:14:38 +02:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-12-08 19:18:55 +01:00
|
|
|
|
} elseif ($arg->value->inferredType->hasMixed()) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
2018-04-20 23:14:38 +02:00
|
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgument(
|
2018-06-28 23:05:50 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
|
2018-04-20 23:14:38 +02:00
|
|
|
|
. ' cannot be mixed, expecting array',
|
2019-01-12 15:13:54 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
2018-04-20 23:14:38 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-20 23:14:38 +02:00
|
|
|
|
)) {
|
2018-06-28 23:05:50 +02:00
|
|
|
|
// fall through
|
2018-04-20 23:14:38 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-07-09 15:59:51 +02:00
|
|
|
|
foreach ($arg->value->inferredType->getTypes() as $atomic_type) {
|
|
|
|
|
if (!$atomic_type->isIterable($codebase)) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
|
2018-12-08 20:10:06 +01:00
|
|
|
|
. ' expects array, ' . $atomic_type->getId() . ' provided',
|
2019-01-12 15:13:54 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
2018-07-09 15:59:51 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-07-09 15:59:51 +02:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-20 23:14:38 +02:00
|
|
|
|
}
|
2017-02-27 17:07:44 +01:00
|
|
|
|
}
|
2018-04-20 23:14:38 +02:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self::checkFunctionArgumentType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-04-20 23:14:38 +02:00
|
|
|
|
$arg->value->inferredType,
|
|
|
|
|
$fleshed_out_type,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$argument_offset,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value),
|
2018-04-20 23:14:38 +02:00
|
|
|
|
$arg->value,
|
|
|
|
|
$context,
|
2018-06-28 23:05:50 +02:00
|
|
|
|
$function_param->by_ref,
|
|
|
|
|
$function_param->is_variadic
|
2018-04-20 23:14:38 +02:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
|
} elseif ($function_param) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
2018-04-07 21:16:46 +02:00
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
|
if ($function_param->type && !$function_param->type->hasMixed()) {
|
2018-04-07 21:16:46 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgument(
|
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
|
|
|
|
|
. ' cannot be mixed, expecting ' . $function_param->type,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $arg->value)
|
2018-04-07 21:16:46 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-07 21:16:46 +02:00
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} 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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
|
if (self::checkArrayFunctionArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 !== '...=')
|
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooManyArguments(
|
2018-02-02 17:26:55 +01:00
|
|
|
|
'Too many arguments for method ' . ($cased_method_id ?: $method_id)
|
|
|
|
|
. ' - expecting ' . count($function_params) . ' but saw ' . count($args),
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$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)) {
|
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
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
if (!$param->is_optional && !$param->is_variadic) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TooFewArguments(
|
2018-02-02 17:26:55 +01:00
|
|
|
|
'Too few arguments for method ' . $cased_method_id
|
|
|
|
|
. ' - expecting ' . count($function_params) . ' but saw ' . count($args),
|
2018-08-16 22:49:33 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
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-01-13 00:18:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return array<string, array{Type\Union, ?string}>
|
|
|
|
|
*/
|
|
|
|
|
private static function getTemplateTypesForFunction(
|
|
|
|
|
FunctionLikeStorage $function_storage,
|
|
|
|
|
ClassLikeStorage $class_storage = null,
|
|
|
|
|
ClassLikeStorage $calling_class_storage = null
|
|
|
|
|
) : array {
|
|
|
|
|
$template_types = [];
|
|
|
|
|
|
|
|
|
|
if ($function_storage->template_types) {
|
|
|
|
|
$template_types = $function_storage->template_types;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($class_storage) {
|
|
|
|
|
if ($calling_class_storage
|
|
|
|
|
&& $class_storage !== $calling_class_storage
|
|
|
|
|
&& $calling_class_storage->template_type_extends
|
|
|
|
|
) {
|
|
|
|
|
foreach ($calling_class_storage->template_type_extends as $class_name_lc => $type_map) {
|
|
|
|
|
foreach ($type_map as $template_name => $type) {
|
|
|
|
|
if (is_string($template_name)
|
|
|
|
|
&& $class_name_lc === strtolower($class_storage->name)
|
|
|
|
|
) {
|
|
|
|
|
if ($type instanceof Type\Atomic\TGenericParam
|
|
|
|
|
&& $type->defining_class
|
|
|
|
|
&& isset(
|
|
|
|
|
$calling_class_storage
|
|
|
|
|
->template_type_extends
|
|
|
|
|
[strtolower($type->defining_class)]
|
|
|
|
|
[$type->param_name]
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$type = $calling_class_storage
|
|
|
|
|
->template_type_extends
|
|
|
|
|
[strtolower($type->defining_class)]
|
|
|
|
|
[$type->param_name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$template_types[$template_name] = [new Type\Union([$type]), $class_storage->name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($class_storage->template_types) {
|
|
|
|
|
foreach ($class_storage->template_types as $template_name => $type) {
|
|
|
|
|
$template_types[$template_name] = $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($template_types as $key => $type) {
|
|
|
|
|
$template_types[$key][0] = clone $type[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-11 01:10:13 +01:00
|
|
|
|
$array_arg = isset($arg->value) ? $arg->value : null;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
2017-04-08 00:30:15 +02:00
|
|
|
|
/** @var ObjectLike|TArray|null */
|
|
|
|
|
$array_arg_type = $array_arg
|
2016-12-31 16:51:42 +01:00
|
|
|
|
&& isset($array_arg->inferredType)
|
2018-01-09 21:05:48 +01:00
|
|
|
|
&& isset($array_arg->inferredType->getTypes()['array'])
|
|
|
|
|
? $array_arg->inferredType->getTypes()['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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_arg_types[] = $array_arg_type;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-01-28 22:52:57 +01:00
|
|
|
|
/** @var null|PhpParser\Node\Arg */
|
2016-12-31 16:51:42 +01:00
|
|
|
|
$closure_arg = isset($args[$closure_index]) ? $args[$closure_index] : null;
|
|
|
|
|
|
|
|
|
|
/** @var Type\Union|null */
|
|
|
|
|
$closure_arg_type = $closure_arg && isset($closure_arg->value->inferredType)
|
|
|
|
|
? $closure_arg->value->inferredType
|
|
|
|
|
: null;
|
|
|
|
|
|
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
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
|
foreach ($closure_arg_type->getTypes() as $closure_type) {
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (self::checkArrayFunctionClosureType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
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
|
2018-03-02 05:33:21 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01: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
|
|
|
|
|
*
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function checkArrayFunctionClosureType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
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
|
|
|
|
$project_analyzer = $statements_analyzer->getFileAnalyzer()->project_analyzer;
|
2017-04-08 00:30:15 +02:00
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (!$closure_type instanceof Type\Atomic\Fn) {
|
|
|
|
|
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
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
|
$function_id_part = $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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$closure_types[] = new Type\Atomic\Fn(
|
|
|
|
|
'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;
|
|
|
|
|
}
|
|
|
|
|
|
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)) {
|
|
|
|
|
$callmap_params_options = CallMap::getParamsFromCallMap($function_id);
|
|
|
|
|
|
|
|
|
|
if ($callmap_params_options === null) {
|
|
|
|
|
throw new \UnexpectedValueException('This should not happen');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$passing_callmap_params_options = [];
|
|
|
|
|
|
|
|
|
|
foreach ($callmap_params_options as $callmap_params_option) {
|
|
|
|
|
$required_param_count = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($callmap_params_option as $i => $param) {
|
|
|
|
|
if (!$param->is_optional && !$param->is_variadic) {
|
|
|
|
|
$required_param_count = $i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($required_param_count <= $max_closure_param_count) {
|
|
|
|
|
$passing_callmap_params_options[] = $callmap_params_option;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($passing_callmap_params_options) {
|
|
|
|
|
foreach ($passing_callmap_params_options as $passing_callmap_params_option) {
|
|
|
|
|
$closure_types[] = new Type\Atomic\Fn(
|
|
|
|
|
'Closure',
|
|
|
|
|
$passing_callmap_params_option,
|
|
|
|
|
$function_storage->return_type ?: Type::getMixed()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$closure_types[] = new Type\Atomic\Fn(
|
|
|
|
|
'Closure',
|
|
|
|
|
$callmap_params_options[0],
|
|
|
|
|
$function_storage->return_type ?: Type::getMixed()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$closure_types[] = new Type\Atomic\Fn(
|
|
|
|
|
'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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (self::checkArrayFunctionClosureTypeArgs(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
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
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $method_id
|
|
|
|
|
* @param int $min_closure_param_count
|
|
|
|
|
* @param int $max_closure_param_count [description]
|
|
|
|
|
* @param (TArray|null)[] $array_arg_types
|
|
|
|
|
*
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
private static function checkArrayFunctionClosureTypeArgs(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-03-02 05:33:21 +01:00
|
|
|
|
$method_id,
|
|
|
|
|
Type\Atomic\Fn $closure_type,
|
|
|
|
|
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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} 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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($closure_params as $closure_param) {
|
|
|
|
|
if (!isset($array_arg_types[$i])) {
|
|
|
|
|
++$i;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Type\Atomic\TArray */
|
|
|
|
|
$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
|
|
|
|
++$i;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$closure_param_type = $closure_param->type;
|
|
|
|
|
|
|
|
|
|
if (!$closure_param_type) {
|
|
|
|
|
++$i;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
$scalar_type_match_found,
|
|
|
|
|
$type_coerced,
|
|
|
|
|
$type_coerced_from_mixed
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($type_coerced) {
|
|
|
|
|
if ($type_coerced_from_mixed) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedTypeCoercion(
|
|
|
|
|
'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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg)
|
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(
|
|
|
|
|
new TypeCoercion(
|
|
|
|
|
'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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg)
|
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
|
|
|
|
|
2018-03-02 05:33:21 +01:00
|
|
|
|
if (!$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
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($scalar_type_match_found) {
|
|
|
|
|
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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg)
|
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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} 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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg)
|
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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $closure_arg)
|
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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-02 05:33:21 +01:00
|
|
|
|
|
|
|
|
|
++$i;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @param Type\Union $input_type
|
|
|
|
|
* @param Type\Union $param_type
|
2017-01-13 18:40:01 +01:00
|
|
|
|
* @param string|null $cased_method_id
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @param int $argument_offset
|
2016-12-04 01:11:30 +01:00
|
|
|
|
* @param CodeLocation $code_location
|
2017-12-15 22:48:06 +01:00
|
|
|
|
* @param bool $by_ref
|
2018-06-28 23:05:50 +02:00
|
|
|
|
* @param bool $variadic
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
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,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$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,
|
2018-06-28 23:05:50 +02:00
|
|
|
|
$by_ref = false,
|
|
|
|
|
$variadic = false
|
2016-11-01 19:14:35 +01:00
|
|
|
|
) {
|
2018-12-08 19:18:55 +01:00
|
|
|
|
if ($param_type->hasMixed()) {
|
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
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2017-09-03 00:15:52 +02:00
|
|
|
|
|
2017-01-13 18:40:01 +01:00
|
|
|
|
$method_identifier = $cased_method_id ? ' of ' . $cased_method_id : '';
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase->infer_types_from_usage && $input_expr->inferredType) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$source_analyzer = $statements_analyzer->getSource();
|
2017-09-03 00:15:52 +02:00
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if ($source_analyzer instanceof FunctionLikeAnalyzer) {
|
2017-12-07 21:50:25 +01:00
|
|
|
|
$context->inferType(
|
|
|
|
|
$input_expr,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$source_analyzer->getFunctionLikeStorage($statements_analyzer),
|
2017-12-07 21:50:25 +01:00
|
|
|
|
$param_type
|
|
|
|
|
);
|
2017-09-03 00:15:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
|
if ($input_type->hasMixed()) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedArgument(
|
2017-01-13 18:40:01 +01:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be mixed, expecting ' .
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$param_type,
|
2016-12-04 01:11:30 +01:00
|
|
|
|
$code_location
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
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()
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$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
|
|
|
|
|
);
|
|
|
|
|
|
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,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$scalar_type_match_found,
|
2018-01-10 16:56:43 +01:00
|
|
|
|
$type_coerced,
|
|
|
|
|
$type_coerced_from_mixed,
|
2016-12-29 06:14:06 +01:00
|
|
|
|
$to_string_cast
|
2016-11-02 07:29:00 +01:00
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
2018-08-29 21:10:56 +02:00
|
|
|
|
if ($context->strict_types && !$param_type->from_docblock && $cased_method_id !== 'echo') {
|
2018-08-28 23:42:39 +02:00
|
|
|
|
$scalar_type_match_found = false;
|
|
|
|
|
|
|
|
|
|
if ($to_string_cast) {
|
|
|
|
|
$to_string_cast = false;
|
|
|
|
|
$type_match_found = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 16:56:43 +01:00
|
|
|
|
if ($type_coerced) {
|
|
|
|
|
if ($type_coerced_from_mixed) {
|
2017-11-19 19:42:48 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedTypeCoercion(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', parent type ' . $input_type->getId() . ' provided',
|
2017-11-19 19:42:48 +01:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
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(
|
|
|
|
|
new TypeCoercion(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', parent type ' . $input_type->getId() . ' provided',
|
2017-11-19 19:42:48 +01:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 06:14:06 +01:00
|
|
|
|
if ($to_string_cast && $cased_method_id !== 'echo') {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 16:56:43 +01:00
|
|
|
|
if (!$type_match_found && !$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
|
|
|
|
);
|
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
|
if ($scalar_type_match_found) {
|
2016-12-09 18:53:22 +01:00
|
|
|
|
if ($cased_method_id !== 'echo') {
|
|
|
|
|
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',
|
2016-12-09 18:53:22 +01:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-12-09 18:53:22 +01:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
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',
|
2017-04-08 15:28:02 +02:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-04-08 15:28:02 +02:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
} elseif (IssueBuffer::accepts(
|
2016-11-01 19:14:35 +01:00
|
|
|
|
new InvalidArgument(
|
2018-05-20 23:43:02 +02:00
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
|
|
|
|
|
', ' . $input_type->getId() . ' provided',
|
2016-12-04 01:11:30 +01:00
|
|
|
|
$code_location
|
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
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
} elseif ($input_expr instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
|| $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
|
|
|
|
) {
|
2018-01-09 21:05:48 +01:00
|
|
|
|
foreach ($param_type->getTypes() 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,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-05 22:06:06 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($param_type_part instanceof TArray
|
|
|
|
|
&& $input_expr instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
) {
|
2018-05-20 06:27:53 +02:00
|
|
|
|
foreach ($param_type_part->type_params[1]->getTypes() as $param_array_type_part) {
|
|
|
|
|
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,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-20 06:27:53 +02:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-03-19 01:29:41 +01:00
|
|
|
|
$function_id_part = $callable_fq_class_name . '::' . $method_name;
|
|
|
|
|
|
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,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-19 01:29:41 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$codebase->classOrInterfaceExists($callable_fq_class_name)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 05:13:57 +01:00
|
|
|
|
if (!$codebase->methodExists($function_id_part)
|
|
|
|
|
&& !$codebase->methodExists($callable_fq_class_name . '::__call')
|
|
|
|
|
) {
|
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
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
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
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-12 00:30:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2018-05-20 19:14:31 +02:00
|
|
|
|
if (!$param_type->isNullable() && $cased_method_id !== 'echo') {
|
|
|
|
|
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(),
|
2018-05-20 19:14:31 +02:00
|
|
|
|
$code_location
|
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-20 19:14:31 +02:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
$code_location
|
|
|
|
|
),
|
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',
|
|
|
|
|
$code_location
|
|
|
|
|
),
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-15 22:48:06 +01:00
|
|
|
|
if ($type_match_found
|
2018-12-08 19:18:55 +01:00
|
|
|
|
&& !$param_type->hasMixed()
|
2017-12-15 22:48:06 +01:00
|
|
|
|
&& !$param_type->from_docblock
|
2018-06-28 23:05:50 +02:00
|
|
|
|
&& !$variadic
|
2017-12-15 22:48:06 +01:00
|
|
|
|
&& !$by_ref
|
2019-01-11 20:46:03 +01:00
|
|
|
|
&& $cased_method_id !== 'echo'
|
2017-12-15 22:48:06 +01:00
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2017-12-11 03:14:30 +01:00
|
|
|
|
$input_expr,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
2017-12-11 03:14:30 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
|
if ($input_type->isNullable() && !$param_type->isNullable()) {
|
2018-01-09 21:05:48 +01:00
|
|
|
|
$input_type->removeType('null');
|
2017-12-11 03:14:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($input_type->getId() === $param_type->getId()) {
|
|
|
|
|
$input_type->from_docblock = false;
|
2018-02-08 19:01:39 +01:00
|
|
|
|
|
|
|
|
|
foreach ($input_type->getTypes() as $atomic_type) {
|
|
|
|
|
$atomic_type->from_docblock = false;
|
|
|
|
|
}
|
2017-12-11 03:14:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer);
|
2017-12-18 05:22:26 +01:00
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $input_type;
|
2017-12-11 03:14:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
return null;
|
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_) {
|
2017-08-30 19:45:41 +02:00
|
|
|
|
return [preg_replace('/^\\\/', '', $callable_arg->value)];
|
2017-08-15 01:30:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count($callable_arg->items) !== 2) {
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($class_arg->inferredType) || !$class_arg->inferredType->hasObjectType()) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$method_ids = [];
|
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
|
foreach ($class_arg->inferredType->getTypes() 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) {
|
2018-11-02 18:08:56 +01:00
|
|
|
|
if ($extra_type instanceof Type\Atomic\TGenericParam) {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get a generic param here');
|
|
|
|
|
}
|
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
|
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
|
* @param Context $context
|
2019-01-10 18:13:49 +01:00
|
|
|
|
* @param array<string, array{Type\Union, ?string}> $generic_params,
|
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,
|
2018-02-23 21:39:33 +01:00
|
|
|
|
array $assertions,
|
|
|
|
|
array $args,
|
2018-11-16 00:50:08 +01:00
|
|
|
|
array $generic_params,
|
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
|
|
|
|
}
|
2018-11-14 21:40:56 +01: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-01-05 06:15:53 +01:00
|
|
|
|
if (isset($generic_params[$rule])) {
|
2019-01-10 18:13:49 +01:00
|
|
|
|
if ($generic_params[$rule][0]->hasMixed()) {
|
2018-11-16 06:56:57 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 18:13:49 +01:00
|
|
|
|
$replacement_atomic_types = $generic_params[$rule][0]->getTypes();
|
2018-11-16 00:50:08 +01:00
|
|
|
|
|
2018-11-16 06:56:57 +01:00
|
|
|
|
if (count($replacement_atomic_types) > 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 00:50:08 +01:00
|
|
|
|
$ored_type_assertions = [];
|
|
|
|
|
|
|
|
|
|
foreach ($replacement_atomic_types as $replacement_atomic_type) {
|
|
|
|
|
if ($replacement_atomic_type instanceof Type\Atomic\TMixed) {
|
|
|
|
|
continue 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2018-11-16 17:50:07 +01:00
|
|
|
|
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNull) {
|
|
|
|
|
$ored_type_assertions[] = $prefix . 'null';
|
2018-10-30 14:20:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-16 00:50:08 +01:00
|
|
|
|
|
2018-11-16 17:50:07 +01:00
|
|
|
|
if ($ored_type_assertions) {
|
|
|
|
|
$type_assertions[$assertion_var_id] = [$ored_type_assertions];
|
|
|
|
|
}
|
2018-10-30 14:20:34 +01:00
|
|
|
|
} else {
|
|
|
|
|
$type_assertions[$assertion_var_id] = $assertion->rule;
|
|
|
|
|
}
|
2019-01-05 20:50:11 +01:00
|
|
|
|
} elseif ($arg_value && $assertion->rule === [['!falsy']]) {
|
|
|
|
|
$assert_clauses = \Psalm\Type\Algebra::getFormula(
|
|
|
|
|
$arg_value,
|
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$statements_analyzer->getCodebase()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$simplified_clauses = \Psalm\Type\Algebra::simplifyCNF(
|
|
|
|
|
array_merge($context->clauses, $assert_clauses)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$assert_type_assertions = \Psalm\Type\Algebra::getTruthsFromFormula(
|
|
|
|
|
$simplified_clauses
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$type_assertions = array_merge($type_assertions, $assert_type_assertions);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$changed_vars = [];
|
|
|
|
|
|
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(
|
|
|
|
|
$type_assertions,
|
|
|
|
|
$context->vars_in_scope,
|
|
|
|
|
$changed_vars,
|
|
|
|
|
$asserted_keys,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$context->inside_loop,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $expr)
|
|
|
|
|
);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
|
foreach ($changed_vars as $changed_var) {
|
|
|
|
|
if (isset($op_vars_in_scope[$changed_var])) {
|
|
|
|
|
$op_vars_in_scope[$changed_var]->from_docblock = true;
|
|
|
|
|
}
|
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
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
}
|