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;
|
2019-01-12 15:13:54 +01:00
|
|
|
use Psalm\Codebase;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2018-11-12 16:46:55 +01:00
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2019-07-18 07:31:48 +02:00
|
|
|
use Psalm\Issue\ImpureMethodCall;
|
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;
|
2019-04-26 00:02:19 +02:00
|
|
|
use Psalm\Issue\MixedPropertyTypeCoercion;
|
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;
|
2019-04-26 00:02:19 +02:00
|
|
|
use Psalm\Issue\PropertyTypeCoercion;
|
2019-01-13 19:07:53 +01:00
|
|
|
use Psalm\Issue\UndefinedInterfaceMethod;
|
2020-01-06 20:23:33 +01:00
|
|
|
use Psalm\Issue\UndefinedMagicMethod;
|
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;
|
2019-01-23 05:42:54 +01:00
|
|
|
use Psalm\Storage\Assertion;
|
2019-01-12 15:13:54 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function is_string;
|
|
|
|
use function array_values;
|
|
|
|
use function array_shift;
|
2019-09-06 03:00:02 +02:00
|
|
|
use function array_unshift;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function get_class;
|
|
|
|
use function strtolower;
|
|
|
|
use function array_map;
|
|
|
|
use function array_merge;
|
|
|
|
use function explode;
|
2019-09-06 03:00:02 +02:00
|
|
|
use function implode;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_search;
|
|
|
|
use function array_keys;
|
|
|
|
use function in_array;
|
2019-08-14 06:47:57 +02:00
|
|
|
use Psalm\Internal\Taint\Source;
|
2019-11-08 17:56:33 +01:00
|
|
|
use Doctrine\Instantiator\Exception\UnexpectedValueException;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
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,
|
2019-05-27 15:18:34 +02:00
|
|
|
Context $context,
|
|
|
|
bool $real_method_call = true
|
2018-01-29 00:29:38 +01:00
|
|
|
) {
|
2019-08-30 22:40:32 +02:00
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-02 16:41:43 +01:00
|
|
|
$context->inside_call = $was_inside_call;
|
2019-08-30 22:40:32 +02:00
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2019-08-26 06:47:46 +02:00
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
$context->inside_call = true;
|
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;
|
|
|
|
}
|
2020-01-02 16:41:43 +01:00
|
|
|
$context->inside_call = $was_inside_call;
|
2018-02-17 23:23:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$lhs_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
|
|
|
);
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$class_type = $lhs_var_id && $context->hasVariable($lhs_var_id, $statements_analyzer)
|
|
|
|
? $context->vars_in_scope[$lhs_var_id]
|
2018-01-29 00:29:38 +01:00
|
|
|
: null;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_var_type = $statements_analyzer->node_data->getType($stmt->var)) {
|
|
|
|
$class_type = $stmt_var_type;
|
2018-01-29 00:29:38 +01:00
|
|
|
} elseif (!$class_type) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 15:36:25 +01:00
|
|
|
if (!$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;
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:05:28 +01:00
|
|
|
if ($class_type
|
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& ($class_type->isNull() || $class_type->isVoid())
|
|
|
|
) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
2019-01-07 22:38:37 +01:00
|
|
|
'Cannot call method ' . $stmt->name->name . ' on null value',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->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 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(
|
2019-01-07 22:38:37 +01:00
|
|
|
'Cannot call method ' . $stmt->name->name . ' on possibly null value',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->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
|
|
|
)) {
|
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(
|
2019-01-07 22:38:37 +01:00
|
|
|
'Cannot call method ' . $stmt->name->name . ' on possibly false value',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->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
|
|
|
)) {
|
2018-11-28 17:45:54 +01:00
|
|
|
// fall through
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$source = $statements_analyzer->getSource();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
if (!$class_type) {
|
|
|
|
$class_type = Type::getMixed();
|
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
$lhs_types = $class_type->getAtomicTypes();
|
2019-04-18 19:51:34 +02:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
$result = new AtomicMethodCallAnalysisResult();
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
foreach ($lhs_types as $lhs_type_part) {
|
2020-03-11 14:38:09 +01:00
|
|
|
AtomicMethodCallAnalyzer::analyze(
|
2019-04-18 19:51:34 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$codebase,
|
|
|
|
$context,
|
|
|
|
$lhs_type_part,
|
2019-10-11 18:02:41 +02:00
|
|
|
$lhs_type_part instanceof Type\Atomic\TNamedObject
|
|
|
|
|| $lhs_type_part instanceof Type\Atomic\TTemplateParam
|
|
|
|
? $lhs_type_part
|
|
|
|
: null,
|
2020-02-28 03:36:03 +01:00
|
|
|
false,
|
2019-04-18 19:51:34 +02:00
|
|
|
$lhs_var_id,
|
2020-03-11 14:38:09 +01:00
|
|
|
$result
|
2019-04-18 19:51:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->invalid_method_call_types) {
|
|
|
|
$invalid_class_type = $result->invalid_method_call_types[0];
|
2019-04-18 19:51:34 +02:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->has_valid_method_call_type || $result->has_mixed_method_call) {
|
2019-04-18 19:51:34 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidMethodCall(
|
|
|
|
'Cannot call method on possible ' . $invalid_class_type . ' variable ' . $lhs_var_id,
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep going
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidMethodCall(
|
|
|
|
'Cannot call method on ' . $invalid_class_type . ' variable ' . $lhs_var_id,
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep going
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->non_existent_magic_method_ids) {
|
2020-01-06 20:23:33 +01:00
|
|
|
if ($context->check_methods) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMagicMethod(
|
2020-03-11 14:38:09 +01:00
|
|
|
'Magic method ' . $result->non_existent_magic_method_ids[0] . ' does not exist',
|
2020-01-06 20:23:33 +01:00
|
|
|
new CodeLocation($source, $stmt->name),
|
2020-03-11 14:38:09 +01:00
|
|
|
$result->non_existent_magic_method_ids[0]
|
2020-01-06 20:23:33 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep going
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->non_existent_class_method_ids) {
|
2019-04-18 19:51:34 +02:00
|
|
|
if ($context->check_methods) {
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->existent_method_ids || $result->has_mixed_method_call) {
|
2019-01-12 15:13:54 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2019-04-18 19:51:34 +02:00
|
|
|
new PossiblyUndefinedMethod(
|
2020-03-11 14:38:09 +01:00
|
|
|
'Method ' . $result->non_existent_class_method_ids[0] . ' does not exist',
|
2019-04-18 19:51:34 +02:00
|
|
|
new CodeLocation($source, $stmt->name),
|
2020-03-11 14:38:09 +01:00
|
|
|
$result->non_existent_class_method_ids[0]
|
2019-01-12 15:13:54 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
2019-02-24 07:33:25 +01:00
|
|
|
// keep going
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-18 19:51:34 +02:00
|
|
|
new UndefinedMethod(
|
2020-03-11 14:38:09 +01:00
|
|
|
'Method ' . $result->non_existent_class_method_ids[0] . ' does not exist',
|
2019-04-18 19:51:34 +02:00
|
|
|
new CodeLocation($source, $stmt->name),
|
2020-03-11 14:38:09 +01:00
|
|
|
$result->non_existent_class_method_ids[0]
|
2019-01-12 15:13:54 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
2019-02-24 07:33:25 +01:00
|
|
|
// keep going
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
return null;
|
|
|
|
}
|
2019-01-13 19:07:53 +01:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->non_existent_interface_method_ids) {
|
2019-04-18 19:51:34 +02:00
|
|
|
if ($context->check_methods) {
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->existent_method_ids || $result->has_mixed_method_call) {
|
2019-04-18 19:51:34 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedMethod(
|
2020-03-11 14:38:09 +01:00
|
|
|
'Method ' . $result->non_existent_interface_method_ids[0] . ' does not exist',
|
2019-04-18 19:51:34 +02:00
|
|
|
new CodeLocation($source, $stmt->name),
|
2020-03-11 14:38:09 +01:00
|
|
|
$result->non_existent_interface_method_ids[0]
|
2019-04-18 19:51:34 +02:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep going
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedInterfaceMethod(
|
2020-03-11 14:38:09 +01:00
|
|
|
'Method ' . $result->non_existent_interface_method_ids[0] . ' does not exist',
|
2019-04-18 19:51:34 +02:00
|
|
|
new CodeLocation($source, $stmt->name),
|
2020-03-11 14:38:09 +01:00
|
|
|
$result->non_existent_interface_method_ids[0]
|
2019-04-18 19:51:34 +02:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// keep going
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
$stmt_type = $result->return_type;
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
if ($stmt_type) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if ($result->returns_by_ref) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$stmt_type) {
|
|
|
|
$stmt_type = Type::getMixed();
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2019-04-18 19:51:34 +02:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
$stmt_type->by_ref = $result->returns_by_ref;
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types
|
2019-07-01 15:55:39 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
2019-11-25 17:44:54 +01:00
|
|
|
&& $stmt_type
|
2019-01-12 15:13:54 +01:00
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
2020-02-23 23:03:27 +01:00
|
|
|
$stmt_type->getId(),
|
2019-05-17 18:38:29 +02:00
|
|
|
$stmt
|
2019-01-12 15:13:54 +01:00
|
|
|
);
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-03-11 14:38:09 +01:00
|
|
|
if (!$result->existent_method_ids) {
|
2019-06-23 14:08:00 +02:00
|
|
|
return self::checkMethodArgs(
|
|
|
|
null,
|
|
|
|
$stmt->args,
|
2019-11-04 03:27:40 +01:00
|
|
|
null,
|
2019-06-23 14:08:00 +02:00
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
// if we called a method on this nullable variable, remove the nullable status here
|
|
|
|
// because any further calls must have worked
|
|
|
|
if ($lhs_var_id
|
2019-04-18 19:51:34 +02:00
|
|
|
&& !$class_type->isMixed()
|
2020-03-11 14:38:09 +01:00
|
|
|
&& $result->has_valid_method_call_type
|
|
|
|
&& !$result->has_mixed_method_call
|
|
|
|
&& !$result->invalid_method_call_types
|
2019-01-12 15:13:54 +01:00
|
|
|
&& ($class_type->from_docblock || $class_type->isNullable())
|
2019-05-27 15:18:34 +02:00
|
|
|
&& $real_method_call
|
2019-01-12 15:13:54 +01:00
|
|
|
) {
|
|
|
|
$keys_to_remove = [];
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-06-26 06:14:06 +02:00
|
|
|
$class_type = clone $class_type;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($class_type->getAtomicTypes() as $key => $type) {
|
2019-01-12 15:13:54 +01:00
|
|
|
if (!$type instanceof TNamedObject) {
|
|
|
|
$keys_to_remove[] = $key;
|
2018-01-29 00:29:38 +01:00
|
|
|
} else {
|
2019-01-12 15:13:54 +01:00
|
|
|
$type->from_docblock = false;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
foreach ($keys_to_remove as $key) {
|
|
|
|
$class_type->removeType($key);
|
|
|
|
}
|
2018-02-17 23:16:22 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$class_type->from_docblock = false;
|
2018-02-17 23:16:22 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$context->removeVarFromConflictingClauses($lhs_var_id, null, $statements_analyzer);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$context->vars_in_scope[$lhs_var_id] = $class_type;
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 17:52:38 +02:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
/**
|
2020-02-15 02:54:26 +01:00
|
|
|
* @param lowercase-string $method_name
|
2019-03-22 23:02:33 +01:00
|
|
|
* @return array<string, array<string, array{Type\Union, 1?:int}>>|null
|
2019-01-12 15:13:54 +01:00
|
|
|
*/
|
2019-01-24 22:09:04 +01:00
|
|
|
public static function getClassTemplateParams(
|
2019-01-12 15:13:54 +01:00
|
|
|
Codebase $codebase,
|
|
|
|
ClassLikeStorage $class_storage,
|
2020-02-06 22:38:45 +01:00
|
|
|
string $static_fq_class_name,
|
2019-02-10 21:01:10 +01:00
|
|
|
string $method_name = null,
|
2019-01-24 22:09:04 +01:00
|
|
|
Type\Atomic $lhs_type_part = null,
|
2019-01-12 15:13:54 +01:00
|
|
|
string $lhs_var_id = null
|
|
|
|
) {
|
2020-02-06 22:38:45 +01:00
|
|
|
$static_class_storage = $codebase->classlike_storage_provider->get($static_fq_class_name);
|
2019-01-12 15:13:54 +01:00
|
|
|
|
2019-07-05 04:26:40 +02:00
|
|
|
$non_trait_class_storage = $class_storage->is_trait
|
2020-02-06 22:38:45 +01:00
|
|
|
? $static_class_storage
|
2019-07-05 04:26:40 +02:00
|
|
|
: $class_storage;
|
|
|
|
|
2019-01-24 22:09:04 +01:00
|
|
|
$template_types = $class_storage->template_types;
|
|
|
|
|
2019-11-08 17:56:33 +01:00
|
|
|
$candidate_class_storages = [$class_storage];
|
|
|
|
|
2020-02-06 22:38:45 +01:00
|
|
|
if ($static_class_storage->template_type_extends
|
2019-03-22 21:29:30 +01:00
|
|
|
&& $method_name
|
2019-07-05 04:26:40 +02:00
|
|
|
&& !empty($non_trait_class_storage->overridden_method_ids[$method_name])
|
2019-03-22 21:29:30 +01:00
|
|
|
&& isset($class_storage->methods[$method_name])
|
2019-07-05 04:26:40 +02:00
|
|
|
&& (!isset($non_trait_class_storage->methods[$method_name]->return_type)
|
2019-06-08 15:44:22 +02:00
|
|
|
|| $class_storage->methods[$method_name]->inherited_return_type)
|
2019-03-22 21:29:30 +01:00
|
|
|
) {
|
2019-07-05 04:26:40 +02:00
|
|
|
foreach ($non_trait_class_storage->overridden_method_ids[$method_name] as $overridden_method_id) {
|
2019-03-22 21:29:30 +01:00
|
|
|
$overridden_storage = $codebase->methods->getStorage($overridden_method_id);
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
if (!$overridden_storage->return_type) {
|
2019-11-08 17:56:33 +01:00
|
|
|
continue;
|
2019-03-22 21:29:30 +01:00
|
|
|
}
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
if ($overridden_storage->return_type->isNull()) {
|
2019-11-08 17:56:33 +01:00
|
|
|
continue;
|
2019-03-22 21:29:30 +01:00
|
|
|
}
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_overridden_class = $overridden_method_id->fq_class_name;
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
$overridden_class_storage = $codebase->classlike_storage_provider->get($fq_overridden_class);
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-11-08 17:56:33 +01:00
|
|
|
$overridden_template_types = $overridden_class_storage->template_types;
|
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
if (!$template_types) {
|
2019-11-08 17:56:33 +01:00
|
|
|
$template_types = $overridden_template_types;
|
|
|
|
} elseif ($overridden_template_types) {
|
|
|
|
foreach ($overridden_template_types as $template_name => $template_map) {
|
|
|
|
if (isset($template_types[$template_name])) {
|
|
|
|
$template_types[$template_name] = array_merge(
|
|
|
|
$template_types[$template_name],
|
|
|
|
$template_map
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$template_types[$template_name] = $template_map;
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 22:09:04 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 17:56:33 +01:00
|
|
|
$candidate_class_storages[] = $overridden_class_storage;
|
2019-01-24 22:09:04 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
if (!$template_types) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$class_template_params = [];
|
2020-02-06 22:38:45 +01:00
|
|
|
$e = $static_class_storage->template_type_extends;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
2020-02-06 22:38:45 +01:00
|
|
|
if ($class_storage === $static_class_storage && $static_class_storage->template_types) {
|
2019-01-12 15:13:54 +01:00
|
|
|
$i = 0;
|
2019-11-08 18:49:53 +01:00
|
|
|
|
2020-02-06 22:38:45 +01:00
|
|
|
foreach ($static_class_storage->template_types as $type_name => $_) {
|
2019-01-12 15:13:54 +01:00
|
|
|
if (isset($lhs_type_part->type_params[$i])) {
|
2020-02-06 22:38:45 +01:00
|
|
|
if ($lhs_var_id !== '$this' || $static_fq_class_name !== $static_class_storage->name) {
|
|
|
|
$class_template_params[$type_name][$static_class_storage->name] = [
|
|
|
|
$lhs_type_part->type_params[$i]
|
|
|
|
];
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$i++;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$i = 0;
|
2019-01-24 22:09:04 +01:00
|
|
|
foreach ($template_types as $type_name => $_) {
|
2019-01-12 15:13:54 +01:00
|
|
|
if (isset($class_template_params[$type_name])) {
|
|
|
|
$i++;
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-02-06 22:38:45 +01:00
|
|
|
if ($class_storage !== $static_class_storage
|
2019-06-25 05:31:06 +02:00
|
|
|
&& isset($e[$class_storage->name][$type_name])
|
2019-01-12 15:13:54 +01:00
|
|
|
) {
|
2019-06-25 05:31:06 +02:00
|
|
|
$input_type_extends = $e[$class_storage->name][$type_name];
|
2019-03-16 16:15:25 +01:00
|
|
|
|
|
|
|
$output_type_extends = null;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($input_type_extends->getAtomicTypes() as $type_extends_atomic) {
|
2019-03-16 16:15:25 +01:00
|
|
|
if ($type_extends_atomic instanceof Type\Atomic\TTemplateParam) {
|
2020-02-06 22:38:45 +01:00
|
|
|
if (isset($static_class_storage->template_types[$type_extends_atomic->param_name])) {
|
2019-03-16 16:15:25 +01:00
|
|
|
$mapped_offset = array_search(
|
|
|
|
$type_extends_atomic->param_name,
|
2020-02-06 22:38:45 +01:00
|
|
|
array_keys($static_class_storage->template_types)
|
2019-03-16 16:15:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($lhs_type_part->type_params[(int) $mapped_offset])) {
|
|
|
|
$candidate_type = $lhs_type_part->type_params[(int) $mapped_offset];
|
|
|
|
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
$output_type_extends = $candidate_type;
|
|
|
|
} else {
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
$candidate_type,
|
|
|
|
$output_type_extends
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 13:25:27 +01:00
|
|
|
} elseif (isset(
|
2020-02-06 22:38:45 +01:00
|
|
|
$static_class_storage
|
2019-12-30 13:25:27 +01:00
|
|
|
->template_type_extends
|
|
|
|
[$type_extends_atomic->defining_class]
|
|
|
|
[$type_extends_atomic->param_name]
|
|
|
|
)) {
|
2019-03-16 16:15:25 +01:00
|
|
|
$mapped_offset = array_search(
|
|
|
|
$type_extends_atomic->param_name,
|
2020-02-06 22:38:45 +01:00
|
|
|
array_keys($static_class_storage
|
2019-01-13 00:18:23 +01:00
|
|
|
->template_type_extends
|
2019-06-25 05:31:06 +02:00
|
|
|
[$type_extends_atomic->defining_class])
|
2019-03-16 16:15:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($lhs_type_part->type_params[(int) $mapped_offset])) {
|
|
|
|
$candidate_type = $lhs_type_part->type_params[(int) $mapped_offset];
|
|
|
|
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
$output_type_extends = $candidate_type;
|
|
|
|
} else {
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
$candidate_type,
|
|
|
|
$output_type_extends
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
$output_type_extends = new Type\Union([$type_extends_atomic]);
|
|
|
|
} else {
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
new Type\Union([$type_extends_atomic]),
|
|
|
|
$output_type_extends
|
|
|
|
);
|
2019-01-28 18:27:04 +01:00
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-16 16:15:25 +01:00
|
|
|
|
2020-02-06 22:38:45 +01:00
|
|
|
if ($lhs_var_id !== '$this' || $static_fq_class_name !== $class_storage->name) {
|
|
|
|
$class_template_params[$type_name][$class_storage->name] = [
|
|
|
|
$output_type_extends ?: Type::getMixed()
|
|
|
|
];
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:38:45 +01:00
|
|
|
if (($lhs_var_id !== '$this' || $static_fq_class_name !== $class_storage->name)
|
|
|
|
&& !isset($class_template_params[$type_name])
|
|
|
|
) {
|
2019-12-22 02:42:39 +01:00
|
|
|
$class_template_params[$type_name] = [
|
|
|
|
$class_storage->name => [Type::getMixed()]
|
|
|
|
];
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
$i++;
|
|
|
|
}
|
2019-11-08 17:56:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($template_types as $type_name => $type_map) {
|
|
|
|
foreach ($type_map as list($type)) {
|
|
|
|
foreach ($candidate_class_storages as $candidate_class_storage) {
|
2020-02-06 22:38:45 +01:00
|
|
|
if ($candidate_class_storage !== $static_class_storage
|
2019-11-08 17:56:33 +01:00
|
|
|
&& isset($e[$candidate_class_storage->name][$type_name])
|
|
|
|
&& !isset($class_template_params[$type_name][$candidate_class_storage->name])
|
2019-03-22 20:59:10 +01:00
|
|
|
) {
|
2019-11-08 17:56:33 +01:00
|
|
|
$class_template_params[$type_name][$candidate_class_storage->name] = [
|
2020-03-07 01:24:47 +01:00
|
|
|
new Type\Union(
|
|
|
|
self::expandType(
|
|
|
|
$e[$candidate_class_storage->name][$type_name],
|
|
|
|
$e,
|
|
|
|
$static_class_storage->name,
|
|
|
|
$static_class_storage->template_types
|
|
|
|
)
|
|
|
|
)
|
2019-03-22 20:59:10 +01:00
|
|
|
];
|
|
|
|
}
|
2019-11-08 17:56:33 +01:00
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
2019-11-08 17:56:33 +01:00
|
|
|
if ($lhs_var_id !== '$this') {
|
|
|
|
if (!isset($class_template_params[$type_name])) {
|
|
|
|
$class_template_params[$type_name][$class_storage->name] = [$type];
|
2019-01-12 15:13:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
|
|
return $class_template_params;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2018-02-10 01:37:09 +01:00
|
|
|
|
2020-03-07 01:24:47 +01:00
|
|
|
/**
|
|
|
|
* @param array<string, array<int|string, Type\Union>> $e
|
|
|
|
* @return non-empty-list<Type\Atomic>
|
|
|
|
*/
|
|
|
|
private static function expandType(
|
|
|
|
Type\Union $input_type_extends,
|
|
|
|
array $e,
|
|
|
|
string $static_fq_class_name,
|
|
|
|
?array $static_template_types
|
|
|
|
) : array {
|
|
|
|
$output_type_extends = [];
|
|
|
|
|
|
|
|
foreach ($input_type_extends->getAtomicTypes() as $type_extends_atomic) {
|
|
|
|
if ($type_extends_atomic instanceof Type\Atomic\TTemplateParam
|
|
|
|
&& ($static_fq_class_name !== $type_extends_atomic->defining_class
|
|
|
|
|| !isset($static_template_types[$type_extends_atomic->param_name]))
|
|
|
|
&& isset($e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name])
|
|
|
|
) {
|
|
|
|
$output_type_extends = array_merge(
|
|
|
|
$output_type_extends,
|
|
|
|
self::expandType(
|
|
|
|
$e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name],
|
|
|
|
$e,
|
|
|
|
$static_fq_class_name,
|
|
|
|
$static_template_types
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$output_type_extends[] = $type_extends_atomic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output_type_extends;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|