2016-11-01 19:14:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
2017-03-18 18:37:00 +01:00
|
|
|
use Psalm\Checker\AlgebraChecker;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Checker\ClassChecker;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\FunctionChecker;
|
|
|
|
use Psalm\Checker\FunctionLikeChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
2017-07-29 21:05:06 +02:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
2016-12-11 19:48:11 +01:00
|
|
|
use Psalm\Checker\TypeChecker;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2017-01-16 02:11:02 +01:00
|
|
|
use Psalm\Config;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
2016-12-31 16:51:42 +01:00
|
|
|
use Psalm\FunctionLikeParameter;
|
2017-01-20 06:10:10 +01:00
|
|
|
use Psalm\Issue\AbstractInstantiation;
|
2017-05-25 06:34:39 +02:00
|
|
|
use Psalm\Issue\DeprecatedClass;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2016-12-29 06:14:06 +01:00
|
|
|
use Psalm\Issue\ImplicitToStringCast;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\InvalidArgument;
|
2016-12-31 16:51:42 +01:00
|
|
|
use Psalm\Issue\InvalidFunctionCall;
|
2017-04-08 15:20:32 +02:00
|
|
|
use Psalm\Issue\InvalidMethodCall;
|
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\InvalidScope;
|
|
|
|
use Psalm\Issue\MixedArgument;
|
|
|
|
use Psalm\Issue\MixedMethodCall;
|
2017-11-19 19:42:48 +01:00
|
|
|
use Psalm\Issue\MixedTypeCoercion;
|
2016-12-14 18:54:34 +01:00
|
|
|
use Psalm\Issue\NullArgument;
|
2017-04-08 17:38:06 +02:00
|
|
|
use Psalm\Issue\NullFunctionCall;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\NullReference;
|
|
|
|
use Psalm\Issue\ParentNotFound;
|
2017-10-23 17:47:00 +02:00
|
|
|
use Psalm\Issue\PossiblyFalseArgument;
|
|
|
|
use Psalm\Issue\PossiblyFalseReference;
|
2017-04-08 15:28:02 +02:00
|
|
|
use Psalm\Issue\PossiblyInvalidArgument;
|
2017-11-15 17:34:40 +01:00
|
|
|
use Psalm\Issue\PossiblyInvalidMethodCall;
|
2017-02-11 23:55:08 +01:00
|
|
|
use Psalm\Issue\PossiblyNullArgument;
|
2017-04-08 17:38:06 +02:00
|
|
|
use Psalm\Issue\PossiblyNullFunctionCall;
|
2017-02-11 23:55:08 +01:00
|
|
|
use Psalm\Issue\PossiblyNullReference;
|
2017-11-11 20:53:35 +01:00
|
|
|
use Psalm\Issue\PossiblyUndefinedMethod;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\TooFewArguments;
|
|
|
|
use Psalm\Issue\TooManyArguments;
|
|
|
|
use Psalm\Issue\TypeCoercion;
|
|
|
|
use Psalm\Issue\UndefinedFunction;
|
2017-09-14 06:32:14 +02:00
|
|
|
use Psalm\Issue\UndefinedMethod;
|
2016-11-01 19:14:35 +01:00
|
|
|
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;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
|
|
|
use Psalm\Type\Atomic\TCallable;
|
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
class CallChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\FuncCall $stmt
|
|
|
|
* @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
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeFunctionCall(
|
2017-07-29 21:05:06 +02:00
|
|
|
ProjectChecker $project_checker,
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$method = $stmt->name;
|
|
|
|
|
|
|
|
if ($method instanceof PhpParser\Node\Name) {
|
|
|
|
$first_arg = isset($stmt->args[0]) ? $stmt->args[0] : null;
|
|
|
|
|
|
|
|
if ($method->parts === ['method_exists']) {
|
|
|
|
$context->check_methods = false;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($method->parts === ['class_exists']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($first_arg && $first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$context->addPhantomClass($first_arg->value->value);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->check_classes = false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($method->parts === ['function_exists']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->check_functions = false;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($method->parts === ['is_callable']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->check_methods = false;
|
|
|
|
$context->check_functions = false;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($method->parts === ['defined']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->check_consts = false;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($method->parts === ['extract']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->check_variables = false;
|
2016-12-12 05:40:46 +01:00
|
|
|
} elseif ($method->parts === ['var_dump'] || $method->parts === ['shell_exec']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new ForbiddenCode(
|
|
|
|
'Unsafe ' . implode('', $method->parts),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-11-01 19:14:35 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($method->parts === ['define']) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($first_arg && $first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$second_arg = $stmt->args[1];
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($statements_checker, $second_arg->value, $context);
|
2016-11-01 19:14:35 +01:00
|
|
|
$const_name = $first_arg->value->value;
|
|
|
|
|
|
|
|
$statements_checker->setConstType(
|
|
|
|
$const_name,
|
2017-01-02 21:31:18 +01:00
|
|
|
isset($second_arg->value->inferredType) ? $second_arg->value->inferredType : Type::getMixed(),
|
|
|
|
$context
|
2016-11-01 19:14:35 +01:00
|
|
|
);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->check_consts = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_id = null;
|
2017-02-15 07:47:03 +01:00
|
|
|
$function_params = null;
|
|
|
|
$in_call_map = false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-15 07:47:03 +01:00
|
|
|
$is_stubbed = false;
|
2017-02-11 00:12:59 +01:00
|
|
|
|
2017-02-15 07:47:03 +01:00
|
|
|
$function_storage = null;
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2017-02-15 07:47:03 +01:00
|
|
|
$code_location = new CodeLocation($statements_checker->getSource(), $stmt);
|
|
|
|
$defined_constants = [];
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
$function_exists = false;
|
2017-01-15 22:43:49 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Expr) {
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->name, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if (isset($stmt->name->inferredType)) {
|
2017-04-08 17:38:06 +02:00
|
|
|
if ($stmt->name->inferredType->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullFunctionCall(
|
|
|
|
'Cannot call function on null value',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->name->inferredType->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullFunctionCall(
|
|
|
|
'Cannot call function on possibly null value',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
foreach ($stmt->name->inferredType->types as $var_type_part) {
|
|
|
|
if ($var_type_part instanceof Type\Atomic\Fn) {
|
|
|
|
$function_params = $var_type_part->params;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes(
|
|
|
|
$stmt->inferredType,
|
|
|
|
$var_type_part->return_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = $var_type_part->return_type;
|
2017-02-17 02:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$function_exists = true;
|
2017-04-08 17:38:06 +02:00
|
|
|
} elseif ($var_type_part instanceof TMixed) {
|
|
|
|
// @todo maybe emit issue here
|
|
|
|
} elseif (($var_type_part instanceof TNamedObject && $var_type_part->value === 'Closure') ||
|
|
|
|
$var_type_part instanceof TCallable
|
|
|
|
) {
|
|
|
|
// this is fine
|
|
|
|
} elseif ($var_type_part instanceof TNull) {
|
|
|
|
// handled above
|
|
|
|
} elseif (!$var_type_part instanceof TNamedObject ||
|
|
|
|
!ClassLikeChecker::classOrInterfaceExists(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
|
|
|
$var_type_part->value
|
2017-04-08 17:38:06 +02:00
|
|
|
) ||
|
|
|
|
!MethodChecker::methodExists(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
|
|
|
$var_type_part->value . '::__invoke'
|
2017-02-17 02:51:53 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->name,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidFunctionCall(
|
|
|
|
'Cannot treat ' . $var_id . ' of type ' . $var_type_part . ' as function',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 02:51:53 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$method_id = implode('\\', $stmt->name->parts);
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
$in_call_map = FunctionChecker::inCallMap($method_id);
|
|
|
|
$is_stubbed = isset(FunctionChecker::$stubbed_functions[strtolower($method_id)]);
|
2017-01-16 02:11:02 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
$is_predefined = true;
|
2017-01-16 02:11:02 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if (!$in_call_map) {
|
|
|
|
$predefined_functions = Config::getInstance()->getPredefinedFunctions();
|
|
|
|
$is_predefined = isset($predefined_functions[$method_id]);
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if (!$in_call_map && !$stmt->name instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$method_id = FunctionChecker::getFQFunctionNameFromString($method_id, $statements_checker);
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if (!$in_call_map && !$is_stubbed) {
|
|
|
|
if ($context->check_functions) {
|
2017-02-10 02:35:17 +01:00
|
|
|
if (self::checkFunctionExists(
|
|
|
|
$statements_checker,
|
|
|
|
$method_id,
|
|
|
|
$code_location
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-11 00:12:59 +01:00
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
$function_exists = FunctionChecker::functionExists(
|
2017-08-15 01:30:11 +02:00
|
|
|
$statements_checker,
|
|
|
|
strtolower($method_id)
|
2017-02-17 02:51:53 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$function_exists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_exists) {
|
2017-02-11 00:12:59 +01:00
|
|
|
if (!$in_call_map || $is_stubbed) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$function_storage = FunctionChecker::getStorage(
|
2017-08-15 01:30:11 +02:00
|
|
|
$statements_checker,
|
|
|
|
strtolower($method_id)
|
2017-02-10 02:35:17 +01:00
|
|
|
);
|
2017-01-15 22:43:49 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$function_params = $function_storage->params;
|
|
|
|
|
|
|
|
if (!$is_predefined) {
|
2017-02-11 00:12:59 +01:00
|
|
|
$defined_constants = $function_storage->defined_constants;
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
2017-02-11 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($in_call_map && !$is_stubbed) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$function_params = FunctionLikeChecker::getFunctionParamsFromCallMapById(
|
2017-07-29 21:05:06 +02:00
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2017-01-15 22:43:49 +01:00
|
|
|
$method_id,
|
2017-07-29 21:05:06 +02:00
|
|
|
$stmt->args
|
2017-01-15 22:43:49 +01:00
|
|
|
);
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2017-02-15 07:47:03 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-15 07:47:03 +01:00
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$function_params,
|
2017-10-28 19:56:29 +02:00
|
|
|
$method_id,
|
2017-02-15 07:47:03 +01:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-04-15 03:32:14 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2017-02-17 02:51:53 +01:00
|
|
|
if ($function_exists) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$generic_params = null;
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $method_id) {
|
2017-02-11 00:12:59 +01:00
|
|
|
if (!$is_stubbed && $in_call_map) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$function_params = FunctionLikeChecker::getFunctionParamsFromCallMapById(
|
2017-07-29 21:05:06 +02:00
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2017-02-10 02:35:17 +01:00
|
|
|
$method_id,
|
2017-07-29 21:05:06 +02:00
|
|
|
$stmt->args
|
2017-02-10 02:35:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
// do this here to allow closure param checks
|
|
|
|
if (self::checkFunctionArgumentsMatch(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$method_id,
|
|
|
|
$function_params ?: [],
|
|
|
|
$function_storage,
|
|
|
|
null,
|
|
|
|
$generic_params,
|
|
|
|
$code_location,
|
2017-09-03 00:15:52 +02:00
|
|
|
$context
|
2017-07-25 22:11:02 +02:00
|
|
|
) === false) {
|
|
|
|
// fall through
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $method_id) {
|
2017-02-11 00:12:59 +01:00
|
|
|
if (!$in_call_map || $is_stubbed) {
|
2017-02-11 04:51:47 +01:00
|
|
|
if ($function_storage && $function_storage->template_types) {
|
2017-02-12 01:30:06 +01:00
|
|
|
foreach ($function_storage->template_types as $template_name => $_) {
|
2017-02-11 04:51:47 +01:00
|
|
|
if (!isset($generic_params[$template_name])) {
|
|
|
|
$generic_params[$template_name] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
try {
|
2017-03-02 06:00:31 +01:00
|
|
|
if ($function_storage && $function_storage->return_type) {
|
2017-12-03 17:23:40 +01:00
|
|
|
$return_type = clone $function_storage->return_type;
|
|
|
|
|
|
|
|
if ($generic_params && $function_storage->template_types) {
|
|
|
|
$return_type->replaceTemplateTypesWithArgTypes(
|
2017-04-28 06:31:55 +02:00
|
|
|
$generic_params
|
|
|
|
);
|
2017-03-01 17:56:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_location = $function_storage->return_type_location;
|
|
|
|
|
|
|
|
$stmt->inferredType = $return_type;
|
|
|
|
|
|
|
|
// only check the type locally if it's defined externally
|
|
|
|
if ($return_type_location &&
|
|
|
|
!$is_stubbed && // makes lookups or array_* functions quicker
|
|
|
|
!$config->isInProjectDirs($return_type_location->file_path)
|
|
|
|
) {
|
|
|
|
$return_type->check(
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
$context->getPhantomClasses()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// this can happen when the function was defined in the Config startup script
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
2017-02-11 00:12:59 +01:00
|
|
|
} else {
|
|
|
|
$stmt->inferredType = FunctionChecker::getReturnTypeFromCallMapWithArgs(
|
2017-08-15 01:30:11 +02:00
|
|
|
$statements_checker,
|
2017-02-11 00:12:59 +01:00
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 22:43:49 +01:00
|
|
|
foreach ($defined_constants as $const_name => $const_type) {
|
|
|
|
$context->constants[$const_name] = clone $const_type;
|
|
|
|
$context->vars_in_scope[$const_name] = clone $const_type;
|
|
|
|
}
|
|
|
|
|
2017-01-28 21:17:14 +01:00
|
|
|
if (Config::getInstance()->use_assert_for_type &&
|
|
|
|
$method instanceof PhpParser\Node\Name &&
|
|
|
|
$method->parts === ['assert'] &&
|
|
|
|
isset($stmt->args[0])
|
|
|
|
) {
|
2017-03-18 18:37:00 +01:00
|
|
|
$assert_clauses = AlgebraChecker::getFormula(
|
2017-01-28 21:17:14 +01:00
|
|
|
$stmt->args[0]->value,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$simplified_clauses = AlgebraChecker::simplifyCNF(array_merge($context->clauses, $assert_clauses));
|
2017-01-28 21:17:14 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$assert_type_assertions = AlgebraChecker::getTruthsFromFormula($simplified_clauses);
|
2017-01-28 21:17:14 +01:00
|
|
|
|
|
|
|
$changed_vars = [];
|
|
|
|
|
|
|
|
// while in an and, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x && $x->foo())
|
|
|
|
$op_vars_in_scope = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$assert_type_assertions,
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$changed_vars,
|
2017-11-28 06:46:41 +01:00
|
|
|
[],
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2017-01-28 21:17:14 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($op_vars_in_scope === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-09 04:17:16 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 21:17:14 +01:00
|
|
|
$context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-04-15 03:32:14 +02:00
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
|
|
|
|
2016-12-06 19:38:11 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
($stmt->name->parts === ['get_class'] || $stmt->name->parts === ['gettype']) &&
|
|
|
|
$stmt->args
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$var = $stmt->args[0]->value;
|
|
|
|
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
|
2017-09-11 17:52:34 +02:00
|
|
|
$atomic_type = $stmt->name->parts === ['get_class']
|
|
|
|
? new Type\Atomic\GetClassT('$' . $var->name)
|
|
|
|
: new Type\Atomic\GetTypeT('$' . $var->name);
|
|
|
|
|
|
|
|
$stmt->inferredType = new Type\Union([$atomic_type]);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\New_ $stmt
|
|
|
|
* @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
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeNew(
|
2016-11-01 19:14:35 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\New_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2017-01-09 06:27:04 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
$late_static = false;
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
2017-11-15 03:56:29 +01:00
|
|
|
if (!in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)) {
|
2016-12-17 01:22:30 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
|
|
|
$stmt->class,
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_checker->getAliases()
|
2016-12-17 01:22:30 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-17 01:22:30 +01:00
|
|
|
if ($context->check_classes) {
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 01:16:51 +01:00
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
2017-02-12 01:30:06 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
switch ($stmt->class->parts[0]) {
|
|
|
|
case 'self':
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = $context->self;
|
2016-11-01 19:14:35 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'parent':
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = $context->parent;
|
2016-11-01 19:14:35 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'static':
|
|
|
|
// @todo maybe we can do better here
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = $context->self;
|
2017-01-20 06:10:10 +01:00
|
|
|
$late_static = true;
|
2016-11-01 19:14:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt->class instanceof PhpParser\Node\Stmt\Class_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$statements_checker->analyze([$stmt->class], $context);
|
2017-07-25 22:11:02 +02:00
|
|
|
$fq_class_name = ClassChecker::getAnonymousClassName($stmt->class, $statements_checker->getFilePath());
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($statements_checker, $stmt->class, $context);
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($fq_class_name) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$stmt->inferredType = new Type\Union([new TNamedObject($fq_class_name)]);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
if (strtolower($fq_class_name) !== 'stdclass' &&
|
2017-01-19 17:45:25 +01:00
|
|
|
$context->check_classes &&
|
2017-07-29 21:05:06 +02:00
|
|
|
ClassChecker::classExists($project_checker, $fq_class_name)
|
2017-01-09 05:58:06 +01:00
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-01-20 06:10:10 +01:00
|
|
|
|
|
|
|
// if we're not calling this constructor via new static()
|
|
|
|
if ($storage->abstract && !$late_static) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new AbstractInstantiation(
|
|
|
|
'Unable to instantiate a abstract class ' . $fq_class_name,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2017-05-25 06:34:39 +02:00
|
|
|
if ($storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedClass(
|
|
|
|
$fq_class_name . ' is marked deprecated',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 05:09:18 +01:00
|
|
|
if (MethodChecker::methodExists(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-02-27 05:09:18 +01:00
|
|
|
$fq_class_name . '::__construct',
|
|
|
|
$context->collect_references ? new CodeLocation($statements_checker->getSource(), $stmt) : null
|
|
|
|
)) {
|
2017-01-20 06:10:10 +01:00
|
|
|
$method_id = $fq_class_name . '::__construct';
|
2017-01-02 02:10:28 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$method_id,
|
2017-01-20 06:10:10 +01:00
|
|
|
$stmt->args,
|
2017-02-10 02:35:17 +01:00
|
|
|
$found_generic_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker
|
2017-01-20 06:10:10 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
|
|
|
if (MethodChecker::checkMethodVisibility(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$generic_params = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($storage->template_types) {
|
|
|
|
foreach ($storage->template_types as $template_name => $_) {
|
|
|
|
if (isset($found_generic_params[$template_name])) {
|
|
|
|
$generic_params[] = $found_generic_params[$template_name];
|
|
|
|
} else {
|
|
|
|
$generic_params[] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 06:10:10 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
if ($fq_class_name === 'ArrayIterator' && isset($stmt->args[0]->value->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$first_arg_type = $stmt->args[0]->value->inferredType;
|
|
|
|
|
|
|
|
if ($first_arg_type->hasGeneric()) {
|
|
|
|
$key_type = null;
|
|
|
|
$value_type = null;
|
|
|
|
|
|
|
|
foreach ($first_arg_type->types as $type) {
|
|
|
|
if ($type instanceof Type\Atomic\TArray) {
|
|
|
|
$first_type_param = count($type->type_params) ? $type->type_params[0] : null;
|
|
|
|
$last_type_param = $type->type_params[count($type->type_params) - 1];
|
|
|
|
|
|
|
|
if ($value_type === null) {
|
|
|
|
$value_type = clone $last_type_param;
|
|
|
|
} else {
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $last_type_param);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$key_type || !$first_type_param) {
|
|
|
|
$key_type = $first_type_param ? clone $first_type_param : Type::getMixed();
|
|
|
|
} else {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $first_type_param);
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2017-01-20 06:10:10 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
if ($key_type === null) {
|
|
|
|
throw new \UnexpectedValueException('$key_type cannot be null');
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
if ($value_type === null) {
|
|
|
|
throw new \UnexpectedValueException('$value_type cannot be null');
|
|
|
|
}
|
2016-12-24 19:23:22 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
$stmt->inferredType = new Type\Union([
|
|
|
|
new Type\Atomic\TGenericObject(
|
|
|
|
$fq_class_name,
|
|
|
|
[
|
|
|
|
$key_type,
|
2017-05-27 02:05:57 +02:00
|
|
|
$value_type,
|
2017-01-20 06:10:10 +01:00
|
|
|
]
|
2017-05-27 02:05:57 +02:00
|
|
|
),
|
2017-01-20 06:10:10 +01:00
|
|
|
]);
|
2016-12-24 19:23:22 +01:00
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
} elseif ($generic_params) {
|
|
|
|
$stmt->inferredType = new Type\Union([
|
|
|
|
new Type\Atomic\TGenericObject(
|
|
|
|
$fq_class_name,
|
|
|
|
$generic_params
|
2017-05-27 02:05:57 +02:00
|
|
|
),
|
2017-02-10 02:35:17 +01:00
|
|
|
]);
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2017-05-25 05:11:40 +02:00
|
|
|
} elseif ($stmt->args) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TooManyArguments(
|
|
|
|
'Class ' . $fq_class_name . ' has no __construct, but arguments were passed',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-04-15 03:32:14 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeMethodCall(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\MethodCall $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->var, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_type = null;
|
|
|
|
$method_id = null;
|
|
|
|
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_checker->getClassName()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new InvalidScope(
|
|
|
|
'Use of $this in non-class context',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-11-01 19:14:35 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->var,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-11-16 02:45:53 +01:00
|
|
|
$class_type = $var_id && $context->hasVariable($var_id) ? $context->vars_in_scope[$var_id] : null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$class_type = $stmt->var->inferredType;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif (!$class_type) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
$source = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if (!$context->check_methods || !$context->check_classes) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$has_mock = false;
|
|
|
|
|
2017-02-11 23:55:08 +01:00
|
|
|
if ($class_type && is_string($stmt->name) && $class_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
|
|
|
'Cannot call method ' . $stmt->name . ' on null variable ' . $var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:38:58 +02:00
|
|
|
if ($class_type &&
|
|
|
|
is_string($stmt->name) &&
|
|
|
|
$class_type->isNullable() &&
|
|
|
|
!$class_type->ignore_nullable_issues
|
|
|
|
) {
|
2017-02-11 23:55:08 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullReference(
|
|
|
|
'Cannot call method ' . $stmt->name . ' on possibly null variable ' . $var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 17:47:00 +02:00
|
|
|
if ($class_type &&
|
|
|
|
is_string($stmt->name) &&
|
|
|
|
$class_type->isFalsable()
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyFalseReference(
|
|
|
|
'Cannot call method ' . $stmt->name . ' on possibly false variable ' . $var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
$config = Config::getInstance();
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2017-03-01 17:56:36 +01:00
|
|
|
|
2017-11-11 20:53:35 +01:00
|
|
|
$non_existent_method_ids = [];
|
|
|
|
$existent_method_ids = [];
|
|
|
|
|
2017-11-15 17:34:40 +01:00
|
|
|
$invalid_method_call_types = [];
|
|
|
|
$has_valid_method_call_type = false;
|
|
|
|
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location = new CodeLocation($source, $stmt);
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($class_type && is_string($stmt->name)) {
|
|
|
|
$return_type = null;
|
2017-12-06 05:47:06 +01:00
|
|
|
$method_name_lc = strtolower($stmt->name);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-02 06:20:58 +01:00
|
|
|
foreach ($class_type->types as $class_type_part) {
|
|
|
|
if (!$class_type_part instanceof TNamedObject) {
|
|
|
|
switch (get_class($class_type_part)) {
|
2017-01-15 01:06:58 +01:00
|
|
|
case 'Psalm\\Type\\Atomic\\TNull':
|
2017-10-23 17:47:00 +02:00
|
|
|
case 'Psalm\\Type\\Atomic\\TFalse':
|
2017-02-11 23:55:08 +01:00
|
|
|
// handled above
|
2017-01-15 01:06:58 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Psalm\\Type\\Atomic\\TInt':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TBool':
|
2017-12-09 20:53:39 +01:00
|
|
|
case 'Psalm\\Type\\Atomic\\TTrue':
|
2017-01-15 01:06:58 +01:00
|
|
|
case 'Psalm\\Type\\Atomic\\TArray':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TString':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TNumericString':
|
2017-11-15 17:34:40 +01:00
|
|
|
$invalid_method_call_types[] = (string)$class_type_part;
|
2017-01-15 01:06:58 +01:00
|
|
|
break;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
case 'Psalm\\Type\\Atomic\\TMixed':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TObject':
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedMethodCall(
|
|
|
|
'Cannot call method ' . $stmt->name . ' on a mixed variable ' . $var_id,
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location
|
2017-01-15 01:06:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2017-12-18 17:05:53 +01:00
|
|
|
// fall through
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-11-15 17:34:40 +01:00
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
2017-02-02 06:20:58 +01:00
|
|
|
$fq_class_name = $class_type_part->value;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-04-16 02:36:40 +02:00
|
|
|
$intersection_types = $class_type_part->getIntersectionTypes();
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$is_mock = ExpressionChecker::isMock($fq_class_name);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$has_mock = $has_mock || $is_mock;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($fq_class_name === 'static') {
|
|
|
|
$fq_class_name = (string) $context->self;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($is_mock ||
|
|
|
|
$context->isPhantomClass($fq_class_name)
|
|
|
|
) {
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-27 07:30:44 +01:00
|
|
|
if ($var_id === '$this') {
|
|
|
|
$does_class_exist = true;
|
|
|
|
} else {
|
2017-03-02 18:19:18 +01:00
|
|
|
$does_class_exist = ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-02-27 07:30:44 +01:00
|
|
|
$fq_class_name,
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location,
|
2017-03-02 18:19:18 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
2017-02-27 07:30:44 +01:00
|
|
|
);
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$does_class_exist) {
|
|
|
|
return $does_class_exist;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-09-14 06:32:14 +02:00
|
|
|
if ($fq_class_name === 'iterable') {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMethod(
|
|
|
|
$fq_class_name . ' has no defined methods',
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location
|
2017-09-14 06:32:14 +02:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-06 05:47:06 +01:00
|
|
|
$method_id = $fq_class_name . '::' . $method_name_lc;
|
2017-11-11 00:08:17 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (MethodChecker::methodExists(
|
|
|
|
$project_checker,
|
|
|
|
$fq_class_name . '::__call'
|
|
|
|
)
|
|
|
|
) {
|
2017-11-11 00:08:17 +01:00
|
|
|
if (!MethodChecker::methodExists($project_checker, $method_id)
|
|
|
|
|| !MethodChecker::isMethodVisible(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-04-16 03:10:49 +02:00
|
|
|
if ($var_id === '$this' &&
|
|
|
|
$context->self &&
|
|
|
|
$fq_class_name !== $context->self &&
|
2017-12-06 05:47:06 +01:00
|
|
|
MethodChecker::methodExists($project_checker, $context->self . '::' . $method_name_lc)
|
2017-04-16 03:10:49 +02:00
|
|
|
) {
|
2017-12-06 05:47:06 +01:00
|
|
|
$method_id = $context->self . '::' . $method_name_lc;
|
2017-04-16 03:10:49 +02:00
|
|
|
$fq_class_name = $context->self;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($intersection_types && !MethodChecker::methodExists($project_checker, $method_id)) {
|
2017-04-16 02:36:40 +02:00
|
|
|
foreach ($intersection_types as $intersection_type) {
|
2017-12-06 05:47:06 +01:00
|
|
|
$method_id = $intersection_type->value . '::' . $method_name_lc;
|
2017-04-16 02:36:40 +02:00
|
|
|
$fq_class_name = $intersection_type->value;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (MethodChecker::methodExists($project_checker, $method_id)) {
|
2017-04-16 02:36:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$cased_method_id = $fq_class_name . '::' . $stmt->name;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-11-11 20:53:35 +01:00
|
|
|
if (!MethodChecker::methodExists($project_checker, $method_id, $code_location)) {
|
|
|
|
$non_existent_method_ids[] = $method_id;
|
|
|
|
continue;
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-11-11 20:53:35 +01:00
|
|
|
$existent_method_ids[] = $method_id;
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$class_template_params = null;
|
|
|
|
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable &&
|
|
|
|
($context->collect_initializations || $context->collect_mutations) &&
|
|
|
|
$stmt->var->name === 'this' &&
|
|
|
|
is_string($stmt->name) &&
|
|
|
|
$source instanceof FunctionLikeChecker
|
|
|
|
) {
|
|
|
|
self::collectSpecialInformation($source, $stmt->name, $context);
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
2017-02-13 01:26:23 +01:00
|
|
|
if ($class_storage->template_types) {
|
|
|
|
$class_template_params = [];
|
2017-02-10 02:35:17 +01:00
|
|
|
|
2017-02-13 01:31:35 +01:00
|
|
|
if ($class_type_part instanceof TGenericObject) {
|
|
|
|
$reversed_class_template_types = array_reverse(array_keys($class_storage->template_types));
|
|
|
|
|
|
|
|
$provided_type_param_count = count($class_type_part->type_params);
|
|
|
|
|
|
|
|
foreach ($reversed_class_template_types as $i => $type_name) {
|
|
|
|
if (isset($class_type_part->type_params[$provided_type_param_count - 1 - $i])) {
|
|
|
|
$class_template_params[$type_name] =
|
|
|
|
$class_type_part->type_params[$provided_type_param_count - 1 - $i];
|
|
|
|
} else {
|
|
|
|
$class_template_params[$type_name] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($class_storage->template_types as $type_name => $_) {
|
2017-02-13 01:26:23 +01:00
|
|
|
$class_template_params[$type_name] = Type::getMixed();
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$class_template_params,
|
|
|
|
$context,
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location,
|
2017-02-10 02:35:17 +01:00
|
|
|
$statements_checker
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
$return_type_location = null;
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
2017-03-01 17:56:36 +01:00
|
|
|
|
2017-12-06 05:47:06 +01:00
|
|
|
switch (strtolower($stmt->name)) {
|
|
|
|
case '__tostring':
|
|
|
|
$return_type = Type::getString();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($method_name_lc === '__tostring') {
|
|
|
|
$return_type_candidate = Type::getString();
|
|
|
|
} elseif (FunctionChecker::inCallMap($cased_method_id)) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$return_type_candidate = FunctionChecker::getReturnTypeFromCallMap($method_id);
|
|
|
|
} else {
|
|
|
|
if (MethodChecker::checkMethodVisibility(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location,
|
2017-01-15 01:06:58 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (MethodChecker::checkMethodNotDeprecated(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-15 01:06:58 +01:00
|
|
|
$method_id,
|
2017-11-11 20:53:35 +01:00
|
|
|
$code_location,
|
2017-01-15 01:06:58 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$return_type_candidate = MethodChecker::getMethodReturnType($project_checker, $method_id);
|
2017-03-01 17:56:36 +01:00
|
|
|
|
|
|
|
if ($return_type_candidate) {
|
2017-12-03 17:23:40 +01:00
|
|
|
$return_type_candidate = clone $return_type_candidate;
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
if ($class_template_params) {
|
2017-12-03 17:23:40 +01:00
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
2017-04-28 06:31:55 +02:00
|
|
|
$class_template_params
|
|
|
|
);
|
2017-03-01 17:56:36 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$return_type_candidate = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
2017-03-01 17:56:36 +01:00
|
|
|
$return_type_candidate,
|
|
|
|
$fq_class_name,
|
|
|
|
$method_id
|
|
|
|
);
|
|
|
|
|
2017-04-28 06:31:55 +02:00
|
|
|
$return_type_location = MethodChecker::getMethodReturnTypeLocation(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-04-28 06:31:55 +02:00
|
|
|
$method_id,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
2017-03-01 17:56:36 +01:00
|
|
|
|
|
|
|
if ($secondary_return_type_location) {
|
|
|
|
$return_type_location = $secondary_return_type_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only check the type locally if it's defined externally
|
|
|
|
if ($return_type_location && !$config->isInProjectDirs($return_type_location->file_path)) {
|
|
|
|
$return_type_candidate->check(
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($source, $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
$context->getPhantomClasses()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($return_type_candidate) {
|
|
|
|
if (!$return_type) {
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
} else {
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$return_type = Type::getMixed();
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 17:34:40 +01:00
|
|
|
if ($invalid_method_call_types) {
|
2017-12-03 23:21:20 +01:00
|
|
|
$invalid_class_type = $invalid_method_call_types[0];
|
2017-11-15 17:34:40 +01:00
|
|
|
|
|
|
|
if ($has_valid_method_call_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidMethodCall(
|
2017-12-03 23:21:20 +01:00
|
|
|
'Cannot call method on possible ' . $invalid_class_type . ' variable ' . $var_id,
|
2017-11-15 17:34:40 +01:00
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidMethodCall(
|
2017-12-03 23:21:20 +01:00
|
|
|
'Cannot call method on ' . $invalid_class_type . ' variable ' . $var_id,
|
2017-11-15 17:34:40 +01:00
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 20:53:35 +01:00
|
|
|
if ($non_existent_method_ids) {
|
|
|
|
if ($existent_method_ids) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedMethod(
|
|
|
|
'Method ' . $non_existent_method_ids[0] . ' does not exist',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMethod(
|
|
|
|
'Method ' . $non_existent_method_ids[0] . ' does not exist',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = $return_type;
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($method_id === null) {
|
|
|
|
return self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$found_generic_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker
|
|
|
|
);
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2017-04-15 03:32:14 +02:00
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
2017-12-03 23:21:20 +01:00
|
|
|
|
|
|
|
// if we called a method on this nullable variable, remove the nullable status here
|
|
|
|
// because any further calls must have worked
|
|
|
|
if ($var_id
|
|
|
|
&& $class_type
|
|
|
|
&& $has_valid_method_call_type
|
|
|
|
&& !$invalid_method_call_types
|
|
|
|
&& $existent_method_ids
|
|
|
|
&& ($class_type->from_docblock || $class_type->isNullable())
|
|
|
|
) {
|
|
|
|
$keys_to_remove = [];
|
|
|
|
|
|
|
|
foreach ($class_type->types as $key => $type) {
|
|
|
|
if (!$type instanceof TNamedObject) {
|
|
|
|
$keys_to_remove[] = $key;
|
|
|
|
} else {
|
|
|
|
$type->from_docblock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($keys_to_remove as $key) {
|
|
|
|
unset($class_type->types[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_type->from_docblock = false;
|
2017-12-03 23:36:50 +01:00
|
|
|
|
|
|
|
$context->removeVarFromConflictingClauses($var_id, null, $statements_checker);
|
2017-12-18 05:22:26 +01:00
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $class_type;
|
2017-12-03 23:21:20 +01:00
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
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
|
|
|
*
|
2017-04-06 20:53:45 +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
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($context->collect_mutations &&
|
|
|
|
$context->self &&
|
|
|
|
(
|
|
|
|
$context->self === $fq_class_name ||
|
|
|
|
ClassChecker::classExtends(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-02-10 02:35:17 +01:00
|
|
|
$context->self,
|
|
|
|
$fq_class_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($method_name);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$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 ||
|
|
|
|
ClassChecker::classExtends(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
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);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $method_id);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$method_storage = MethodChecker::getStorage($project_checker, (string)$declaring_method_id);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
|
|
$class_checker = $source->getSource();
|
|
|
|
|
|
|
|
if ($class_checker instanceof ClassLikeChecker &&
|
2017-11-30 05:46:56 +01:00
|
|
|
($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 => $type) {
|
|
|
|
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 => $type) {
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
$local_vars_possibly_in_scope[$var] = $context->vars_possibly_in_scope[$var];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 20:53:45 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($local_vars_possibly_in_scope as $var => $type) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\StaticCall $stmt
|
|
|
|
* @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
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeStaticCall(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\StaticCall $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$method_id = null;
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
$lhs_type = null;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = $statements_checker->getFileChecker();
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $file_checker->project_checker;
|
2017-03-01 17:56:36 +01:00
|
|
|
$source = $statements_checker->getSource();
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-12-17 07:06:12 +01:00
|
|
|
$stmt->inferredType = null;
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-05-27 02:17:56 +02:00
|
|
|
if (count($stmt->class->parts) === 1
|
2017-11-15 03:56:29 +01:00
|
|
|
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
|
2017-05-27 02:17:56 +02:00
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($stmt->class->parts[0] === 'parent') {
|
2017-01-07 20:35:07 +01:00
|
|
|
$fq_class_name = $statements_checker->getParentFQCLN();
|
2016-11-05 02:14:04 +01:00
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($fq_class_name === null) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new ParentNotFound(
|
|
|
|
'Cannot call method on parent as this class does not extend another',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-11-01 19:14:35 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-05 02:14:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-06-30 07:24:45 +02:00
|
|
|
|
|
|
|
if (is_string($stmt->name) && $class_storage->user_defined) {
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($stmt->name);
|
|
|
|
|
|
|
|
$old_context_include_location = $context->include_location;
|
|
|
|
$old_self = $context->self;
|
|
|
|
$context->include_location = new CodeLocation($statements_checker->getSource(), $stmt);
|
|
|
|
$context->self = $fq_class_name;
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($context->collect_mutations) {
|
2017-06-30 07:24:45 +02:00
|
|
|
$file_checker->getMethodMutations($method_id, $context);
|
|
|
|
} elseif ($context->collect_initializations) {
|
|
|
|
$local_vars_in_scope = [];
|
|
|
|
$local_vars_possibly_in_scope = [];
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
$local_vars_in_scope[$var] = $context->vars_in_scope[$var];
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2017-06-30 07:24:45 +02:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
$local_vars_possibly_in_scope[$var] = $context->vars_possibly_in_scope[$var];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_checker->getMethodMutations($method_id, $context);
|
|
|
|
|
|
|
|
foreach ($local_vars_in_scope as $var => $type) {
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($local_vars_possibly_in_scope as $var => $type) {
|
|
|
|
$context->vars_possibly_in_scope[$var] = $type;
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
2017-06-30 07:24:45 +02:00
|
|
|
|
|
|
|
$context->include_location = $old_context_include_location;
|
|
|
|
$context->self = $old_self;
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
} else {
|
2016-11-05 02:14:04 +01:00
|
|
|
$namespace = $statements_checker->getNamespace()
|
2016-11-02 07:29:00 +01:00
|
|
|
? $statements_checker->getNamespace() . '\\'
|
2016-11-05 02:14:04 +01:00
|
|
|
: '';
|
|
|
|
|
2016-12-30 05:37:09 +01:00
|
|
|
$fq_class_name = $context->self ?: $namespace . $statements_checker->getClassName();
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($context->check_classes) {
|
2016-11-08 01:16:51 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->class,
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_checker->getAliases()
|
2016-11-01 19:14:35 +01:00
|
|
|
);
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-12 18:31:39 +01:00
|
|
|
$does_class_exist = false;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($context->self) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$self_storage = $project_checker->classlike_storage_provider->get($context->self);
|
|
|
|
|
|
|
|
if (isset($self_storage->used_traits[strtolower($fq_class_name)])) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$fq_class_name = $context->self;
|
|
|
|
$does_class_exist = true;
|
|
|
|
}
|
2017-02-12 18:31:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$does_class_exist) {
|
|
|
|
$does_class_exist = ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-02-12 18:31:39 +01:00
|
|
|
$fq_class_name,
|
2017-03-01 17:56:36 +01:00
|
|
|
new CodeLocation($source, $stmt->class),
|
2017-03-02 18:19:18 +01:00
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
false
|
2017-02-12 18:31:39 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if (!$does_class_exist) {
|
|
|
|
return $does_class_exist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($fq_class_name) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$lhs_type = new Type\Union([new TNamedObject($fq_class_name)]);
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($statements_checker, $stmt->class, $context);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
/** @var Type\Union */
|
|
|
|
$lhs_type = $stmt->class->inferredType;
|
2017-11-06 21:37:49 +01:00
|
|
|
|
2017-11-06 21:42:39 +01:00
|
|
|
if (!isset($lhs_type) || $lhs_type->hasString()) {
|
2017-11-06 21:37:49 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$context->check_methods || !$lhs_type) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$has_mock = false;
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($lhs_type->types as $lhs_type_part) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$lhs_type_part instanceof TNamedObject) {
|
|
|
|
// @todo deal with it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = $lhs_type_part->value;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$is_mock = ExpressionChecker::isMock($fq_class_name);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
$has_mock = $has_mock || $is_mock;
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
$method_id = null;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (is_string($stmt->name) &&
|
2017-07-29 21:05:06 +02:00
|
|
|
!MethodChecker::methodExists($project_checker, $fq_class_name . '::__callStatic') &&
|
2016-11-02 07:29:00 +01:00
|
|
|
!$is_mock
|
|
|
|
) {
|
2016-11-07 23:29:51 +01:00
|
|
|
$method_id = $fq_class_name . '::' . strtolower($stmt->name);
|
|
|
|
$cased_method_id = $fq_class_name . '::' . $stmt->name;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$does_method_exist = MethodChecker::checkMethodExists(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-11-02 07:29:00 +01:00
|
|
|
$cased_method_id,
|
2017-03-01 17:56:36 +01:00
|
|
|
new CodeLocation($source, $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if (!$does_method_exist) {
|
|
|
|
return $does_method_exist;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-05-25 06:34:39 +02:00
|
|
|
|
|
|
|
if ($class_storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedClass(
|
|
|
|
$fq_class_name . ' is marked deprecated',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (MethodChecker::checkMethodVisibility(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
2017-03-01 17:56:36 +01:00
|
|
|
new CodeLocation($source, $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name
|
2017-01-30 04:13:53 +01:00
|
|
|
&& ($stmt->class->parts[0] !== 'parent' || $statements_checker->isStatic())
|
2017-12-01 01:00:09 +01:00
|
|
|
&& (
|
|
|
|
!$context->self
|
2016-12-12 05:40:46 +01:00
|
|
|
|| $statements_checker->isStatic()
|
2017-07-29 21:05:06 +02:00
|
|
|
|| !ClassChecker::classExtends($project_checker, $context->self, $fq_class_name)
|
2016-12-12 05:40:46 +01:00
|
|
|
)
|
2016-11-01 19:14:35 +01:00
|
|
|
) {
|
2017-01-12 03:37:53 +01:00
|
|
|
if (MethodChecker::checkStatic(
|
2016-11-02 07:29:00 +01:00
|
|
|
$method_id,
|
2017-11-15 03:56:29 +01:00
|
|
|
$stmt->class instanceof PhpParser\Node\Name && strtolower($stmt->class->parts[0]) === 'self',
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-03-01 17:56:36 +01:00
|
|
|
new CodeLocation($source, $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
2016-12-31 15:25:04 +01:00
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (MethodChecker::checkMethodNotDeprecated(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-11-02 07:29:00 +01:00
|
|
|
$method_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
2016-12-31 15:25:04 +01:00
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$found_generic_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
$fq_class_name = $stmt->class instanceof PhpParser\Node\Name && $stmt->class->parts === ['parent']
|
|
|
|
? $statements_checker->getFQCLN()
|
|
|
|
: $fq_class_name;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$return_type_candidate = MethodChecker::getMethodReturnType($project_checker, $method_id);
|
2017-03-01 17:56:36 +01:00
|
|
|
|
|
|
|
if ($return_type_candidate) {
|
2017-12-03 17:23:40 +01:00
|
|
|
$return_type_candidate = clone $return_type_candidate;
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
if ($found_generic_params) {
|
2017-12-03 17:23:40 +01:00
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
2017-04-28 06:31:55 +02:00
|
|
|
$found_generic_params
|
|
|
|
);
|
2017-03-01 17:56:36 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$return_type_candidate = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
2017-03-01 17:56:36 +01:00
|
|
|
$return_type_candidate,
|
|
|
|
$fq_class_name,
|
2016-11-01 19:14:35 +01:00
|
|
|
$method_id
|
|
|
|
);
|
|
|
|
|
2017-04-28 06:31:55 +02:00
|
|
|
$return_type_location = MethodChecker::getMethodReturnTypeLocation(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-04-28 06:31:55 +02:00
|
|
|
$method_id,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
2017-03-01 17:56:36 +01:00
|
|
|
|
|
|
|
if ($secondary_return_type_location) {
|
|
|
|
$return_type_location = $secondary_return_type_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only check the type locally if it's defined externally
|
|
|
|
if ($return_type_location && !$config->isInProjectDirs($return_type_location->file_path)) {
|
|
|
|
$return_type_candidate->check(
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($source, $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
$context->getPhantomClasses()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return_type_candidate) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (isset($stmt->inferredType)) {
|
2017-03-01 17:56:36 +01:00
|
|
|
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, $return_type_candidate);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-03-01 17:56:36 +01:00
|
|
|
$stmt->inferredType = $return_type_candidate;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($method_id === null) {
|
|
|
|
return self::checkMethodArgs(
|
|
|
|
$method_id,
|
2016-12-07 20:13:39 +01:00
|
|
|
$stmt->args,
|
2017-02-10 02:35:17 +01:00
|
|
|
$found_generic_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
2017-04-15 03:32:14 +02:00
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
2016-12-07 20:13:39 +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,
|
2017-04-28 06:31:55 +02:00
|
|
|
&$generic_params,
|
2017-02-10 02:35:17 +01:00
|
|
|
Context $context,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
StatementsChecker $statements_checker
|
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2017-01-02 02:10:28 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$method_params = $method_id
|
2017-07-29 21:05:06 +02:00
|
|
|
? FunctionLikeChecker::getMethodParamsById($project_checker, $method_id, $args)
|
2017-02-10 02:35:17 +01:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
$statements_checker,
|
|
|
|
$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);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$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(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:07:44 +01:00
|
|
|
if (self::checkFunctionArgumentsMatch(
|
|
|
|
$statements_checker,
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-31 16:51:42 +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
|
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(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
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
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
if (ExpressionChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$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 */
|
|
|
|
$array_type = $array_arg->inferredType->types['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
|
|
|
|
|
|
|
if (ExpressionChecker::analyze(
|
|
|
|
$statements_checker,
|
2016-11-02 07:29:00 +01:00
|
|
|
$arg->value,
|
2017-10-28 19:56:29 +02:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$by_ref_type = Type::combineUnionTypes(
|
|
|
|
$by_ref_type,
|
|
|
|
$by_ref_type = new Type\Union(
|
|
|
|
[
|
|
|
|
new TArray(
|
|
|
|
[
|
|
|
|
Type::getInt(),
|
2017-10-28 21:17:32 +02:00
|
|
|
isset($arg->value->inferredType)
|
2017-10-28 19:56:29 +02:00
|
|
|
? clone $arg->value->inferredType
|
|
|
|
: Type::getMixed(),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2017-10-28 19:56:29 +02:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
ExpressionChecker::assignByRefParam(
|
|
|
|
$statements_checker,
|
|
|
|
$array_arg,
|
|
|
|
$by_ref_type,
|
2017-10-28 21:33:29 +02:00
|
|
|
$context,
|
|
|
|
false
|
2017-10-28 19:56:29 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
2017-10-28 19:56:29 +02:00
|
|
|
if ($by_ref && $by_ref_type) {
|
|
|
|
// 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
|
2017-10-28 19:56:29 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (ExpressionChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$arg->value,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-11-20 03:36:09 +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);
|
|
|
|
}
|
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
|
2017-11-20 03:36:09 +01:00
|
|
|
if (in_array($method_id, ['reset', 'end', 'next', 'prev'], 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 */
|
|
|
|
$array_type = $arg->value->inferredType->types['array'];
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionChecker::assignByRefParam(
|
|
|
|
$statements_checker,
|
|
|
|
$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') {
|
|
|
|
if (ExpressionChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$arg->value,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-10-28 19:56:29 +02:00
|
|
|
} else {
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (ExpressionChecker::analyzeVariable(
|
|
|
|
$statements_checker,
|
|
|
|
$arg->value,
|
2017-11-20 06:12:17 +01:00
|
|
|
$context
|
2017-10-28 19:56:29 +02:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch) {
|
|
|
|
if (FetchChecker::analyzePropertyFetch(
|
|
|
|
$statements_checker,
|
|
|
|
$arg->value,
|
|
|
|
$context
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $arg->value, $context) === false) {
|
|
|
|
return false;
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-10-28 19:56:29 +02:00
|
|
|
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) || $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;
|
|
|
|
if (!$statements_checker->hasVariable($var_id)) {
|
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$var_id,
|
|
|
|
new CodeLocation($statements_checker, $arg->value)
|
|
|
|
);
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-31 16:51:42 +01:00
|
|
|
* @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
|
2017-04-28 06:31:55 +02:00
|
|
|
* @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
|
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
|
|
|
|
*/
|
|
|
|
protected static function checkFunctionArgumentsMatch(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
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
|
|
|
) {
|
|
|
|
$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;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$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, '::')) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$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];
|
2017-07-29 21:05:06 +02:00
|
|
|
$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) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId($project_checker, $method_id);
|
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) {
|
|
|
|
$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
|
2017-11-20 06:12:17 +01:00
|
|
|
) {
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Scalar
|
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
|
2017-11-20 06:12:17 +01:00
|
|
|
) {
|
|
|
|
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
|
|
|
}
|
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',
|
2017-11-20 23:34:32 +01:00
|
|
|
'array_push', 'array_unshift', 'socket_select',
|
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 = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$by_ref_type = clone $by_ref_type;
|
|
|
|
|
2017-12-03 17:23:40 +01:00
|
|
|
$by_ref_type->replaceTemplateTypesWithStandins($template_types, $generic_params);
|
2017-11-20 06:12:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)) {
|
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) {
|
|
|
|
if (!$param_type->hasArray() || !$param_type->types['array'] instanceof TArray) {
|
2017-11-20 06:12:17 +01:00
|
|
|
continue;
|
2017-01-17 06:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = clone $param_type->types['array']->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,
|
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->types as $offset_value_type_part) {
|
|
|
|
// register class if the class exists
|
|
|
|
if ($offset_value_type_part instanceof TNamedObject) {
|
|
|
|
ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-02-27 22:38:43 +01:00
|
|
|
$offset_value_type_part->value,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $arg->value),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 19:22:06 +01:00
|
|
|
|
|
|
|
$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) {
|
2017-02-10 04:57:23 +01:00
|
|
|
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(
|
2017-02-10 04:57:23 +01:00
|
|
|
$template_types,
|
|
|
|
$generic_params,
|
|
|
|
$arg->value->inferredType
|
|
|
|
);
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 06:07:58 +01:00
|
|
|
// for now stop when we encounter a packed argument
|
|
|
|
if ($arg->unpack) {
|
2016-11-01 19:14:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$fleshed_out_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
2017-02-10 04:57:23 +01:00
|
|
|
$param_type,
|
|
|
|
$fq_class_name,
|
|
|
|
$method_id
|
|
|
|
);
|
|
|
|
|
2017-09-03 00:15:52 +02:00
|
|
|
if ($context->check_variables) {
|
2017-02-27 17:07:44 +01:00
|
|
|
if (self::checkFunctionArgumentType(
|
|
|
|
$statements_checker,
|
|
|
|
$arg->value->inferredType,
|
|
|
|
$fleshed_out_type,
|
|
|
|
$cased_method_id,
|
|
|
|
$argument_offset,
|
2017-08-12 00:30:58 +02:00
|
|
|
new CodeLocation($statements_checker->getSource(), $arg->value),
|
2017-09-03 00:15:52 +02:00
|
|
|
$arg->value,
|
2017-12-15 22:48:06 +01:00
|
|
|
$context,
|
|
|
|
$function_param->by_ref
|
2017-02-27 17:07:44 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($method_id === 'array_map' || $method_id === 'array_filter') {
|
2017-10-07 16:33:19 +02:00
|
|
|
if ($method_id === 'array_map' && count($args) < 2) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TooFewArguments(
|
|
|
|
'Too few arguments for ' . $method_id,
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if (self::checkArrayFunctionArgumentsMatch(
|
|
|
|
$statements_checker,
|
|
|
|
$args,
|
2017-02-12 01:30:06 +01:00
|
|
|
$method_id
|
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(
|
|
|
|
'Too many arguments for method ' . ($cased_method_id ?: $method_id),
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// 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)) {
|
2017-05-27 02:05:57 +02:00
|
|
|
for ($i = count($args); $i < count($function_params); ++$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(
|
|
|
|
'Too few arguments for method ' . $cased_method_id,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
* @param string|null $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(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
array $args,
|
2017-02-12 01:30:06 +01:00
|
|
|
$method_id
|
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)
|
|
|
|
&& isset($array_arg->inferredType->types['array'])
|
|
|
|
? $array_arg->inferredType->types['array']
|
|
|
|
: 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
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
/** @var ?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-08-15 01:30:11 +02:00
|
|
|
$file_checker = $statements_checker->getFileChecker();
|
|
|
|
|
|
|
|
$project_checker = $file_checker->project_checker;
|
|
|
|
|
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
|
|
|
|
|
|
|
foreach ($closure_arg_type->types as $closure_type) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$closure_type instanceof Type\Atomic\Fn) {
|
2016-11-01 19:14:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-03 18:36:49 +02:00
|
|
|
if (count($closure_type->params) > $max_closure_param_count) {
|
2016-12-31 16:51:42 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-04-03 18:36:49 +02:00
|
|
|
} elseif (count($closure_type->params) < $min_closure_param_count) {
|
2016-12-31 16:51:42 +01:00
|
|
|
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
|
|
|
}
|
2017-04-08 00:30:15 +02: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
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$closure_params = $closure_type->params;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$i = 0;
|
|
|
|
|
2017-02-11 01:10:13 +01:00
|
|
|
foreach ($closure_params as $closure_param) {
|
2017-04-03 18:36:49 +02:00
|
|
|
if (!isset($array_arg_types[$i])) {
|
2017-05-27 02:05:57 +02:00
|
|
|
++$i;
|
2016-12-31 16:51:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
/** @var Type\Atomic\TArray */
|
2016-12-31 16:51:42 +01:00
|
|
|
$array_arg_type = $array_arg_types[$i];
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
$input_type = $array_arg_type->type_params[1];
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($input_type->isMixed()) {
|
2017-05-27 02:05:57 +02:00
|
|
|
++$i;
|
2016-11-01 19:14:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
$closure_param_type = $closure_param->type;
|
|
|
|
|
2017-09-02 17:18:56 +02:00
|
|
|
if (!$closure_param_type) {
|
|
|
|
++$i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
$type_match_found = TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-12-31 16:51:42 +01:00
|
|
|
$input_type,
|
|
|
|
$closure_param_type,
|
|
|
|
false,
|
2017-10-23 17:47:00 +02:00
|
|
|
false,
|
2016-12-31 16:51:42 +01:00
|
|
|
$scalar_type_match_found,
|
2017-11-19 19:42:48 +01:00
|
|
|
$coerced_type,
|
|
|
|
$coerced_type_from_mixed
|
2016-12-31 16:51:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($coerced_type) {
|
2017-11-19 19:42:48 +01:00
|
|
|
if ($coerced_type_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()
|
|
|
|
)) {
|
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(
|
|
|
|
'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()
|
|
|
|
)) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:33:18 +01:00
|
|
|
if (!$coerced_type && !$type_match_found) {
|
2017-04-08 15:47:03 +02:00
|
|
|
$types_can_be_identical = TypeChecker::canBeIdenticalTo(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-04-08 15:47:03 +02:00
|
|
|
$input_type,
|
2017-07-29 21:05:06 +02:00
|
|
|
$closure_param_type
|
2017-04-08 15:47:03 +02:00
|
|
|
);
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($scalar_type_match_found) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-12-31 16:51:42 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-04-08 15:47:03 +02:00
|
|
|
} 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;
|
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
} 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-01-06 07:07:11 +01:00
|
|
|
|
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
|
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
|
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(
|
2016-11-01 19:14:35 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
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,
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-09-03 00:15:52 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
2017-01-13 18:40:01 +01:00
|
|
|
$method_identifier = $cased_method_id ? ' of ' . $cased_method_id : '';
|
|
|
|
|
2017-12-11 03:14:30 +01:00
|
|
|
if ($project_checker->infer_types_from_usage && $input_expr->inferredType) {
|
2017-09-03 00:15:52 +02:00
|
|
|
$source_checker = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if ($source_checker instanceof FunctionLikeChecker) {
|
2017-12-07 21:50:25 +01:00
|
|
|
$context->inferType(
|
|
|
|
$input_expr,
|
|
|
|
$source_checker->getFunctionLikeStorage($statements_checker),
|
|
|
|
$param_type
|
|
|
|
);
|
2017-09-03 00:15:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($input_type->isMixed()) {
|
|
|
|
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
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:38:58 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 17:47:00 +02:00
|
|
|
if ($input_type->isFalsable() && !$param_type->hasBool()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$param_type = TypeChecker::simplifyUnionType(
|
2017-09-03 00:15:52 +02:00
|
|
|
$project_checker,
|
2017-07-29 21:05:06 +02:00
|
|
|
$param_type
|
|
|
|
);
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
$type_match_found = TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
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,
|
2016-12-29 06:14:06 +01:00
|
|
|
$coerced_type,
|
2017-11-19 19:42:48 +01:00
|
|
|
$coerced_type_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 ($coerced_type) {
|
2017-11-19 19:42:48 +01:00
|
|
|
if ($coerced_type_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()
|
|
|
|
)) {
|
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(
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
|
|
|
|
', parent type ' . $input_type . ' provided',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
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 ' .
|
2016-12-29 06:14:06 +01:00
|
|
|
$param_type . ', ' . $input_type . ' provided with a __toString method',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 06:57:31 +01:00
|
|
|
if (!$type_match_found && !$coerced_type) {
|
2017-04-08 15:43:58 +02:00
|
|
|
$types_can_be_identical = TypeChecker::canBeIdenticalTo(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-04-08 15:28:02 +02:00
|
|
|
$param_type,
|
2017-07-29 21:05:06 +02:00
|
|
|
$input_type
|
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 ' .
|
2016-12-29 06:14:06 +01:00
|
|
|
$param_type . ', ' . $input_type . ' provided',
|
2016-12-09 18:53:22 +01:00
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
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(
|
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
|
2017-04-08 15:43:58 +02:00
|
|
|
', possibly different type ' . $input_type . ' provided',
|
2017-04-08 15:28:02 +02:00
|
|
|
$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(
|
2017-01-13 18:40:01 +01:00
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type .
|
2016-11-02 07:29:00 +01:00
|
|
|
', ' . $input_type . ' provided',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
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_
|
|
|
|
) {
|
2017-08-12 00:30:58 +02:00
|
|
|
foreach ($param_type->types as $param_type_part) {
|
|
|
|
if ($param_type_part instanceof TCallable) {
|
2017-08-15 01:30:11 +02:00
|
|
|
$function_ids = self::getFunctionIdsFromCallableArg(
|
|
|
|
$statements_checker,
|
|
|
|
$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) {
|
|
|
|
list($callable_fq_class_name) = explode('::', $function_id);
|
|
|
|
|
2017-11-15 03:56:29 +01:00
|
|
|
if (!in_array(strtolower($callable_fq_class_name), ['self', 'static', 'parent'], true)) {
|
2017-08-15 01:30:11 +02:00
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
|
|
|
$project_checker,
|
|
|
|
$callable_fq_class_name,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-12 00:48:58 +02:00
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
if (MethodChecker::checkMethodExists(
|
|
|
|
$project_checker,
|
|
|
|
$function_id,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-12 00:48:58 +02:00
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
} else {
|
|
|
|
if (self::checkFunctionExists(
|
|
|
|
$statements_checker,
|
|
|
|
$function_id,
|
|
|
|
$code_location
|
2017-08-12 00:48:58 +02:00
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
2017-12-15 22:48:06 +01:00
|
|
|
if ($type_match_found
|
|
|
|
&& !$param_type->isMixed()
|
|
|
|
&& !$param_type->from_docblock
|
|
|
|
&& !$by_ref
|
|
|
|
) {
|
2017-12-11 03:14:30 +01:00
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$input_expr,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
if ($input_type->isNullable() && !$param_type->isNullable()) {
|
|
|
|
unset($input_type->types['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;
|
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
|
|
|
/**
|
|
|
|
* @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_) {
|
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 [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
&& 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->types 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
|
2016-12-04 01:11:30 +01:00
|
|
|
* @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,
|
2017-01-15 18:34:23 +01:00
|
|
|
&$function_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
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);
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
if (!FunctionChecker::functionExists($statements_checker, $function_id)) {
|
2017-01-15 18:34:23 +01:00
|
|
|
$root_function_id = preg_replace('/.*\\\/', '', $function_id);
|
|
|
|
|
|
|
|
if ($function_id !== $root_function_id &&
|
2017-08-15 01:30:11 +02:00
|
|
|
FunctionChecker::functionExists($statements_checker, $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',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|