1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 11:26:55 +01:00
psalm/src/Psalm/Checker/Statements/Expression/CallChecker.php

1389 lines
52 KiB
PHP
Raw Normal View History

2016-11-01 19:14:35 +01:00
<?php
namespace Psalm\Checker\Statements\Expression;
use PhpParser;
use Psalm\Checker\ClassLikeChecker;
use Psalm\Checker\FunctionChecker;
use Psalm\Checker\FunctionLikeChecker;
use Psalm\Checker\MethodChecker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\StatementsChecker;
use Psalm\Checker\TypeChecker;
use Psalm\CodeLocation;
2016-11-02 07:29:00 +01:00
use Psalm\Context;
use Psalm\FunctionLikeParameter;
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;
use Psalm\Issue\MixedTypeCoercion;
use Psalm\Issue\NullArgument;
use Psalm\Issue\PossiblyFalseArgument;
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;
use Psalm\Storage\FunctionLikeStorage;
2016-11-01 19:14:35 +01:00
use Psalm\Type;
use Psalm\Type\Atomic\ObjectLike;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TNamedObject;
2016-11-01 19:14:35 +01:00
class CallChecker
{
2017-02-10 02:35:17 +01:00
/**
* @param FunctionLikeChecker $source
* @param string $method_name
* @param Context $context
2017-05-27 02:16:18 +02:00
*
* @return void
2017-02-10 02:35:17 +01:00
*/
public static function collectSpecialInformation(
FunctionLikeChecker $source,
$method_name,
Context $context
) {
$fq_class_name = (string)$source->getFQCLN();
2016-11-02 07:29:00 +01:00
$project_checker = $source->getFileChecker()->project_checker;
$codebase = $project_checker->codebase;
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);
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;
}
$project_checker->getMethodMutations($method_id, $context);
}
2017-02-10 02:35:17 +01:00
} elseif ($context->collect_initializations &&
$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
)
) &&
$source->getMethodName() !== $method_name
2017-02-10 02:35:17 +01:00
) {
$method_id = $fq_class_name . '::' . strtolower($method_name);
$declaring_method_id = (string) MethodChecker::getDeclaringMethodId($project_checker, $method_id);
2017-02-10 02:35:17 +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;
$method_storage = $codebase->getMethodStorage($declaring_method_id);
2017-02-10 02:35:17 +01:00
$class_checker = $source->getSource();
if ($class_checker instanceof ClassLikeChecker &&
($method_storage->visibility === ClassLikeChecker::VISIBILITY_PRIVATE || $method_storage->final)
2017-02-10 02:35:17 +01:00
) {
$local_vars_in_scope = [];
$local_vars_possibly_in_scope = [];
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];
}
}
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];
}
}
$class_checker->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
* @param array<string, Type\Union>|null &$generic_params
* @param Context $context
* @param CodeLocation $code_location
* @param StatementsChecker $statements_checker
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,
&$generic_params,
2017-02-10 02:35:17 +01:00
Context $context,
CodeLocation $code_location,
StatementsChecker $statements_checker
) {
$project_checker = $statements_checker->getFileChecker()->project_checker;
2017-02-10 02:35:17 +01:00
$method_params = $method_id
? FunctionLikeChecker::getMethodParamsById($project_checker, $method_id, $args)
2017-02-10 02:35:17 +01:00
: null;
if (self::checkFunctionArguments(
$statements_checker,
$args,
$method_params,
$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);
$class_storage = $project_checker->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) {
$declaring_class_storage = $project_checker->classlike_storage_provider->get($declaring_fq_class_name);
} 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)];
}
2017-02-10 02:35:17 +01:00
if (!$class_storage->user_defined) {
// check again after we've processed args
$method_params = FunctionLikeChecker::getMethodParamsById(
$project_checker,
2016-11-02 07:29:00 +01:00
$method_id,
$args
2017-02-10 02:35:17 +01:00
);
}
if (self::checkFunctionArgumentsMatch(
$statements_checker,
$args,
$method_id,
$method_params,
$method_storage,
$class_storage,
$generic_params,
$code_location,
$context
) === 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
}
/**
* @param StatementsChecker $statements_checker
* @param array<int, PhpParser\Node\Arg> $args
2017-01-01 23:39:39 +01:00
* @param array<int, FunctionLikeParameter>|null $function_params
* @param string|null $method_id
* @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(
StatementsChecker $statements_checker,
array $args,
2017-05-05 03:57:26 +02:00
$function_params,
$method_id,
2016-12-07 20:13:39 +01:00
Context $context
2016-11-02 07:29:00 +01:00
) {
$last_param = $function_params
? $function_params[count($function_params) - 1]
: null;
// 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;
if (ExpressionChecker::analyze(
$statements_checker,
$array_arg,
$context
) === false) {
return false;
}
2016-11-01 19:14:35 +01:00
if (isset($array_arg->inferredType) && $array_arg->inferredType->hasArray()) {
/** @var TArray|ObjectLike */
$array_type = $array_arg->inferredType->getTypes()['array'];
if ($array_type instanceof ObjectLike) {
$array_type = $array_type->getGenericArrayType();
}
2016-11-01 19:14:35 +01: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
}
if (ExpressionChecker::analyze(
$statements_checker,
2016-11-02 07:29:00 +01:00
$arg->value,
$context
) === false) {
return false;
}
$by_ref_type = Type::combineUnionTypes(
$by_ref_type,
new Type\Union(
[
new TArray(
[
Type::getInt(),
2017-10-28 21:17:32 +02:00
isset($arg->value->inferredType)
? clone $arg->value->inferredType
: Type::getMixed(),
]
),
]
)
2016-11-02 07:29:00 +01:00
);
}
2016-11-01 19:14:35 +01:00
ExpressionChecker::assignByRefParam(
$statements_checker,
$array_arg,
$by_ref_type,
2017-10-28 21:33:29 +02:00
$context,
false
);
}
return;
}
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
}
$by_ref_type = $by_ref_type ? clone $by_ref_type : Type::getMixed();
2016-11-01 19:14:35 +01:00
}
if ($by_ref
&& $by_ref_type
&& !(
$arg->value instanceof PhpParser\Node\Expr\ConstFetch
|| $arg->value instanceof PhpParser\Node\Expr\FuncCall
|| $arg->value instanceof PhpParser\Node\Expr\MethodCall
)
) {
// special handling for array sort
if ($argument_offset === 0
&& $method_id
&& in_array(
$method_id,
[
'shuffle', 'sort', 'rsort', 'usort', 'ksort', 'asort',
'krsort', 'arsort', 'natcasesort', 'natsort', 'reset',
'end', 'next', 'prev', 'array_pop', 'array_shift',
2017-10-28 21:33:29 +02:00
],
true
)
) {
if (ExpressionChecker::analyze(
$statements_checker,
$arg->value,
$context
) === false) {
return false;
}
2016-11-01 19:14:35 +01:00
if (in_array($method_id, ['array_pop', 'array_shift'], true)) {
$var_id = ExpressionChecker::getVarId(
$arg->value,
$statements_checker->getFQCLN(),
$statements_checker
);
if ($var_id) {
$context->removeVarFromConflictingClauses($var_id, null, $statements_checker);
}
continue;
}
// noops
if (in_array($method_id, ['reset', 'end', 'next', 'prev'], true)) {
continue;
}
if (isset($arg->value->inferredType)
&& $arg->value->inferredType->hasArray()
) {
/** @var TArray|ObjectLike */
$array_type = $arg->value->inferredType->getTypes()['array'];
if ($array_type instanceof ObjectLike) {
$array_type = $array_type->getGenericArrayType();
}
if (in_array($method_id, ['shuffle', 'sort', 'rsort', 'usort'], true)) {
$tvalue = $array_type->type_params[1];
$by_ref_type = new Type\Union([new TArray([Type::getInt(), clone $tvalue])]);
} else {
$by_ref_type = new Type\Union([clone $array_type]);
}
ExpressionChecker::assignByRefParam(
$statements_checker,
$arg->value,
$by_ref_type,
2017-10-28 21:33:29 +02:00
$context,
false
);
continue;
}
}
2016-11-01 19:14:35 +01:00
if ($method_id === 'socket_select') {
if (ExpressionChecker::analyze(
$statements_checker,
$arg->value,
$context
) === false) {
return false;
}
}
} else {
2018-01-14 18:09:40 +01:00
if (ExpressionChecker::analyze($statements_checker, $arg->value, $context) === false) {
return false;
2016-11-01 19:14:35 +01:00
}
}
2016-11-02 07:29:00 +01:00
} else {
if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch && is_string($arg->value->name)) {
$var_id = '$' . $arg->value->name;
} else {
$var_id = ExpressionChecker::getVarId(
$arg->value,
$statements_checker->getFQCLN(),
$statements_checker
);
}
if ($var_id &&
(!$context->hasVariable($var_id, $statements_checker) || $context->vars_in_scope[$var_id]->isNull())
) {
// 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;
2018-01-29 00:13:38 +01:00
if (strpos($var_id, '-') === false
&& strpos($var_id, '[') === false
&& !$statements_checker->hasVariable($var_id)
) {
$location = new CodeLocation($statements_checker, $arg->value);
$statements_checker->registerVariable(
$var_id,
2018-01-29 00:13:38 +01:00
$location,
null
);
2018-01-29 00:13:38 +01:00
$statements_checker->registerVariableUse($location);
}
2016-11-01 19:14:35 +01:00
}
}
}
2016-12-07 20:13:39 +01:00
return null;
}
/**
* @param StatementsChecker $statements_checker
* @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
* @param FunctionLikeStorage|null $function_storage
* @param ClassLikeStorage|null $class_storage
2017-02-10 02:35:17 +01:00
* @param array<string, Type\Union>|null $generic_params
* @param CodeLocation $code_location
* @param Context $context
2017-05-27 02:16:18 +02:00
*
2016-12-07 20:13:39 +01:00
* @return false|null
*/
protected static function checkFunctionArgumentsMatch(
StatementsChecker $statements_checker,
array $args,
$method_id,
2017-02-10 02:35:17 +01:00
array $function_params,
$function_storage,
$class_storage,
&$generic_params,
CodeLocation $code_location,
Context $context
2016-12-07 20:13:39 +01:00
) {
$in_call_map = $method_id ? FunctionChecker::inCallMap($method_id) : false;
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;
$project_checker = $statements_checker->getFileChecker()->project_checker;
2016-12-07 20:13:39 +01:00
if ($method_id) {
if ($in_call_map || !strpos($method_id, '::')) {
$is_variadic = FunctionChecker::isVariadic(
$project_checker,
strtolower($method_id),
$statements_checker->getFilePath()
);
2016-12-07 20:13:39 +01:00
} else {
$fq_class_name = explode('::', $method_id)[0];
$is_variadic = MethodChecker::isVariadic($project_checker, $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) {
$cased_method_id = MethodChecker::getCasedMethodId($project_checker, $method_id);
2016-11-01 19:14:35 +01:00
}
if ($method_id && strpos($method_id, '::')) {
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $method_id);
if ($declaring_method_id && $declaring_method_id !== $method_id) {
list($fq_class_name) = explode('::', $declaring_method_id);
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
}
}
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;
}
$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) {
$template_types = [];
if ($function_storage->template_types) {
$template_types = $function_storage->template_types;
}
if ($class_storage && $class_storage->template_types) {
$template_types = array_merge($template_types, $class_storage->template_types);
}
}
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
) {
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
|| (
(
$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
)
)
) {
if (IssueBuffer::accepts(
new InvalidPassByReference(
'Parameter ' . ($argument_offset + 1) . ' of ' . $method_id . ' expects a variable',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
continue;
2017-09-16 19:16:21 +02:00
}
if (!in_array(
$method_id,
[
'shuffle', 'sort', 'rsort', 'usort', 'ksort', 'asort',
'krsort', 'arsort', 'natcasesort', 'natsort', 'reset',
'end', 'next', 'prev', 'array_pop', 'array_shift',
'array_push', 'array_unshift', 'socket_select',
],
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 = [];
}
$by_ref_type = clone $by_ref_type;
2017-12-03 17:23:40 +01:00
$by_ref_type->replaceTemplateTypesWithStandins($template_types, $generic_params);
}
}
$by_ref_type = $by_ref_type ?: Type::getMixed();
ExpressionChecker::assignByRefParam(
$statements_checker,
$arg->value,
$by_ref_type,
$context,
$method_id && (strpos($method_id, '::') !== false || !FunctionChecker::inCallMap($method_id))
);
}
2017-09-16 19:16:21 +02:00
}
if (isset($arg->value->inferredType)) {
if ($function_param && $function_param->type) {
2017-02-10 02:35:17 +01:00
$param_type = clone $function_param->type;
if ($function_param->is_variadic) {
if (!$param_type->hasArray()) {
continue;
}
$array_atomic_type = $param_type->getTypes()['array'];
if (!$array_atomic_type instanceof TArray) {
continue;
}
$param_type = clone $array_atomic_type->type_params[1];
}
2016-11-01 19:14:35 +01:00
2017-02-10 02:35:17 +01:00
if ($function_storage) {
if (isset($function_storage->template_typeof_params[$argument_offset])) {
$template_type = $function_storage->template_typeof_params[$argument_offset];
$offset_value_type = null;
if ($arg->value instanceof PhpParser\Node\Expr\ClassConstFetch &&
$arg->value->class instanceof PhpParser\Node\Name &&
is_string($arg->value->name) &&
strtolower($arg->value->name) === 'class'
) {
$offset_value_type = Type::parseString(
ClassLikeChecker::getFQCLNFromNameObject(
$arg->value->class,
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$statements_checker->getAliases()
2017-02-10 02:35:17 +01:00
)
);
2017-02-13 01:51:48 +01:00
} elseif ($arg->value instanceof PhpParser\Node\Scalar\String_ && $arg->value->value) {
2017-02-10 02:35:17 +01:00
$offset_value_type = Type::parseString($arg->value->value);
}
2017-02-27 22:38:43 +01:00
if ($offset_value_type) {
foreach ($offset_value_type->getTypes() as $offset_value_type_part) {
2017-02-27 22:38:43 +01:00
// register class if the class exists
if ($offset_value_type_part instanceof TNamedObject) {
ClassLikeChecker::checkFullyQualifiedClassLikeName(
$statements_checker,
2017-02-27 22:38:43 +01:00
$offset_value_type_part->value,
new CodeLocation($statements_checker->getSource(), $arg->value),
$statements_checker->getSuppressedIssues()
);
}
}
$offset_value_type->setFromDocblock();
2017-02-27 22:35:24 +01:00
}
2017-02-10 02:35:17 +01:00
if ($generic_params === null) {
$generic_params = [];
}
$generic_params[$template_type] = $offset_value_type ?: Type::getMixed();
} elseif ($template_types) {
if ($generic_params === null) {
$generic_params = [];
2017-02-10 02:35:17 +01:00
}
2017-12-03 17:23:40 +01:00
$param_type->replaceTemplateTypesWithStandins(
$template_types,
$generic_params,
$arg->value->inferredType
);
2017-02-10 02:35:17 +01:00
}
}
// for now stop when we encounter a packed argument
if ($arg->unpack) {
2016-11-01 19:14:35 +01:00
break;
}
$fleshed_out_type = ExpressionChecker::fleshOutType(
$project_checker,
$param_type,
$fq_class_name,
2018-01-26 19:51:00 +01:00
$fq_class_name
);
if ($context->check_variables) {
if (self::checkFunctionArgumentType(
$statements_checker,
$arg->value->inferredType,
$fleshed_out_type,
$cased_method_id,
$argument_offset,
new CodeLocation($statements_checker->getSource(), $arg->value),
$arg->value,
$context,
$function_param->by_ref
) === false) {
return false;
}
2016-11-01 19:14:35 +01:00
}
}
}
}
2017-02-10 02:35:17 +01:00
if ($method_id === 'array_map' || $method_id === 'array_filter') {
if ($method_id === 'array_map' && count($args) < 2) {
if (IssueBuffer::accepts(
new TooFewArguments(
'Too few arguments for ' . $method_id,
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif ($method_id === 'array_filter' && count($args) < 1) {
if (IssueBuffer::accepts(
new TooFewArguments(
'Too few arguments for ' . $method_id,
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
}
if (self::checkArrayFunctionArgumentsMatch(
$statements_checker,
$args,
2017-02-12 01:30:06 +01:00
$method_id
) === 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(
'Too many arguments for method ' . ($cased_method_id ?: $method_id),
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
2017-02-10 02:35:17 +01:00
return null;
}
2017-02-10 02:35:17 +01:00
if (!$has_packed_var && count($args) < count($function_params)) {
for ($i = count($args), $j = count($function_params); $i < $j; ++$i) {
2017-02-10 02:35:17 +01:00
$param = $function_params[$i];
2017-02-10 02:35:17 +01:00
if (!$param->is_optional && !$param->is_variadic) {
if (IssueBuffer::accepts(
new TooFewArguments(
'Too few arguments for method ' . $cased_method_id,
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2017-02-10 02:35:17 +01:00
break;
}
}
}
}
/**
* @param StatementsChecker $statements_checker
* @param array<int, PhpParser\Node\Arg> $args
* @param string|null $method_id
2017-05-27 02:16:18 +02:00
*
* @return false|null
*/
protected static function checkArrayFunctionArgumentsMatch(
StatementsChecker $statements_checker,
array $args,
2017-02-12 01:30:06 +01:00
$method_id
) {
$closure_index = $method_id === 'array_map' ? 0 : 1;
2016-11-01 19:14:35 +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;
/** @var ObjectLike|TArray|null */
$array_arg_type = $array_arg
&& isset($array_arg->inferredType)
&& isset($array_arg->inferredType->getTypes()['array'])
? $array_arg->inferredType->getTypes()['array']
: null;
if ($array_arg_type instanceof ObjectLike) {
$array_arg_type = $array_arg_type->getGenericArrayType();
}
$array_arg_types[] = $array_arg_type;
}
2016-11-01 19:14:35 +01:00
/** @var null|PhpParser\Node\Arg */
$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;
$file_checker = $statements_checker->getFileChecker();
$project_checker = $file_checker->project_checker;
if ($closure_arg && $closure_arg_type) {
$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;
}
foreach ($closure_arg_type->getTypes() as $closure_type) {
if (!$closure_type instanceof Type\Atomic\Fn) {
2016-11-01 19:14:35 +01:00
continue;
}
if (count($closure_type->params) > $max_closure_param_count) {
if (IssueBuffer::accepts(
new TooManyArguments(
'Too many arguments in closure for ' . $method_id,
new CodeLocation($statements_checker->getSource(), $closure_arg)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif (count($closure_type->params) < $min_closure_param_count) {
if (IssueBuffer::accepts(
new TooFewArguments(
'You must supply a param in the closure for ' . $method_id,
new CodeLocation($statements_checker->getSource(), $closure_arg)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-11-01 19:14:35 +01:00
}
// abandon attempt to validate closure params if we have an extra arg for ARRAY_FILTER
if ($method_id === 'array_filter' && count($args) > 2) {
continue;
}
2016-11-01 19:14:35 +01:00
$closure_params = $closure_type->params;
2016-11-01 19:14:35 +01:00
$i = 0;
2017-02-11 01:10:13 +01:00
foreach ($closure_params as $closure_param) {
if (!isset($array_arg_types[$i])) {
2017-05-27 02:05:57 +02:00
++$i;
continue;
}
2016-12-07 20:13:39 +01:00
/** @var Type\Atomic\TArray */
$array_arg_type = $array_arg_types[$i];
2016-11-01 19:14:35 +01:00
$input_type = $array_arg_type->type_params[1];
2016-11-01 19:14:35 +01:00
if ($input_type->isMixed()) {
2017-05-27 02:05:57 +02:00
++$i;
2016-11-01 19:14:35 +01:00
continue;
}
$closure_param_type = $closure_param->type;
if (!$closure_param_type) {
++$i;
continue;
}
$type_match_found = TypeChecker::isContainedBy(
2018-02-01 06:50:01 +01:00
$project_checker->codebase,
$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 ' .
$closure_param_type . ', parent type ' . $input_type . ' provided',
new CodeLocation($statements_checker->getSource(), $closure_arg)
),
$statements_checker->getSuppressedIssues()
)) {
// keep soldiering on
}
} else {
if (IssueBuffer::accepts(
new TypeCoercion(
'First parameter of closure passed to function ' . $method_id . ' expects ' .
$closure_param_type . ', parent type ' . $input_type . ' provided',
new CodeLocation($statements_checker->getSource(), $closure_arg)
),
$statements_checker->getSuppressedIssues()
)) {
// keep soldiering on
}
2016-11-01 19:14:35 +01:00
}
}
if (!$type_coerced && !$type_match_found) {
$types_can_be_identical = TypeChecker::canBeIdenticalTo(
2018-02-01 06:50:01 +01:00
$project_checker->codebase,
$input_type,
$closure_param_type
);
if ($scalar_type_match_found) {
2016-11-01 19:14:35 +01:00
if (IssueBuffer::accepts(
new InvalidScalarArgument(
'First parameter of closure passed to function ' . $method_id . ' expects ' .
2016-12-07 20:13:39 +01:00
$closure_param_type . ', ' . $input_type . ' provided',
new CodeLocation($statements_checker->getSource(), $closure_arg)
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif ($types_can_be_identical) {
if (IssueBuffer::accepts(
new PossiblyInvalidArgument(
'First parameter of closure passed to function ' . $method_id . ' expects ' .
$closure_param_type . ', possibly different type ' . $input_type . ' provided',
new CodeLocation($statements_checker->getSource(), $closure_arg)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif (IssueBuffer::accepts(
new InvalidArgument(
'First parameter of closure passed to function ' . $method_id . ' expects ' .
$closure_param_type . ', ' . $input_type . ' provided',
new CodeLocation($statements_checker->getSource(), $closure_arg)
2016-11-02 07:29:00 +01:00
),
2016-11-01 19:14:35 +01:00
$statements_checker->getSuppressedIssues()
)) {
return false;
}
}
2017-05-27 02:05:57 +02:00
++$i;
2016-11-01 19:14:35 +01:00
}
}
}
}
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param Type\Union $input_type
* @param Type\Union $param_type
* @param string|null $cased_method_id
2016-11-02 07:29:00 +01:00
* @param int $argument_offset
* @param CodeLocation $code_location
* @param bool $by_ref
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
*/
public static function checkFunctionArgumentType(
2016-11-01 19:14:35 +01:00
StatementsChecker $statements_checker,
Type\Union $input_type,
Type\Union $param_type,
$cased_method_id,
$argument_offset,
CodeLocation $code_location,
PhpParser\Node\Expr $input_expr,
Context $context,
$by_ref = false
2016-11-01 19:14:35 +01:00
) {
if ($param_type->isMixed()) {
2016-11-02 07:29:00 +01:00
return null;
2016-11-01 19:14:35 +01:00
}
$project_checker = $statements_checker->getFileChecker()->project_checker;
$method_identifier = $cased_method_id ? ' of ' . $cased_method_id : '';
if ($project_checker->infer_types_from_usage && $input_expr->inferredType) {
$source_checker = $statements_checker->getSource();
if ($source_checker instanceof FunctionLikeChecker) {
$context->inferType(
$input_expr,
$source_checker->getFunctionLikeStorage($statements_checker),
$param_type
);
}
}
2016-11-01 19:14:35 +01:00
if ($input_type->isMixed()) {
$project_checker->codebase->incrementMixedCount($statements_checker->getCheckedFilePath());
2016-11-01 19:14:35 +01:00
if (IssueBuffer::accepts(
new MixedArgument(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be mixed, expecting ' .
2016-11-02 07:29:00 +01:00
$param_type,
$code_location
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-11-02 07:29:00 +01:00
return null;
2016-11-01 19:14:35 +01:00
}
$project_checker->codebase->incrementNonMixedCount($statements_checker->getCheckedFilePath());
2017-02-11 23:55:08 +01:00
if (!$param_type->isNullable() && $cased_method_id !== 'echo') {
if ($input_type->isNull()) {
if (IssueBuffer::accepts(
new NullArgument(
2017-02-27 16:53:32 +01:00
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be null, ' .
2017-02-11 23:55:08 +01:00
'null value provided',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
return null;
}
if ($input_type->isNullable() && !$input_type->ignore_nullable_issues) {
2017-02-11 23:55:08 +01:00
if (IssueBuffer::accepts(
new PossiblyNullArgument(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be null, possibly ' .
'null value provided',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-11-01 19:14:35 +01:00
}
}
if ($input_type->isFalsable() && !$param_type->hasBool() && !$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
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
}
$param_type = TypeChecker::simplifyUnionType(
2018-02-01 06:50:01 +01:00
$project_checker->codebase,
$param_type
);
$type_match_found = TypeChecker::isContainedBy(
2018-02-01 06:50:01 +01:00
$project_checker->codebase,
2016-11-02 07:29:00 +01:00
$input_type,
$param_type,
true,
true,
2016-11-02 07:29:00 +01:00
$scalar_type_match_found,
$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
if ($type_coerced) {
if ($type_coerced_from_mixed) {
if (IssueBuffer::accepts(
new MixedTypeCoercion(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
', parent type ' . $input_type . ' provided',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
// keep soldiering on
}
} else {
if (IssueBuffer::accepts(
new TypeCoercion(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
', parent type ' . $input_type . ' provided',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
// keep soldiering on
}
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(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' .
2016-12-29 06:14:06 +01:00
$param_type . ', ' . $input_type . ' provided with a __toString method',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
if (!$type_match_found && !$type_coerced) {
$types_can_be_identical = TypeChecker::canBeIdenticalTo(
2018-02-01 06:50:01 +01:00
$project_checker->codebase,
$param_type,
$input_type
);
2016-11-01 19:14:35 +01:00
if ($scalar_type_match_found) {
if ($cased_method_id !== 'echo') {
if (IssueBuffer::accepts(
new InvalidScalarArgument(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' .
2016-12-29 06:14:06 +01:00
$param_type . ', ' . $input_type . ' provided',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-11-01 19:14:35 +01:00
}
} elseif ($types_can_be_identical) {
if (IssueBuffer::accepts(
new PossiblyInvalidArgument(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
', possibly different type ' . $input_type . ' provided',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif (IssueBuffer::accepts(
2016-11-01 19:14:35 +01:00
new InvalidArgument(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
2016-11-02 07:29:00 +01:00
', ' . $input_type . ' provided',
$code_location
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif ($input_expr instanceof PhpParser\Node\Scalar\String_
|| $input_expr instanceof PhpParser\Node\Expr\Array_
) {
foreach ($param_type->getTypes() as $param_type_part) {
if ($param_type_part instanceof TCallable) {
$function_ids = self::getFunctionIdsFromCallableArg(
$statements_checker,
$input_expr
);
foreach ($function_ids as $function_id) {
if (strpos($function_id, '::') !== false) {
list($callable_fq_class_name) = explode('::', $function_id);
if (!in_array(strtolower($callable_fq_class_name), ['self', 'static', 'parent'], true)) {
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
2018-01-03 03:23:48 +01:00
$statements_checker,
$callable_fq_class_name,
$code_location,
$statements_checker->getSuppressedIssues()
) === false
) {
return false;
}
if (MethodChecker::checkMethodExists(
2018-01-03 03:23:48 +01:00
$project_checker,
$function_id,
$code_location,
$statements_checker->getSuppressedIssues()
) === false
) {
return false;
}
}
} else {
if (self::checkFunctionExists(
2018-01-03 03:23:48 +01:00
$statements_checker,
$function_id,
$code_location
) === false
) {
return false;
}
}
}
}
}
2016-11-01 19:14:35 +01:00
}
2016-11-02 07:29:00 +01:00
if ($type_match_found
&& !$param_type->isMixed()
&& !$param_type->from_docblock
&& !$by_ref
) {
$var_id = ExpressionChecker::getVarId(
$input_expr,
$statements_checker->getFQCLN(),
$statements_checker
);
if ($var_id) {
if ($input_type->isNullable() && !$param_type->isNullable()) {
$input_type->removeType('null');
}
if ($input_type->getId() === $param_type->getId()) {
$input_type->from_docblock = false;
}
$context->removeVarFromConflictingClauses($var_id, null, $statements_checker);
2017-12-18 05:22:26 +01:00
$context->vars_in_scope[$var_id] = $input_type;
}
}
2016-11-02 07:29:00 +01:00
return null;
2016-11-01 19:14:35 +01:00
}
/**
* @param PhpParser\Node\Scalar\String_|PhpParser\Node\Expr\Array_ $callable_arg
*
* @return string[]
*/
public static function getFunctionIdsFromCallableArg(
StatementsChecker $statements_checker,
$callable_arg
) {
if ($callable_arg instanceof PhpParser\Node\Scalar\String_) {
return [preg_replace('/^\\\/', '', $callable_arg->value)];
}
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');
}
$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_) {
return [preg_replace('/^\\\/', '', $class_arg->value) . '::' . $method_name_arg->value];
}
if ($class_arg instanceof PhpParser\Node\Expr\ClassConstFetch
&& is_string($class_arg->name)
&& strtolower($class_arg->name) === 'class'
&& $class_arg->class instanceof PhpParser\Node\Name
) {
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
$class_arg->class,
$statements_checker->getAliases()
);
return [$fq_class_name . '::' . $method_name_arg->value];
}
if (!isset($class_arg->inferredType) || !$class_arg->inferredType->hasObjectType()) {
return [];
}
$method_ids = [];
foreach ($class_arg->inferredType->getTypes() as $type_part) {
if ($type_part instanceof TNamedObject) {
$method_ids[] = $type_part . '::' . $method_name_arg->value;
}
}
return $method_ids;
}
2016-11-01 19:14:35 +01:00
/**
* @param StatementsChecker $statements_checker
* @param string $function_id
* @param CodeLocation $code_location
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(
StatementsChecker $statements_checker,
&$function_id,
CodeLocation $code_location
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);
$codebase = $statements_checker->getFileChecker()->project_checker->codebase;
if (!$codebase->functionExists($statements_checker, $function_id)) {
$root_function_id = preg_replace('/.*\\\/', '', $function_id);
if ($function_id !== $root_function_id
&& $codebase->functionExists($statements_checker, $root_function_id)
) {
$function_id = $root_function_id;
} else {
if (IssueBuffer::accepts(
new UndefinedFunction(
'Function ' . $cased_function_id . ' does not exist',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
return false;
2016-11-01 19:14:35 +01:00
}
}
return true;
}
}