2018-01-29 00:29:38 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression\Call;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\FunctionLikeChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
2018-02-10 01:37:09 +01:00
|
|
|
use Psalm\Checker\TypeChecker;
|
2018-02-04 00:52:35 +01:00
|
|
|
use Psalm\Codebase\CallMap;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2018-02-12 02:56:34 +01:00
|
|
|
use Psalm\FileManipulation\FileManipulationBuffer;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\InvalidMethodCall;
|
2018-02-10 01:37:09 +01:00
|
|
|
use Psalm\Issue\InvalidPropertyAssignmentValue;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\InvalidScope;
|
|
|
|
use Psalm\Issue\MixedMethodCall;
|
2018-02-22 01:34:21 +01:00
|
|
|
use Psalm\Issue\MixedTypeCoercion;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\NullReference;
|
|
|
|
use Psalm\Issue\PossiblyFalseReference;
|
|
|
|
use Psalm\Issue\PossiblyInvalidMethodCall;
|
|
|
|
use Psalm\Issue\PossiblyNullReference;
|
|
|
|
use Psalm\Issue\PossiblyUndefinedMethod;
|
2018-02-22 01:34:21 +01:00
|
|
|
use Psalm\Issue\TypeCoercion;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\UndefinedMethod;
|
2018-02-10 01:37:09 +01:00
|
|
|
use Psalm\Issue\UndefinedThisPropertyAssignment;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyFetch;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
|
|
|
|
class MethodCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyze(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\MethodCall $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-02-17 23:23:57 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->name, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
$method_id = null;
|
|
|
|
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
2018-03-18 06:07:14 +01:00
|
|
|
if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_checker->getFQCLN()) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidScope(
|
|
|
|
'Use of $this in non-class context',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->var,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
$class_type = $var_id && $context->hasVariable($var_id, $statements_checker)
|
|
|
|
? $context->vars_in_scope[$var_id]
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
$class_type = $stmt->var->inferredType;
|
|
|
|
} elseif (!$class_type) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
$source = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if (!$context->check_methods || !$context->check_classes) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_mock = false;
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($class_type && $stmt->name instanceof PhpParser\Node\Identifier && $class_type->isNull()) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
2018-04-17 18:16:25 +02:00
|
|
|
'Cannot call method ' . $stmt->name->name . ' on null variable ' . $var_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_type
|
2018-04-17 18:16:25 +02:00
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
2018-01-29 00:29:38 +01:00
|
|
|
&& $class_type->isNullable()
|
|
|
|
&& !$class_type->ignore_nullable_issues
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullReference(
|
2018-04-17 18:16:25 +02:00
|
|
|
'Cannot call method ' . $stmt->name->name . ' on possibly null variable ' . $var_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_type
|
2018-04-17 18:16:25 +02:00
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
2018-01-29 00:29:38 +01:00
|
|
|
&& $class_type->isFalsable()
|
|
|
|
&& !$class_type->ignore_falsable_issues
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyFalseReference(
|
2018-04-17 18:16:25 +02:00
|
|
|
'Cannot call method ' . $stmt->name->name . ' on possibly false variable ' . $var_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = Config::getInstance();
|
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
$non_existent_method_ids = [];
|
|
|
|
$existent_method_ids = [];
|
|
|
|
|
|
|
|
$invalid_method_call_types = [];
|
|
|
|
$has_valid_method_call_type = false;
|
|
|
|
|
|
|
|
$code_location = new CodeLocation($source, $stmt);
|
|
|
|
|
|
|
|
$returns_by_ref = false;
|
|
|
|
|
2018-02-17 23:16:22 +01:00
|
|
|
if ($class_type) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$return_type = null;
|
|
|
|
|
|
|
|
foreach ($class_type->getTypes() as $class_type_part) {
|
|
|
|
if (!$class_type_part instanceof TNamedObject) {
|
|
|
|
switch (get_class($class_type_part)) {
|
|
|
|
case 'Psalm\\Type\\Atomic\\TNull':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TFalse':
|
|
|
|
// handled above
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Psalm\\Type\\Atomic\\TInt':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TBool':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TTrue':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TArray':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TString':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TNumericString':
|
2018-03-05 22:06:06 +01:00
|
|
|
case 'Psalm\\Type\\Atomic\\TClassString':
|
2018-01-29 00:29:38 +01:00
|
|
|
$invalid_method_call_types[] = (string)$class_type_part;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Psalm\\Type\\Atomic\\TMixed':
|
|
|
|
case 'Psalm\\Type\\Atomic\\TObject':
|
2018-02-04 00:52:35 +01:00
|
|
|
$codebase->analyzer->incrementMixedCount($statements_checker->getCheckedFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedMethodCall(
|
2018-02-17 23:16:22 +01:00
|
|
|
'Cannot call method on a mixed variable ' . $var_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-02-09 05:22:29 +01:00
|
|
|
|
|
|
|
$return_type = Type::getMixed();
|
2018-01-29 00:29:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_checker->getCheckedFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
|
|
|
$fq_class_name = $class_type_part->value;
|
|
|
|
|
|
|
|
$intersection_types = $class_type_part->getIntersectionTypes();
|
|
|
|
|
|
|
|
$is_mock = ExpressionChecker::isMock($fq_class_name);
|
|
|
|
|
|
|
|
$has_mock = $has_mock || $is_mock;
|
|
|
|
|
|
|
|
if ($fq_class_name === 'static') {
|
|
|
|
$fq_class_name = (string) $context->self;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_mock ||
|
|
|
|
$context->isPhantomClass($fq_class_name)
|
|
|
|
) {
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id === '$this') {
|
|
|
|
$does_class_exist = true;
|
|
|
|
} else {
|
|
|
|
$does_class_exist = ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
|
|
|
$statements_checker,
|
|
|
|
$fq_class_name,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$does_class_exist) {
|
|
|
|
return $does_class_exist;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_class_name === 'iterable') {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMethod(
|
|
|
|
$fq_class_name . ' has no defined methods',
|
2018-03-21 03:36:03 +01:00
|
|
|
$code_location,
|
2018-04-17 18:16:25 +02:00
|
|
|
$fq_class_name . '::'
|
|
|
|
. (!$stmt->name instanceof PhpParser\Node\Identifier ? '$method' : $stmt->name->name)
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-02-17 23:16:22 +01:00
|
|
|
$return_type = Type::getMixed();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$method_name_lc = strtolower($stmt->name->name);
|
2018-02-17 23:16:22 +01:00
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
$method_id = $fq_class_name . '::' . $method_name_lc;
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
if ($codebase->methodExists($fq_class_name . '::__call')) {
|
|
|
|
if (!$codebase->methodExists($method_id)
|
2018-01-29 00:29:38 +01:00
|
|
|
|| !MethodChecker::isMethodVisible(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource()
|
|
|
|
)
|
|
|
|
) {
|
2018-03-04 19:23:40 +01:00
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
$existent_method_ids[] = $method_id;
|
2018-01-29 00:29:38 +01:00
|
|
|
$return_type = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 21:55:31 +01:00
|
|
|
$source_source = $statements_checker->getSource();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Psalm\Checker\ClassLikeChecker|null
|
|
|
|
*/
|
|
|
|
$classlike_source = $source_source->getSource();
|
|
|
|
$classlike_source_fqcln = $classlike_source ? $classlike_source->getFQCLN() : null;
|
|
|
|
|
|
|
|
if ($var_id === '$this' && $context->self && $classlike_source_fqcln) {
|
|
|
|
if ($fq_class_name !== $context->self
|
|
|
|
&& $codebase->methodExists($context->self . '::' . $method_name_lc)
|
|
|
|
) {
|
|
|
|
$method_id = $context->self . '::' . $method_name_lc;
|
|
|
|
$fq_class_name = $context->self;
|
|
|
|
} elseif ($classlike_source instanceof \Psalm\Checker\TraitChecker
|
|
|
|
&& $codebase->methodExists($classlike_source_fqcln . '::' . $method_name_lc)
|
|
|
|
) {
|
2018-03-21 22:39:01 +01:00
|
|
|
$declaring_method_id = (string) $codebase->methods->getDeclaringMethodId(
|
|
|
|
$classlike_source_fqcln . '::' . $method_name_lc
|
|
|
|
);
|
|
|
|
|
|
|
|
list($declaring_class) = explode('::', $declaring_method_id);
|
|
|
|
|
|
|
|
if ($declaring_class === $classlike_source_fqcln) {
|
|
|
|
$method_id = $classlike_source_fqcln . '::' . $method_name_lc;
|
|
|
|
$fq_class_name = $classlike_source_fqcln;
|
|
|
|
}
|
2018-03-21 21:55:31 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
if ($intersection_types && !$codebase->methodExists($method_id)) {
|
2018-01-29 00:29:38 +01:00
|
|
|
foreach ($intersection_types as $intersection_type) {
|
|
|
|
$method_id = $intersection_type->value . '::' . $method_name_lc;
|
|
|
|
$fq_class_name = $intersection_type->value;
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
if ($codebase->methodExists($method_id)) {
|
2018-01-29 00:29:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$cased_method_id = $fq_class_name . '::' . $stmt->name->name;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
if (!$codebase->methodExists($method_id, $code_location)) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$non_existent_method_ids[] = $method_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$existent_method_ids[] = $method_id;
|
|
|
|
|
|
|
|
$class_template_params = null;
|
|
|
|
|
2018-02-07 19:57:45 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& ($context->collect_initializations || $context->collect_mutations)
|
|
|
|
&& $stmt->var->name === 'this'
|
|
|
|
&& $source instanceof FunctionLikeChecker
|
2018-01-29 00:29:38 +01:00
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
self::collectSpecialInformation($source, $stmt->name->name, $context);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
if ($class_storage->template_types) {
|
|
|
|
$class_template_params = [];
|
|
|
|
|
|
|
|
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 => $_) {
|
|
|
|
$class_template_params[$type_name] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$class_template_params,
|
|
|
|
$context,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
switch (strtolower($stmt->name->name)) {
|
2018-01-29 00:29:38 +01:00
|
|
|
case '__tostring':
|
|
|
|
$return_type = Type::getString();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($method_name_lc === '__tostring') {
|
|
|
|
$return_type_candidate = Type::getString();
|
2018-02-04 00:52:35 +01:00
|
|
|
} elseif (CallMap::inCallMap($cased_method_id)) {
|
2018-02-01 05:27:25 +01:00
|
|
|
if ($class_template_params
|
|
|
|
&& isset($class_storage->methods[$method_name_lc])
|
|
|
|
&& ($method_storage = $class_storage->methods[$method_name_lc])
|
|
|
|
&& $method_storage->return_type
|
|
|
|
) {
|
|
|
|
$return_type_candidate = clone $method_storage->return_type;
|
|
|
|
|
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
|
|
|
$class_template_params
|
|
|
|
);
|
|
|
|
} else {
|
2018-02-04 00:52:35 +01:00
|
|
|
$return_type_candidate = CallMap::getReturnTypeFromCallMap($method_id);
|
2018-02-01 05:27:25 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
$return_type_candidate = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$return_type_candidate,
|
|
|
|
$fq_class_name,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (MethodChecker::checkMethodVisibility(
|
|
|
|
$method_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MethodChecker::checkMethodNotDeprecated(
|
|
|
|
$project_checker,
|
|
|
|
$method_id,
|
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:37:09 +01:00
|
|
|
if (!self::checkMagicGetterOrSetterProperty(
|
|
|
|
$statements_checker,
|
|
|
|
$project_checker,
|
|
|
|
$stmt,
|
|
|
|
$fq_class_name
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
$self_fq_class_name = $fq_class_name;
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$return_type_candidate = $codebase->methods->getMethodReturnType(
|
2018-01-29 00:29:38 +01:00
|
|
|
$method_id,
|
|
|
|
$self_fq_class_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($return_type_candidate) {
|
|
|
|
$return_type_candidate = clone $return_type_candidate;
|
|
|
|
|
|
|
|
if ($class_template_params) {
|
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
|
|
|
$class_template_params
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_candidate = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$return_type_candidate,
|
|
|
|
$self_fq_class_name,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$return_type_location = $codebase->methods->getMethodReturnTypeLocation(
|
2018-01-29 00:29:38 +01:00
|
|
|
$method_id,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
|
|
|
|
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$returns_by_ref =
|
|
|
|
$returns_by_ref
|
2018-02-04 00:52:35 +01:00
|
|
|
|| $codebase->methods->getMethodReturnsByRef($method_id);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (strpos($stmt->name->name, 'assert') === 0) {
|
2018-02-23 21:39:33 +01:00
|
|
|
$assertions = $codebase->methods->getMethodAssertions($method_id);
|
|
|
|
|
|
|
|
if ($assertions) {
|
|
|
|
self::applyAssertionsToContext(
|
|
|
|
$assertions,
|
|
|
|
$stmt->args,
|
|
|
|
$context,
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if ($config->after_method_checks) {
|
|
|
|
$file_manipulations = [];
|
|
|
|
|
2018-02-12 03:07:19 +01:00
|
|
|
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
foreach ($config->after_method_checks as $plugin_fq_class_name) {
|
|
|
|
$plugin_fq_class_name::afterMethodCallCheck(
|
2018-02-12 02:56:34 +01:00
|
|
|
$statements_checker,
|
|
|
|
$method_id,
|
2018-02-12 03:07:19 +01:00
|
|
|
$appearing_method_id,
|
|
|
|
$declaring_method_id,
|
2018-02-12 02:56:34 +01:00
|
|
|
$stmt->args,
|
|
|
|
$code_location,
|
2018-02-12 03:07:19 +01:00
|
|
|
$file_manipulations,
|
|
|
|
$return_type_candidate
|
2018-02-12 02:56:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
|
|
|
FileManipulationBuffer::add($statements_checker->getFilePath(), $file_manipulations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_method_call_types) {
|
|
|
|
$invalid_class_type = $invalid_method_call_types[0];
|
|
|
|
|
|
|
|
if ($has_valid_method_call_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidMethodCall(
|
|
|
|
'Cannot call method on possible ' . $invalid_class_type . ' variable ' . $var_id,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidMethodCall(
|
|
|
|
'Cannot call method on ' . $invalid_class_type . ' variable ' . $var_id,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($non_existent_method_ids) {
|
|
|
|
if ($existent_method_ids) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedMethod(
|
|
|
|
'Method ' . $non_existent_method_ids[0] . ' does not exist',
|
2018-03-21 03:36:03 +01:00
|
|
|
$code_location,
|
|
|
|
$non_existent_method_ids[0]
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMethod(
|
|
|
|
'Method ' . $non_existent_method_ids[0] . ' does not exist',
|
2018-03-21 03:36:03 +01:00
|
|
|
$code_location,
|
|
|
|
$non_existent_method_ids[0]
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = $return_type;
|
|
|
|
|
|
|
|
if ($returns_by_ref) {
|
|
|
|
if (!$stmt->inferredType) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType->by_ref = $returns_by_ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($method_id === null) {
|
|
|
|
return self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$found_generic_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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->getTypes() as $key => $type) {
|
|
|
|
if (!$type instanceof TNamedObject) {
|
|
|
|
$keys_to_remove[] = $key;
|
|
|
|
} else {
|
|
|
|
$type->from_docblock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($keys_to_remove as $key) {
|
|
|
|
$class_type->removeType($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_type->from_docblock = false;
|
|
|
|
|
|
|
|
$context->removeVarFromConflictingClauses($var_id, null, $statements_checker);
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $class_type;
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:37:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check properties accessed with magic getters and setters.
|
|
|
|
* If `@psalm-seal-properties` is set, they must be defined.
|
|
|
|
* If an `@property` annotation is specified, the setter must set something with the correct
|
|
|
|
* type.
|
|
|
|
*
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param \Psalm\Checker\ProjectChecker $project_checker
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
* @param string $fq_class_name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function checkMagicGetterOrSetterProperty(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
\Psalm\Checker\ProjectChecker $project_checker,
|
|
|
|
PhpParser\Node\Expr\MethodCall $stmt,
|
|
|
|
$fq_class_name
|
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-02-10 01:37:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$method_name = strtolower($stmt->name->name);
|
2018-02-10 01:37:09 +01:00
|
|
|
if (!in_array($method_name, ['__get', '__set'], true)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$first_arg_value = $stmt->args[0]->value;
|
|
|
|
if (!$first_arg_value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$prop_name = $first_arg_value->value;
|
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
switch ($method_name) {
|
|
|
|
case '__set':
|
|
|
|
// If `@psalm-seal-properties` is set, the property must be defined with
|
|
|
|
// a `@property` annotation
|
|
|
|
if ($class_storage->sealed_properties
|
|
|
|
&& !isset($class_storage->pseudo_property_set_types['$' . $prop_name])
|
|
|
|
&& IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyAssignment(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a `@property` annotation is set, the type of the value passed to the
|
|
|
|
// magic setter must match the annotation.
|
2018-02-11 05:30:40 +01:00
|
|
|
$second_arg_type = isset($stmt->args[1]->value->inferredType)
|
|
|
|
? $stmt->args[1]->value->inferredType
|
|
|
|
: null;
|
|
|
|
|
2018-02-22 01:34:21 +01:00
|
|
|
if (isset($class_storage->pseudo_property_set_types['$' . $prop_name]) && $second_arg_type) {
|
|
|
|
$pseudo_set_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$class_storage->pseudo_property_set_types['$' . $prop_name],
|
|
|
|
$fq_class_name,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$type_match_found = TypeChecker::isContainedBy(
|
2018-02-10 01:37:09 +01:00
|
|
|
$project_checker->codebase,
|
|
|
|
$second_arg_type,
|
2018-02-22 01:34:21 +01:00
|
|
|
$pseudo_set_type,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
$has_scalar_match,
|
|
|
|
$type_coerced,
|
|
|
|
$type_coerced_from_mixed,
|
|
|
|
$to_string_cast
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($type_coerced) {
|
|
|
|
if ($type_coerced_from_mixed) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedTypeCoercion(
|
|
|
|
$prop_name . ' expects \'' . $pseudo_set_type . '\', '
|
|
|
|
. ' parent type `' . $second_arg_type . '` provided',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TypeCoercion(
|
|
|
|
$prop_name . ' expects \'' . $pseudo_set_type . '\', '
|
|
|
|
. ' parent type `' . $second_arg_type . '` provided',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type_match_found && !$type_coerced_from_mixed) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignmentValue(
|
|
|
|
$prop_name . ' with declared type \''
|
|
|
|
. $pseudo_set_type
|
|
|
|
. '\' cannot be assigned type \'' . $second_arg_type . '\'',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:37:09 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '__get':
|
|
|
|
// If `@psalm-seal-properties` is set, the property must be defined with
|
|
|
|
// a `@property` annotation
|
|
|
|
if ($class_storage->sealed_properties
|
|
|
|
&& !isset($class_storage->pseudo_property_get_types['$' . $prop_name])
|
|
|
|
&& IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyFetch(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|