2018-01-29 00:29:38 +01:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\Call;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\MethodAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2018-11-12 16:46:55 +01:00
|
|
|
|
use Psalm\Internal\Codebase\CallMap;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
|
use Psalm\Config;
|
|
|
|
|
use Psalm\Context;
|
2018-11-12 16:46:55 +01:00
|
|
|
|
use Psalm\Internal\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;
|
2018-05-03 19:56:30 +02:00
|
|
|
|
use Psalm\Issue\PossiblyInvalidPropertyAssignmentValue;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
|
{
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
|
* @param Context $context
|
|
|
|
|
*
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
PhpParser\Node\Expr\MethodCall $stmt,
|
|
|
|
|
Context $context
|
|
|
|
|
) {
|
2018-05-09 15:30:23 +02:00
|
|
|
|
$stmt->inferredType = null;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->var, $context) === false) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context) === false) {
|
2018-02-17 23:23:57 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$method_id = null;
|
|
|
|
|
|
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_analyzer->getFQCLN()) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidScope(
|
|
|
|
|
'Use of $this in non-class context',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_id = ExpressionAnalyzer::getArrayVarId(
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$stmt->var,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$class_type = $var_id && $context->hasVariable($var_id, $statements_analyzer)
|
2018-01-29 00:29:38 +01:00
|
|
|
|
? $context->vars_in_scope[$var_id]
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
|
$class_type = $stmt->var->inferredType;
|
|
|
|
|
} elseif (!$class_type) {
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$source = $statements_analyzer->getSource();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
if (!$context->check_methods || !$context->check_classes) {
|
2018-06-26 01:38:15 +02:00
|
|
|
|
if (self::checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-08-02 23:14:53 +02:00
|
|
|
|
$stmt->args,
|
2018-06-26 01:38:15 +02:00
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
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-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
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-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
2018-11-28 17:45:54 +01:00
|
|
|
|
// fall through
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
2018-11-28 17:45:54 +01:00
|
|
|
|
// fall through
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$config = Config::getInstance();
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
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);
|
2018-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location = new CodeLocation($source, $stmt->name);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
$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;
|
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$lhs_types = $class_type->getTypes();
|
|
|
|
|
|
|
|
|
|
foreach ($lhs_types as $lhs_type_part) {
|
|
|
|
|
if (!$lhs_type_part instanceof TNamedObject) {
|
|
|
|
|
switch (get_class($lhs_type_part)) {
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TNull::class:
|
|
|
|
|
case Type\Atomic\TFalse::class:
|
2018-01-29 00:29:38 +01:00
|
|
|
|
// handled above
|
|
|
|
|
break;
|
|
|
|
|
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TInt::class:
|
2018-05-05 23:30:18 +02:00
|
|
|
|
case Type\Atomic\TLiteralInt::class:
|
|
|
|
|
case Type\Atomic\TFloat::class:
|
|
|
|
|
case Type\Atomic\TLiteralFloat::class:
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TBool::class:
|
|
|
|
|
case Type\Atomic\TTrue::class:
|
|
|
|
|
case Type\Atomic\TArray::class:
|
2018-11-09 16:56:27 +01:00
|
|
|
|
case Type\Atomic\TNonEmptyArray::class:
|
2018-05-05 23:30:18 +02:00
|
|
|
|
case Type\Atomic\ObjectLike::class:
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TString::class:
|
2018-08-21 17:40:29 +02:00
|
|
|
|
case Type\Atomic\TSingleLetter::class:
|
2018-05-05 23:30:18 +02:00
|
|
|
|
case Type\Atomic\TLiteralString::class:
|
2018-05-21 18:40:39 +02:00
|
|
|
|
case Type\Atomic\TLiteralClassString::class:
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TNumericString::class:
|
2018-10-29 16:54:25 +01:00
|
|
|
|
case Type\Atomic\THtmlEscapedString::class:
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TClassString::class:
|
2018-05-08 22:34:08 +02:00
|
|
|
|
case Type\Atomic\TEmptyMixed::class:
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$invalid_method_call_types[] = (string)$lhs_type_part;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
break;
|
|
|
|
|
|
2018-04-20 16:52:23 +02:00
|
|
|
|
case Type\Atomic\TMixed::class:
|
|
|
|
|
case Type\Atomic\TGenericParam::class:
|
|
|
|
|
case Type\Atomic\TObject::class:
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
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-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
2018-02-09 05:22:29 +01:00
|
|
|
|
|
2018-05-09 00:11:10 +02:00
|
|
|
|
if (self::checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-08-02 23:14:53 +02:00
|
|
|
|
$stmt->args,
|
2018-05-09 00:11:10 +02:00
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 05:22:29 +01:00
|
|
|
|
$return_type = Type::getMixed();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$fq_class_name = $lhs_type_part->value;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$intersection_types = $lhs_type_part->getIntersectionTypes();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$is_mock = ExpressionAnalyzer::isMock($fq_class_name);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
$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 {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$does_class_exist = ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$fq_class_name,
|
2018-07-14 00:47:21 +02:00
|
|
|
|
new CodeLocation($source, $stmt->var),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-14 00:47:21 +02:00
|
|
|
|
new CodeLocation($source, $stmt->var),
|
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
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
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-07-16 17:52:38 +02:00
|
|
|
|
$intersection_method_id = $intersection_types
|
|
|
|
|
? '(' . $lhs_type_part . ')' . '::' . $stmt->name->name
|
|
|
|
|
: null;
|
|
|
|
|
|
2018-08-02 23:14:53 +02:00
|
|
|
|
$args = $stmt->args;
|
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
|
if (!$codebase->methods->methodExists($method_id)
|
2018-11-06 03:57:36 +01:00
|
|
|
|
|| !MethodAnalyzer::isMethodVisible(
|
2018-09-26 00:37:24 +02:00
|
|
|
|
$method_id,
|
|
|
|
|
$context->self,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSource()
|
2018-09-26 00:37:24 +02:00
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
if ($codebase->methods->methodExists($fq_class_name . '::__call', $context->calling_method_id)) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-04-22 04:44:54 +02:00
|
|
|
|
|
2018-09-09 17:45:52 +02:00
|
|
|
|
if (isset($class_storage->pseudo_methods[$method_name_lc])) {
|
|
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
$existent_method_ids[] = $method_id;
|
2018-04-22 04:13:10 +02:00
|
|
|
|
|
2018-09-09 17:45:52 +02:00
|
|
|
|
$pseudo_method_storage = $class_storage->pseudo_methods[$method_name_lc];
|
2018-04-22 04:13:10 +02:00
|
|
|
|
|
2018-09-09 17:45:52 +02:00
|
|
|
|
if (self::checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-09-09 17:45:52 +02:00
|
|
|
|
$args,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$method_id,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-04-22 04:13:10 +02:00
|
|
|
|
|
2018-09-09 17:45:52 +02:00
|
|
|
|
$generic_params = [];
|
|
|
|
|
|
|
|
|
|
if (self::checkFunctionLikeArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-09-09 17:45:52 +02:00
|
|
|
|
$args,
|
|
|
|
|
null,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$pseudo_method_storage,
|
|
|
|
|
null,
|
|
|
|
|
$generic_params,
|
|
|
|
|
$code_location,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($pseudo_method_storage->return_type) {
|
|
|
|
|
$return_type_candidate = clone $pseudo_method_storage->return_type;
|
2018-06-27 05:11:16 +02:00
|
|
|
|
|
2018-09-09 17:45:52 +02:00
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
2018-06-27 05:11:16 +02:00
|
|
|
|
}
|
2018-09-09 17:45:52 +02:00
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (self::checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-09-09 17:45:52 +02:00
|
|
|
|
$args,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($class_storage->sealed_methods) {
|
|
|
|
|
$non_existent_method_ids[] = $method_id;
|
|
|
|
|
continue;
|
2018-04-22 04:13:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 06:40:30 +02:00
|
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
$existent_method_ids[] = $method_id;
|
|
|
|
|
|
2018-07-17 04:48:53 +02:00
|
|
|
|
$array_values = array_map(
|
|
|
|
|
/**
|
|
|
|
|
* @return PhpParser\Node\Expr\ArrayItem
|
|
|
|
|
*/
|
|
|
|
|
function (PhpParser\Node\Arg $arg) {
|
|
|
|
|
return new PhpParser\Node\Expr\ArrayItem($arg->value);
|
|
|
|
|
},
|
|
|
|
|
$args
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$args = [
|
|
|
|
|
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\String_($method_name_lc)),
|
|
|
|
|
new PhpParser\Node\Arg(new PhpParser\Node\Expr\Array_($array_values)),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$method_id = $fq_class_name . '::__call';
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$source_source = $statements_analyzer->getSource();
|
2018-03-21 21:55:31 +01:00
|
|
|
|
|
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
|
* @var \Psalm\Internal\Analyzer\ClassLikeAnalyzer|null
|
2018-03-21 21:55:31 +01:00
|
|
|
|
*/
|
|
|
|
|
$classlike_source = $source_source->getSource();
|
|
|
|
|
$classlike_source_fqcln = $classlike_source ? $classlike_source->getFQCLN() : null;
|
|
|
|
|
|
2018-06-09 02:06:05 +02:00
|
|
|
|
if ($var_id === '$this'
|
|
|
|
|
&& $context->self
|
|
|
|
|
&& $classlike_source_fqcln
|
|
|
|
|
&& $fq_class_name !== $context->self
|
|
|
|
|
&& $codebase->methodExists($context->self . '::' . $method_name_lc)
|
|
|
|
|
) {
|
|
|
|
|
$method_id = $context->self . '::' . $method_name_lc;
|
|
|
|
|
$fq_class_name = $context->self;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
|
$check_visibility = true;
|
|
|
|
|
|
|
|
|
|
if ($intersection_types) {
|
|
|
|
|
foreach ($intersection_types as $intersection_type) {
|
|
|
|
|
if ($intersection_type instanceof TNamedObject
|
|
|
|
|
&& $codebase->interfaceExists($intersection_type->value)
|
|
|
|
|
) {
|
|
|
|
|
$interface_storage = $codebase->classlike_storage_provider->get($intersection_type->value);
|
|
|
|
|
|
|
|
|
|
$check_visibility = $check_visibility && !$interface_storage->override_method_visibility;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
|
if ($intersection_types && !$codebase->methodExists($method_id)) {
|
2018-11-25 17:11:33 +01:00
|
|
|
|
if ($codebase->interfaceExists($fq_class_name)) {
|
|
|
|
|
$interface_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
|
|
$check_visibility = $check_visibility && !$interface_storage->override_method_visibility;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
foreach ($intersection_types as $intersection_type) {
|
2018-11-02 18:08:56 +01:00
|
|
|
|
if ($intersection_type instanceof Type\Atomic\TGenericParam) {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get a generic param here');
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 22:07:18 +02:00
|
|
|
|
$method_id = $intersection_type->value . '::' . $method_name_lc;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$fq_class_name = $intersection_type->value;
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$does_class_exist = ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-10-30 15:34:02 +01:00
|
|
|
|
$fq_class_name,
|
|
|
|
|
new CodeLocation($source, $stmt->var),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-10-30 15:34:02 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!$does_class_exist) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 22:07:18 +02:00
|
|
|
|
if ($codebase->methodExists($method_id)) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$source_method_id = $source instanceof FunctionLikeAnalyzer
|
2018-06-19 22:14:51 +02:00
|
|
|
|
? $source->getMethodId()
|
|
|
|
|
: null;
|
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
|
if (!$codebase->methods->methodExists(
|
|
|
|
|
$method_id,
|
|
|
|
|
$context->calling_method_id,
|
|
|
|
|
$method_id !== $source_method_id ? $code_location : null
|
|
|
|
|
)) {
|
2018-11-30 21:13:25 +01:00
|
|
|
|
if ($config->use_phpdoc_method_without_magic_or_parent) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-04-22 04:44:54 +02:00
|
|
|
|
|
|
|
|
|
if (isset($class_storage->pseudo_methods[$method_name_lc])) {
|
|
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
$existent_method_ids[] = $method_id;
|
|
|
|
|
|
|
|
|
|
$pseudo_method_storage = $class_storage->pseudo_methods[$method_name_lc];
|
|
|
|
|
|
|
|
|
|
if (self::checkFunctionArguments(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-07-17 04:48:53 +02:00
|
|
|
|
$args,
|
2018-04-22 04:44:54 +02:00
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$method_id,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$generic_params = [];
|
|
|
|
|
|
|
|
|
|
if (self::checkFunctionLikeArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-07-17 04:48:53 +02:00
|
|
|
|
$args,
|
2018-04-22 04:44:54 +02:00
|
|
|
|
null,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$pseudo_method_storage,
|
|
|
|
|
null,
|
|
|
|
|
$generic_params,
|
|
|
|
|
$code_location,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($pseudo_method_storage->return_type) {
|
|
|
|
|
$return_type_candidate = clone $pseudo_method_storage->return_type;
|
|
|
|
|
|
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 17:52:38 +02:00
|
|
|
|
$non_existent_method_ids[] = $intersection_method_id ?: $method_id;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
|
if ($context->collect_initializations && $context->calling_method_id) {
|
|
|
|
|
list($calling_method_class) = explode('::', $context->calling_method_id);
|
|
|
|
|
$codebase->file_reference_provider->addReferenceToClassMethod(
|
|
|
|
|
$calling_method_class . '::__construct',
|
|
|
|
|
strtolower($method_id)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$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'
|
2018-11-06 03:57:36 +01:00
|
|
|
|
&& $source instanceof FunctionLikeAnalyzer
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
if ($class_storage->template_types) {
|
|
|
|
|
$class_template_params = [];
|
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$reversed_class_template_types = array_reverse(array_keys($class_storage->template_types));
|
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$provided_type_param_count = count($lhs_type_part->type_params);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
foreach ($reversed_class_template_types as $i => $type_name) {
|
2018-07-11 17:22:07 +02:00
|
|
|
|
if (isset($lhs_type_part->type_params[$provided_type_param_count - 1 - $i])) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$class_template_params[$type_name] =
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$lhs_type_part->type_params[$provided_type_param_count - 1 - $i];
|
2018-01-29 00:29:38 +01:00
|
|
|
|
} else {
|
|
|
|
|
$class_template_params[$type_name] = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($class_storage->template_types as $type_name => $_) {
|
2018-04-20 16:52:23 +02:00
|
|
|
|
if (!$stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
|| $stmt->var->name !== 'this'
|
|
|
|
|
) {
|
|
|
|
|
$class_template_params[$type_name] = Type::getMixed();
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
|
$method_id,
|
2018-07-17 04:48:53 +02:00
|
|
|
|
$args,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$class_template_params,
|
|
|
|
|
$context,
|
|
|
|
|
$code_location,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
|
) === 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();
|
2018-07-13 00:04:05 +02:00
|
|
|
|
continue 2;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-12 19:19:19 +02:00
|
|
|
|
$call_map_id = strtolower(
|
|
|
|
|
$codebase->methods->getDeclaringMethodId($method_id) ?: $method_id
|
|
|
|
|
);
|
2018-05-12 06:28:21 +02:00
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
if ($method_name_lc === '__tostring') {
|
|
|
|
|
$return_type_candidate = Type::getString();
|
2018-05-12 06:28:21 +02:00
|
|
|
|
} elseif ($call_map_id && CallMap::inCallMap($call_map_id)) {
|
2018-07-06 05:02:09 +02:00
|
|
|
|
if (($class_template_params || $class_storage->stubbed)
|
2018-02-01 05:27:25 +01:00
|
|
|
|
&& 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;
|
|
|
|
|
|
2018-07-06 05:02:09 +02:00
|
|
|
|
if ($class_template_params) {
|
|
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
|
|
|
|
$class_template_params
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-02-01 05:27:25 +01:00
|
|
|
|
} else {
|
2018-05-12 06:28:21 +02:00
|
|
|
|
if ($call_map_id === 'domnode::appendchild'
|
2018-07-17 04:48:53 +02:00
|
|
|
|
&& isset($args[0]->value->inferredType)
|
|
|
|
|
&& $args[0]->value->inferredType->hasObjectType()
|
2018-05-12 06:28:21 +02:00
|
|
|
|
) {
|
2018-07-17 04:48:53 +02:00
|
|
|
|
$return_type_candidate = clone $args[0]->value->inferredType;
|
|
|
|
|
} elseif ($call_map_id === 'simplexmlelement::asxml' && !count($args)) {
|
2018-06-06 05:42:02 +02:00
|
|
|
|
$return_type_candidate = Type::parseString('string|false');
|
2018-05-12 06:28:21 +02:00
|
|
|
|
} else {
|
|
|
|
|
$return_type_candidate = CallMap::getReturnTypeFromCallMap($call_map_id);
|
2018-06-18 16:07:05 +02:00
|
|
|
|
if ($return_type_candidate->isFalsable()) {
|
|
|
|
|
$return_type_candidate->ignore_falsable_issues = true;
|
|
|
|
|
}
|
2018-05-12 06:28:21 +02:00
|
|
|
|
}
|
2018-02-01 05:27:25 +01:00
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$return_type_candidate = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$return_type_candidate,
|
|
|
|
|
$fq_class_name,
|
2018-11-23 19:24:35 +01:00
|
|
|
|
$lhs_type_part
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
} else {
|
2018-11-25 17:11:33 +01:00
|
|
|
|
if ($check_visibility) {
|
|
|
|
|
if (MethodAnalyzer::checkMethodVisibility(
|
|
|
|
|
$method_id,
|
|
|
|
|
$context->self,
|
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
|
$name_code_location,
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (MethodAnalyzer::checkMethodNotDeprecated(
|
|
|
|
|
$codebase,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$method_id,
|
2018-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 01:37:09 +01:00
|
|
|
|
if (!self::checkMagicGetterOrSetterProperty(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-02-10 01:37:09 +01:00
|
|
|
|
$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,
|
2018-05-20 18:47:18 +02:00
|
|
|
|
$self_fq_class_name,
|
2018-07-17 04:48:53 +02:00
|
|
|
|
$args
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
|
if ($codebase->server_mode && $method_id) {
|
|
|
|
|
$codebase->analyzer->addNodeReference(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
|
$stmt->name,
|
|
|
|
|
$method_id . '()'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 04:01:05 +02:00
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
|
$return_type_candidate = $stmt->inferredType;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
if ($return_type_candidate) {
|
|
|
|
|
$return_type_candidate = clone $return_type_candidate;
|
|
|
|
|
|
|
|
|
|
if ($class_template_params) {
|
|
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
|
|
|
|
$class_template_params
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$return_type_candidate = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$return_type_candidate,
|
|
|
|
|
$self_fq_class_name,
|
2018-11-23 19:24:35 +01:00
|
|
|
|
$lhs_type_part
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
|
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(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
new CodeLocation($source, $stmt),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2018-08-10 05:29:30 +02:00
|
|
|
|
$context->phantom_classes
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} 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-07-11 17:22:07 +02:00
|
|
|
|
if (count($lhs_types) === 1) {
|
|
|
|
|
$method_storage = $codebase->methods->getUserMethodStorage($method_id);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
2018-10-27 17:47:27 +02:00
|
|
|
|
if ($method_storage) {
|
|
|
|
|
if ($method_storage->assertions) {
|
|
|
|
|
self::applyAssertionsToContext(
|
2018-11-14 18:25:17 +01:00
|
|
|
|
$stmt->name,
|
2018-10-27 17:47:27 +02:00
|
|
|
|
$method_storage->assertions,
|
|
|
|
|
$args,
|
2018-11-16 00:50:08 +01:00
|
|
|
|
$class_template_params ?: [],
|
2018-10-30 14:20:34 +01:00
|
|
|
|
$method_storage->template_typeof_params ?: [],
|
2018-10-27 17:47:27 +02:00
|
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer
|
2018-10-27 17:47:27 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2018-07-11 17:22:07 +02:00
|
|
|
|
|
2018-10-27 17:47:27 +02:00
|
|
|
|
if ($method_storage->if_true_assertions) {
|
|
|
|
|
$stmt->ifTrueAssertions = $method_storage->if_true_assertions;
|
|
|
|
|
}
|
2018-07-11 17:22:07 +02:00
|
|
|
|
|
2018-10-27 17:47:27 +02:00
|
|
|
|
if ($method_storage->if_false_assertions) {
|
|
|
|
|
$stmt->ifFalseAssertions = $method_storage->if_false_assertions;
|
|
|
|
|
}
|
2018-07-11 17:22:07 +02:00
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 04:48:53 +02:00
|
|
|
|
if (!$args && $var_id) {
|
2018-04-28 19:46:01 +02:00
|
|
|
|
if ($config->memoize_method_calls) {
|
|
|
|
|
$method_var_id = $var_id . '->' . $method_name_lc . '()';
|
|
|
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$method_var_id])) {
|
|
|
|
|
$return_type_candidate = clone $context->vars_in_scope[$method_var_id];
|
|
|
|
|
} elseif ($return_type_candidate) {
|
|
|
|
|
$context->vars_in_scope[$method_var_id] = $return_type_candidate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-11-23 19:24:35 +01:00
|
|
|
|
if ($appearing_method_id && $declaring_method_id) {
|
|
|
|
|
foreach ($config->after_method_checks as $plugin_fq_class_name) {
|
|
|
|
|
$plugin_fq_class_name::afterMethodCallAnalysis(
|
|
|
|
|
$stmt,
|
|
|
|
|
$method_id,
|
|
|
|
|
$appearing_method_id,
|
|
|
|
|
$declaring_method_id,
|
|
|
|
|
$context,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$file_manipulations,
|
|
|
|
|
$return_type_candidate
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-02-12 02:56:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
2018-11-11 18:01:14 +01:00
|
|
|
|
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
|
2018-02-12 02:56:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2018-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidMethodCall(
|
|
|
|
|
'Cannot call method on ' . $invalid_class_type . ' variable ' . $var_id,
|
2018-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
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-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location,
|
2018-03-21 03:36:03 +01:00
|
|
|
|
$non_existent_method_ids[0]
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UndefinedMethod(
|
|
|
|
|
'Method ' . $non_existent_method_ids[0] . ' does not exist',
|
2018-07-14 00:47:21 +02:00
|
|
|
|
$name_code_location,
|
2018-03-21 03:36:03 +01:00
|
|
|
|
$non_existent_method_ids[0]
|
2018-01-29 00:29:38 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
|
)) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
if ($method_id === null) {
|
|
|
|
|
return self::checkMethodArgs(
|
|
|
|
|
$method_id,
|
2018-08-02 23:14:53 +02:00
|
|
|
|
$stmt->args,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$found_generic_params,
|
|
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
|
$statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
|
if ($codebase->server_mode
|
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
|
&& isset($stmt->inferredType)
|
|
|
|
|
) {
|
|
|
|
|
$codebase->analyzer->addNodeType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
|
$stmt->name,
|
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
$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.
|
|
|
|
|
*
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-02-10 01:37:09 +01:00
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
|
* @param string $fq_class_name
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private static function checkMagicGetterOrSetterProperty(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-02-10 01:37:09 +01:00
|
|
|
|
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;
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-02-10 01:37:09 +01:00
|
|
|
|
|
|
|
|
|
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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
|
$property_id
|
2018-02-10 01:37:09 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-02-10 01:37:09 +01:00
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
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) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$pseudo_set_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
2018-02-22 01:34:21 +01:00
|
|
|
|
$class_storage->pseudo_property_set_types['$' . $prop_name],
|
|
|
|
|
$fq_class_name,
|
2018-11-23 19:24:35 +01:00
|
|
|
|
new Type\Atomic\TNamedObject($fq_class_name)
|
2018-02-22 01:34:21 +01:00
|
|
|
|
);
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$type_match_found = TypeAnalyzer::isContainedBy(
|
|
|
|
|
$codebase,
|
2018-02-10 01:37:09 +01:00
|
|
|
|
$second_arg_type,
|
2018-02-22 01:34:21 +01:00
|
|
|
|
$pseudo_set_type,
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$second_arg_type->ignore_nullable_issues,
|
|
|
|
|
$second_arg_type->ignore_falsable_issues,
|
2018-02-22 01:34:21 +01:00
|
|
|
|
$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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-02-22 01:34:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-02-22 01:34:21 +01:00
|
|
|
|
)) {
|
|
|
|
|
// keep soldiering on
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeCoercion(
|
|
|
|
|
$prop_name . ' expects \'' . $pseudo_set_type . '\', '
|
|
|
|
|
. ' parent type `' . $second_arg_type . '` provided',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-02-22 01:34:21 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-02-22 01:34:21 +01:00
|
|
|
|
)) {
|
|
|
|
|
// keep soldiering on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$type_match_found && !$type_coerced_from_mixed) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (TypeAnalyzer::canBeContainedBy(
|
|
|
|
|
$codebase,
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$second_arg_type,
|
|
|
|
|
$pseudo_set_type
|
2018-02-22 01:34:21 +01:00
|
|
|
|
)) {
|
2018-05-03 19:56:30 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyInvalidPropertyAssignmentValue(
|
|
|
|
|
$prop_name . ' with declared type \''
|
|
|
|
|
. $pseudo_set_type
|
|
|
|
|
. '\' cannot be assigned possibly different type \'' . $second_arg_type . '\'',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
|
$property_id
|
2018-05-03 19:56:30 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-03 19:56:30 +02:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidPropertyAssignmentValue(
|
|
|
|
|
$prop_name . ' with declared type \''
|
|
|
|
|
. $pseudo_set_type
|
|
|
|
|
. '\' cannot be assigned type \'' . $second_arg_type . '\'',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
|
$property_id
|
2018-05-03 19:56:30 +02:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-03 19:56:30 +02:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-22 01:34:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
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',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
|
$property_id
|
2018-02-10 01:37:09 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-02-10 01:37:09 +01:00
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-05-09 04:01:05 +02:00
|
|
|
|
|
|
|
|
|
if (isset($class_storage->pseudo_property_get_types['$' . $prop_name])) {
|
|
|
|
|
$stmt->inferredType = clone $class_storage->pseudo_property_get_types['$' . $prop_name];
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 01:37:09 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|