2016-11-01 19:14:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\ClassChecker;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\FunctionChecker;
|
|
|
|
use Psalm\Checker\FunctionLikeChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\TraitChecker;
|
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;
|
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;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\InvalidScalarArgument;
|
|
|
|
use Psalm\Issue\InvalidScope;
|
|
|
|
use Psalm\Issue\MixedArgument;
|
|
|
|
use Psalm\Issue\MixedMethodCall;
|
2016-12-14 18:54:34 +01:00
|
|
|
use Psalm\Issue\NullArgument;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\NullReference;
|
|
|
|
use Psalm\Issue\ParentNotFound;
|
|
|
|
use Psalm\Issue\TooFewArguments;
|
|
|
|
use Psalm\Issue\TooManyArguments;
|
|
|
|
use Psalm\Issue\TypeCoercion;
|
|
|
|
use Psalm\Issue\UndefinedFunction;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\Generic;
|
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
use Psalm\Type\Atomic\Scalar;
|
|
|
|
use Psalm\Type\Atomic\TNumeric;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
|
|
|
use Psalm\Type\Atomic\TVoid;
|
|
|
|
use Psalm\Type\Atomic\TFloat;
|
|
|
|
use Psalm\Type\Atomic\TString;
|
|
|
|
use Psalm\Type\Atomic\TBool;
|
|
|
|
use Psalm\Type\Atomic\TFalse;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
|
|
|
use Psalm\Type\Atomic\TEmpty;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TObject;
|
|
|
|
use Psalm\Type\Atomic\TResource;
|
|
|
|
use Psalm\Type\Atomic\TCallable;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
|
|
|
use Psalm\Type\Atomic\TNumericString;
|
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
|
|
|
|
* @return false|null
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeFunctionCall(
|
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;
|
|
|
|
|
|
|
|
if ($context->check_functions) {
|
2016-12-31 16:51:42 +01:00
|
|
|
$in_call_map = false;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
$function_params = null;
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
$code_location = new CodeLocation($statements_checker->getSource(), $stmt);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2017-01-15 22:43:49 +01:00
|
|
|
$defined_constants = [];
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Expr) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->name, $context) === false) {
|
2016-12-31 16:51:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if (isset($stmt->name->inferredType)) {
|
|
|
|
foreach ($stmt->name->inferredType->types as $var_type_part) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($var_type_part instanceof Type\Atomic\Fn) {
|
2017-01-06 07:07:11 +01:00
|
|
|
$function_params = $var_type_part->params;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
|
|
if ($var_type_part->return_type) {
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes(
|
|
|
|
$stmt->inferredType,
|
|
|
|
$var_type_part->return_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = $var_type_part->return_type;
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
} elseif (!$var_type_part instanceof TMixed &&
|
|
|
|
(!$var_type_part instanceof TNamedObject || $var_type_part->value !== 'Closure') &&
|
2017-01-16 06:29:18 +01:00
|
|
|
!$var_type_part instanceof TCallable &&
|
|
|
|
(!$var_type_part instanceof TNamedObject ||
|
|
|
|
!MethodChecker::methodExists($var_type_part->value . '::__invoke')
|
|
|
|
)
|
2017-01-02 01:24:15 +01:00
|
|
|
) {
|
2016-12-31 16:51:42 +01:00
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->name,
|
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-12-31 16:51:42 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +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-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$method_id = implode('\\', $stmt->name->parts);
|
|
|
|
|
2017-01-12 15:42:24 +01:00
|
|
|
$in_call_map = FunctionChecker::inCallMap($method_id);
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-01-16 02:11:02 +01:00
|
|
|
$is_predefined = true;
|
|
|
|
|
|
|
|
if (!$in_call_map) {
|
|
|
|
$predefined_functions = Config::getInstance()->getPredefinedFunctions();
|
|
|
|
$is_predefined = isset($predefined_functions[$method_id]);
|
|
|
|
}
|
|
|
|
|
2017-01-12 15:42:24 +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
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->self) {
|
|
|
|
//$method_id = $statements_checker->getFQCLN() . '::' . $method_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$in_call_map &&
|
|
|
|
self::checkFunctionExists($statements_checker, $method_id, $context, $code_location) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_params = FunctionLikeChecker::getFunctionParamsById(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$statements_checker->getFileChecker()
|
2016-12-31 16:51:42 +01:00
|
|
|
);
|
2017-01-15 22:43:49 +01:00
|
|
|
|
2017-01-16 02:11:02 +01:00
|
|
|
if (!$in_call_map && !$is_predefined) {
|
2017-01-15 22:43:49 +01:00
|
|
|
$defined_constants = FunctionChecker::getDefinedConstants(
|
|
|
|
$method_id,
|
|
|
|
$statements_checker->getFilePath()
|
|
|
|
);
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (self::checkFunctionArguments(
|
2016-12-07 20:13:39 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
2016-12-31 16:51:42 +01:00
|
|
|
$function_params,
|
2016-12-07 20:13:39 +01:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $method_id) {
|
|
|
|
$function_params = FunctionLikeChecker::getFunctionParamsById(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$statements_checker->getFileChecker()
|
2016-12-31 16:51:42 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if (self::checkFunctionArgumentsMatch(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$method_id,
|
2016-12-31 16:51:42 +01:00
|
|
|
$function_params,
|
2016-11-02 07:29:00 +01:00
|
|
|
$context,
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-12-05 00:42:20 +01:00
|
|
|
// fall through
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($method_id) {
|
|
|
|
if ($in_call_map) {
|
|
|
|
$stmt->inferredType = FunctionChecker::getReturnTypeFromCallMapWithArgs(
|
2016-11-02 07:29:00 +01:00
|
|
|
$method_id,
|
2016-12-31 16:51:42 +01:00
|
|
|
$stmt->args,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-12-31 16:51:42 +01:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$stmt->inferredType = FunctionChecker::getFunctionReturnType(
|
|
|
|
$method_id,
|
|
|
|
$statements_checker->getCheckedFilePath()
|
|
|
|
);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// this can happen when the function was defined in the Config startup script
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-15 01:06:58 +01:00
|
|
|
$stmt->inferredType = new Type\Union([new Type\Atomic\T('$' . $var->name)]);
|
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
|
|
|
|
* @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-01-09 06:27:04 +01:00
|
|
|
$file_checker = $statements_checker->getFileChecker();
|
|
|
|
|
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) {
|
|
|
|
if (!in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
2016-12-17 01:22:30 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
|
|
|
$stmt->class,
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
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(
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2017-01-09 06:27:04 +01:00
|
|
|
$file_checker,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
2017-01-09 05:58:06 +01:00
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
true
|
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);
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = $stmt->class->name;
|
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-01-20 06:10:10 +01:00
|
|
|
ClassChecker::classExists($fq_class_name, $file_checker)
|
2017-01-09 05:58:06 +01:00
|
|
|
) {
|
2017-01-20 06:10:10 +01:00
|
|
|
$storage = ClassLikeChecker::$storage[strtolower($fq_class_name)];
|
|
|
|
|
|
|
|
// 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-01-20 06:10:10 +01:00
|
|
|
if (MethodChecker::methodExists($fq_class_name . '::__construct')) {
|
|
|
|
$method_id = $fq_class_name . '::__construct';
|
2017-01-02 02:10:28 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
$method_params = FunctionLikeChecker::getMethodParamsById($method_id, $stmt->args, $file_checker);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$method_params,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
// check again after we've processed args
|
|
|
|
$method_params = FunctionLikeChecker::getMethodParamsById($method_id, $stmt->args, $file_checker);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-20 06:10:10 +01:00
|
|
|
if (self::checkFunctionArgumentsMatch(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$method_id,
|
|
|
|
$method_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
|
|
|
}
|
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()) {
|
|
|
|
/** @var Type\Union|null */
|
|
|
|
$key_type = null;
|
|
|
|
|
|
|
|
/** @var Type\Union|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,
|
|
|
|
$value_type
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
2016-12-24 19:23:22 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @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
|
|
|
|
|
|
|
$class_type = isset($context->vars_in_scope[$var_id]) ? $context->vars_in_scope[$var_id] : null;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ($class_type && is_string($stmt->name)) {
|
|
|
|
/** @var Type\Union|null */
|
|
|
|
$return_type = null;
|
|
|
|
|
|
|
|
foreach ($class_type->types as $type) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$type instanceof TNamedObject) {
|
|
|
|
switch (get_class($type)) {
|
|
|
|
case 'Psalm\\Type\\Atomic\\TNull':
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
|
|
|
'Cannot call method ' . $stmt->name . ' on possibly null variable ' . $var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Psalm\\Type\\Atomic\\TInt':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TBool':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TFalse':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TArray':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TString':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TNumericString':
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArgument(
|
|
|
|
'Cannot call method ' . $stmt->name . ' on ' . $class_type . ' variable ' . $var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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-01-15 01:06:58 +01:00
|
|
|
$fq_class_name = $type->value;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
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-01-15 01:06:58 +01:00
|
|
|
$does_class_exist = ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
|
|
|
$fq_class_name,
|
|
|
|
$statements_checker->getFileChecker(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
true
|
|
|
|
);
|
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-01-15 01:06:58 +01:00
|
|
|
if (MethodChecker::methodExists($fq_class_name . '::__call')) {
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-15 01:06:58 +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
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$does_method_exist = MethodChecker::checkMethodExists(
|
|
|
|
$cased_method_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$does_method_exist) {
|
|
|
|
return $does_method_exist;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (FunctionChecker::inCallMap($cased_method_id)) {
|
|
|
|
$return_type_candidate = FunctionChecker::getReturnTypeFromCallMap($method_id);
|
|
|
|
} else {
|
|
|
|
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-01-15 01:06:58 +01:00
|
|
|
if (MethodChecker::checkMethodNotDeprecated(
|
|
|
|
$method_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$return_type_candidate = MethodChecker::getMethodReturnType($method_id);
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $stmt->var->name === 'this'
|
|
|
|
&& is_string($stmt->name)
|
|
|
|
&& $source instanceof FunctionLikeChecker
|
|
|
|
) {
|
|
|
|
$this_method_id = $source->getMethodId();
|
|
|
|
|
|
|
|
$fq_class_name = (string)$statements_checker->getFQCLN();
|
|
|
|
|
|
|
|
if ($context->collect_mutations &&
|
|
|
|
$context->self &&
|
|
|
|
(
|
|
|
|
$context->self === $fq_class_name ||
|
|
|
|
ClassChecker::classExtends(
|
|
|
|
$context->self,
|
|
|
|
$fq_class_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$file_checker = $source->getFileChecker();
|
|
|
|
|
|
|
|
$method_id = $statements_checker->getFQCLN() . '::' . strtolower($stmt->name);
|
|
|
|
|
|
|
|
if ($file_checker->project_checker->getMethodMutations($method_id, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($context->collect_initializations &&
|
|
|
|
$context->self &&
|
|
|
|
(
|
|
|
|
$context->self === $fq_class_name ||
|
|
|
|
ClassChecker::classExtends(
|
|
|
|
$context->self,
|
|
|
|
$fq_class_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$method_id = $statements_checker->getFQCLN() . '::' . strtolower($stmt->name);
|
|
|
|
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
$method_storage = MethodChecker::getStorage((string)$declaring_method_id);
|
|
|
|
|
|
|
|
$class_checker = $source->getSource();
|
|
|
|
|
|
|
|
if ($class_checker instanceof ClassLikeChecker &&
|
|
|
|
$method_storage->visibility === ClassLikeChecker::VISIBILITY_PRIVATE
|
|
|
|
) {
|
|
|
|
if ($class_checker->getMethodMutations(strtolower($stmt->name), $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($return_type_candidate) {
|
|
|
|
$return_type_candidate = ExpressionChecker::fleshOutTypes(
|
|
|
|
$return_type_candidate,
|
|
|
|
$stmt->args,
|
|
|
|
$fq_class_name,
|
|
|
|
$method_id
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = $return_type;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$method_params = $method_id
|
|
|
|
? FunctionLikeChecker::getMethodParamsById($method_id, $stmt->args, $statements_checker->getFileChecker())
|
|
|
|
: null;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (self::checkFunctionArguments(
|
2016-12-07 20:13:39 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
2016-12-31 16:51:42 +01:00
|
|
|
$method_params,
|
2016-12-07 20:13:39 +01:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-02 02:10:28 +01:00
|
|
|
// check again after we've processed args
|
2017-01-02 21:31:18 +01:00
|
|
|
$method_params = $method_id
|
|
|
|
? FunctionLikeChecker::getMethodParamsById($method_id, $stmt->args, $statements_checker->getFileChecker())
|
|
|
|
: null;
|
2017-01-02 02:10:28 +01:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if (self::checkFunctionArgumentsMatch(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$method_id,
|
2016-12-31 16:51:42 +01:00
|
|
|
$method_params,
|
2016-11-02 07:29:00 +01:00
|
|
|
$context,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$has_mock
|
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
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\StaticCall $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @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
|
|
|
|
) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr\Variable ||
|
|
|
|
$stmt->class instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// this is when calling $some_class::staticMethod() - which is a shitty way of doing things
|
|
|
|
// because it can't be statically type-checked
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
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();
|
|
|
|
|
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
|
|
|
|
|
|
|
if (count($stmt->class->parts) === 1 && in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
|
|
|
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
|
|
|
|
|
|
|
if (is_string($stmt->name)) {
|
|
|
|
if ($context->collect_mutations) {
|
|
|
|
$method_id = $fq_class_name . '::' . strtolower($stmt->name);
|
|
|
|
|
2017-01-12 06:54:41 +01:00
|
|
|
if ($file_checker->project_checker->getMethodMutations($method_id, $context) === false) {
|
2017-01-12 03:37:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
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-08 01:16:51 +01:00
|
|
|
$does_class_exist = ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker,
|
2016-12-08 23:15:51 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
2017-01-09 05:58:06 +01:00
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
true
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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) &&
|
2016-11-07 23:29:51 +01:00
|
|
|
!MethodChecker::methodExists($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(
|
|
|
|
$cased_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()
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if (!$does_method_exist) {
|
|
|
|
return $does_method_exist;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (MethodChecker::checkMethodVisibility(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
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-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name
|
|
|
|
&& $stmt->class->parts[0] !== 'parent'
|
2016-12-12 05:40:46 +01:00
|
|
|
&& (!$context->self
|
|
|
|
|| $statements_checker->isStatic()
|
|
|
|
|| !ClassChecker::classExtends($context->self, $fq_class_name)
|
|
|
|
)
|
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,
|
2016-12-31 15:20:10 +01:00
|
|
|
$stmt->class instanceof PhpParser\Node\Name && $stmt->class->parts[0] === 'self',
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (MethodChecker::checkMethodNotDeprecated(
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2016-12-06 22:33:47 +01:00
|
|
|
$return_types = MethodChecker::getMethodReturnType($method_id);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if ($return_types) {
|
|
|
|
$return_types = ExpressionChecker::fleshOutTypes(
|
|
|
|
$return_types,
|
|
|
|
$stmt->args,
|
|
|
|
$stmt->class instanceof PhpParser\Node\Name && $stmt->class->parts === ['parent']
|
2016-11-08 01:16:51 +01:00
|
|
|
? $statements_checker->getFQCLN()
|
2016-11-07 23:29:51 +01:00
|
|
|
: $fq_class_name,
|
2016-11-01 19:14:35 +01:00
|
|
|
$method_id
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, $return_types);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = $return_types;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$method_params = $method_id
|
|
|
|
? FunctionLikeChecker::getMethodParamsById($method_id, $stmt->args, $file_checker)
|
|
|
|
: null;
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (self::checkFunctionArguments(
|
2016-12-07 20:13:39 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
2016-12-31 16:51:42 +01:00
|
|
|
$method_params,
|
2016-12-07 20:13:39 +01:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-02 02:10:28 +01:00
|
|
|
// get them again
|
2017-01-02 21:31:18 +01:00
|
|
|
$method_params = $method_id
|
|
|
|
? FunctionLikeChecker::getMethodParamsById($method_id, $stmt->args, $file_checker)
|
|
|
|
: null;
|
2017-01-02 02:10:28 +01:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if (self::checkFunctionArgumentsMatch(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$method_id,
|
2016-12-31 16:51:42 +01:00
|
|
|
$method_params,
|
2016-11-02 07:29:00 +01:00
|
|
|
$context,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$has_mock
|
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-12-31 16:51:42 +01:00
|
|
|
* @param Context $context
|
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,
|
2016-12-31 16:51:42 +01:00
|
|
|
array $function_params = null,
|
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;
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($function_params !== null) {
|
2017-01-16 18:39:38 +01:00
|
|
|
$by_ref = $argument_offset < count($function_params)
|
|
|
|
? $function_params[$argument_offset]->by_ref
|
2017-01-16 18:59:09 +01:00
|
|
|
: $last_param && $last_param->is_variadic && $last_param->by_ref;
|
2017-01-16 18:39:38 +01:00
|
|
|
|
|
|
|
$by_ref_type = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
if ($by_ref && $last_param) {
|
2017-01-16 18:39:38 +01:00
|
|
|
$by_ref_type = $argument_offset < count($function_params)
|
|
|
|
? clone $function_params[$argument_offset]->type
|
|
|
|
: clone $last_param->type;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if ($by_ref && $by_ref_type) {
|
|
|
|
ExpressionChecker::assignByRefParam($statements_checker, $arg->value, $by_ref_type, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (FetchChecker::analyzePropertyFetch($statements_checker, $arg->value, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$arg->value,
|
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
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($var_id &&
|
|
|
|
(!isset($context->vars_in_scope[$var_id]) || $context->vars_in_scope[$var_id]->isNull())
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// we don't know if it exists, assume it's passed by reference
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
$statements_checker->registerVariable('$' . $var_id, $arg->value->getLine());
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($arg->value instanceof PhpParser\Node\Expr\Variable) {
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($function_params !== null) {
|
2017-01-16 18:39:38 +01:00
|
|
|
$by_ref = $argument_offset < count($function_params)
|
|
|
|
? $function_params[$argument_offset]->by_ref
|
2017-01-16 18:59:09 +01:00
|
|
|
: $last_param && $last_param->is_variadic && $last_param->by_ref;
|
2017-01-16 18:39:38 +01:00
|
|
|
|
|
|
|
$by_ref_type = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
if ($by_ref && $last_param) {
|
2017-01-16 18:39:38 +01:00
|
|
|
$by_ref_type = $argument_offset < count($function_params)
|
|
|
|
? clone $function_params[$argument_offset]->type
|
|
|
|
: clone $last_param->type;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyzeVariable(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$arg->value,
|
|
|
|
$context,
|
|
|
|
$by_ref,
|
|
|
|
$by_ref_type
|
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif (is_string($arg->value->name)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
if (false ||
|
|
|
|
!isset($context->vars_in_scope['$' . $arg->value->name]) ||
|
|
|
|
$context->vars_in_scope['$' . $arg->value->name]->isNull()
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// we don't know if it exists, assume it's passed by reference
|
|
|
|
$context->vars_in_scope['$' . $arg->value->name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $arg->value->name] = true;
|
|
|
|
$statements_checker->registerVariable('$' . $arg->value->name, $arg->value->getLine());
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $arg->value, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param array<int,FunctionLikeParameter>|null $function_params
|
|
|
|
* @param Context $context
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @param boolean $is_mock
|
2016-12-07 20:13:39 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
protected static function checkFunctionArgumentsMatch(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
array $args,
|
|
|
|
$method_id,
|
2016-12-31 16:51:42 +01:00
|
|
|
array $function_params = null,
|
2016-12-07 20:13:39 +01:00
|
|
|
Context $context,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
$is_mock = false
|
|
|
|
) {
|
|
|
|
$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;
|
|
|
|
|
|
|
|
if ($method_id) {
|
|
|
|
if ($in_call_map || !strpos($method_id, '::')) {
|
2016-12-31 00:08:07 +01:00
|
|
|
$is_variadic = FunctionChecker::isVariadic(strtolower($method_id), $statements_checker->getFilePath());
|
2016-12-07 20:13:39 +01:00
|
|
|
} else {
|
|
|
|
$fq_class_name = explode('::', $method_id)[0];
|
|
|
|
$is_variadic = $is_mock || MethodChecker::isVariadic($method_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($method_id && strpos($method_id, '::') && !$in_call_map) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId($method_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($args as $argument_offset => $arg) {
|
2017-01-13 18:40:01 +01:00
|
|
|
if ($function_params !== null && isset($arg->value->inferredType)) {
|
2017-01-17 06:07:58 +01:00
|
|
|
$function_param = count($function_params) > $argument_offset
|
|
|
|
? $function_params[$argument_offset]
|
|
|
|
: ($last_param && $last_param->is_variadic ? $last_param : null);
|
|
|
|
|
|
|
|
if ($function_param) {
|
|
|
|
$param_type = $function_param->type;
|
|
|
|
|
|
|
|
if ($function_param->is_variadic) {
|
|
|
|
if (!$param_type->hasArray() || !$param_type->types['array'] instanceof TArray) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = clone $param_type->types['array']->type_params[1];
|
|
|
|
}
|
2016-11-01 19:14:35 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::checkFunctionArgumentType(
|
|
|
|
$statements_checker,
|
|
|
|
$arg->value->inferredType,
|
|
|
|
ExpressionChecker::fleshOutTypes(
|
|
|
|
clone $param_type,
|
|
|
|
[],
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2016-11-01 19:14:35 +01:00
|
|
|
$method_id
|
|
|
|
),
|
|
|
|
$cased_method_id,
|
|
|
|
$argument_offset,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $arg->value)
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if ($function_params !== null && ($method_id === 'array_map' || $method_id === 'array_filter')) {
|
|
|
|
if (self::checkArrayFunctionArgumentsMatch(
|
|
|
|
$statements_checker,
|
|
|
|
$args,
|
|
|
|
$method_id,
|
|
|
|
$function_params,
|
|
|
|
$context,
|
|
|
|
$code_location
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_params !== null) {
|
|
|
|
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()
|
|
|
|
)) {
|
2017-01-07 20:35:07 +01:00
|
|
|
// fall through
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has_packed_var && count($args) < count($function_params)) {
|
|
|
|
for ($i = count($args); $i < count($function_params); $i++) {
|
|
|
|
$param = $function_params[$i];
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
* @param string|null $method_id
|
|
|
|
* @param array<int,FunctionLikeParameter> $function_params
|
|
|
|
* @param Context $context
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
protected static function checkArrayFunctionArgumentsMatch(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
array $args,
|
|
|
|
$method_id,
|
|
|
|
array $function_params,
|
|
|
|
Context $context,
|
|
|
|
CodeLocation $code_location
|
|
|
|
) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$array_arg = isset($args[$i]->value) ? $args[$i]->value : null;
|
|
|
|
|
|
|
|
$array_arg_types[] = $array_arg
|
|
|
|
&& isset($array_arg->inferredType)
|
|
|
|
&& isset($array_arg->inferredType->types['array'])
|
2017-01-15 01:06:58 +01:00
|
|
|
&& $array_arg->inferredType->types['array'] instanceof Type\Atomic\TArray
|
2016-12-31 16:51:42 +01:00
|
|
|
? $array_arg->inferredType->types['array']
|
|
|
|
: null;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
/** @var PhpParser\Node\Arg */
|
|
|
|
$closure_arg = isset($args[$closure_index]) ? $args[$closure_index] : null;
|
|
|
|
|
|
|
|
/** @var Type\Union|null */
|
|
|
|
$closure_arg_type = $closure_arg && isset($closure_arg->value->inferredType)
|
|
|
|
? $closure_arg->value->inferredType
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ($closure_arg_type) {
|
|
|
|
$expected_closure_param_count = $method_id === 'array_filter' ? 1 : count($array_arg_types);
|
|
|
|
|
|
|
|
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-01-06 07:07:11 +01:00
|
|
|
if (count($closure_type->params) > $expected_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-01-06 07:07:11 +01:00
|
|
|
} elseif (count($closure_type->params) < $expected_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-01-06 07:07:11 +01:00
|
|
|
$closure_params = $closure_type->params;
|
2016-12-31 16:51:42 +01:00
|
|
|
$closure_return_type = $closure_type->return_type;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
foreach ($closure_params as $param_name => $closure_param) {
|
2016-12-31 16:51:42 +01:00
|
|
|
if (!$array_arg_types[$i]) {
|
2017-01-06 07:07:11 +01: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-01-06 07:07:11 +01: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;
|
|
|
|
|
|
|
|
$type_match_found = TypeChecker::isContainedBy(
|
|
|
|
$input_type,
|
|
|
|
$closure_param_type,
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker->getFileChecker(),
|
2016-12-31 16:51:42 +01:00
|
|
|
false,
|
|
|
|
$scalar_type_match_found,
|
|
|
|
$coerced_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($coerced_type) {
|
2016-12-07 20:13:39 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-12-31 16:51:42 +01:00
|
|
|
new TypeCoercion(
|
|
|
|
'First parameter of closure passed to function ' . $method_id . ' expects ' .
|
|
|
|
$closure_param_type . ', parent type ' . $input_type . ' provided',
|
2016-12-07 20:13:39 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $closure_arg)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
if (!$type_match_found) {
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$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
|
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,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location
|
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-01-13 18:40:01 +01:00
|
|
|
$method_identifier = $cased_method_id ? ' of ' . $cased_method_id : '';
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-10 00:52:36 +01:00
|
|
|
if ($input_type->isNullable() && !$param_type->isNullable() && $cased_method_id !== 'echo') {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-12-14 18:54:34 +01:00
|
|
|
new NullArgument(
|
2017-01-13 18:40:01 +01:00
|
|
|
'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be null, possibly ' .
|
2016-11-02 07:29:00 +01:00
|
|
|
'null value provided',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
$type_match_found = TypeChecker::isContainedBy(
|
2016-11-02 07:29:00 +01:00
|
|
|
$input_type,
|
|
|
|
$param_type,
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker->getFileChecker(),
|
2016-12-11 19:48:11 +01:00
|
|
|
true,
|
2016-11-02 07:29:00 +01:00
|
|
|
$scalar_type_match_found,
|
2016-12-29 06:14:06 +01:00
|
|
|
$coerced_type,
|
|
|
|
$to_string_cast
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if ($coerced_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TypeCoercion(
|
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
|
|
|
', parent type ' . $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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
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
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param string $function_id
|
|
|
|
* @param Context $context
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param CodeLocation $code_location
|
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-11-02 07:29:00 +01:00
|
|
|
Context $context,
|
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);
|
|
|
|
|
2016-12-31 00:08:07 +01:00
|
|
|
if (!FunctionChecker::functionExists($function_id, $statements_checker->getFilePath())) {
|
2017-01-15 18:34:23 +01:00
|
|
|
$root_function_id = preg_replace('/.*\\\/', '', $function_id);
|
|
|
|
|
|
|
|
if ($function_id !== $root_function_id &&
|
|
|
|
FunctionChecker::functionExists($root_function_id, $statements_checker->getFilePath())
|
|
|
|
) {
|
|
|
|
$function_id = $root_function_id;
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedFunction(
|
|
|
|
'Function ' . $cased_function_id . ' does not exist',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|