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;
|
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;
|
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
|
|
|
|
) {
|
2018-05-09 15:30:23 +02:00
|
|
|
|
$stmt->inferredType = null;
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 22:40:32 +02:00
|
|
|
|
if (!$was_inside_call) {
|
|
|
|
|
$context->inside_call = false;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2019-08-26 06:47:46 +02:00
|
|
|
|
if (!$was_inside_call) {
|
|
|
|
|
$context->inside_call = false;
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
|
$class_type = $stmt->var->inferredType;
|
|
|
|
|
} elseif (!$class_type) {
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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(
|
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-13 19:07:53 +01:00
|
|
|
|
$non_existent_class_method_ids = [];
|
|
|
|
|
$non_existent_interface_method_ids = [];
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$existent_method_ids = [];
|
2018-12-10 17:28:05 +01:00
|
|
|
|
$has_mixed_method_call = false;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
$invalid_method_call_types = [];
|
|
|
|
|
$has_valid_method_call_type = false;
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$source = $statements_analyzer->getSource();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
$returns_by_ref = false;
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$no_method_id = false;
|
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
if (!$class_type) {
|
|
|
|
|
$class_type = Type::getMixed();
|
|
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
$return_type = null;
|
|
|
|
|
|
|
|
|
|
$lhs_types = $class_type->getTypes();
|
|
|
|
|
|
|
|
|
|
foreach ($lhs_types as $lhs_type_part) {
|
|
|
|
|
$result = self::analyzeAtomicCall(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
|
|
|
|
$codebase,
|
|
|
|
|
$context,
|
|
|
|
|
$lhs_type_part,
|
2019-05-28 06:32:17 +02:00
|
|
|
|
$lhs_type_part instanceof Type\Atomic\TNamedObject ? $lhs_type_part : null,
|
2019-04-18 19:51:34 +02:00
|
|
|
|
$lhs_var_id,
|
|
|
|
|
$return_type,
|
|
|
|
|
$returns_by_ref,
|
|
|
|
|
$has_mock,
|
|
|
|
|
$has_valid_method_call_type,
|
|
|
|
|
$has_mixed_method_call,
|
|
|
|
|
$invalid_method_call_types,
|
|
|
|
|
$existent_method_ids,
|
|
|
|
|
$non_existent_class_method_ids,
|
|
|
|
|
$non_existent_interface_method_ids
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
|
return false;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
if ($result === true) {
|
|
|
|
|
$no_method_id = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($invalid_method_call_types) {
|
|
|
|
|
$invalid_class_type = $invalid_method_call_types[0];
|
|
|
|
|
|
|
|
|
|
if ($has_valid_method_call_type || $has_mixed_method_call) {
|
|
|
|
|
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
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
if ($non_existent_class_method_ids) {
|
|
|
|
|
if ($context->check_methods) {
|
|
|
|
|
if ($existent_method_ids || $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(
|
|
|
|
|
'Method ' . $non_existent_class_method_ids[0] . ' does not exist',
|
|
|
|
|
new CodeLocation($source, $stmt->name),
|
|
|
|
|
$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(
|
|
|
|
|
'Method ' . $non_existent_class_method_ids[0] . ' does not exist',
|
|
|
|
|
new CodeLocation($source, $stmt->name),
|
|
|
|
|
$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
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
if ($non_existent_interface_method_ids) {
|
|
|
|
|
if ($context->check_methods) {
|
|
|
|
|
if ($existent_method_ids || $has_mixed_method_call) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyUndefinedMethod(
|
|
|
|
|
'Method ' . $non_existent_interface_method_ids[0] . ' does not exist',
|
|
|
|
|
new CodeLocation($source, $stmt->name),
|
|
|
|
|
$non_existent_interface_method_ids[0]
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// keep going
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UndefinedInterfaceMethod(
|
|
|
|
|
'Method ' . $non_existent_interface_method_ids[0] . ' does not exist',
|
|
|
|
|
new CodeLocation($source, $stmt->name),
|
|
|
|
|
$non_existent_interface_method_ids[0]
|
|
|
|
|
),
|
|
|
|
|
$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
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
$stmt->inferredType = $return_type;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-04-18 19:51:34 +02:00
|
|
|
|
if ($returns_by_ref) {
|
|
|
|
|
if (!$stmt->inferredType) {
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
2019-04-18 19:51:34 +02:00
|
|
|
|
|
|
|
|
|
$stmt->inferredType->by_ref = $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-01-12 15:13:54 +01:00
|
|
|
|
&& isset($stmt->inferredType)
|
|
|
|
|
) {
|
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
|
$stmt->name,
|
2019-05-17 18:38:29 +02:00
|
|
|
|
(string) $stmt->inferredType,
|
|
|
|
|
$stmt
|
2019-01-12 15:13:54 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-06-23 14:08:00 +02:00
|
|
|
|
if ($no_method_id) {
|
|
|
|
|
return self::checkMethodArgs(
|
|
|
|
|
null,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
$found_generic_params,
|
|
|
|
|
$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()
|
2019-01-12 15:13:54 +01:00
|
|
|
|
&& $has_valid_method_call_type
|
|
|
|
|
&& !$has_mixed_method_call
|
|
|
|
|
&& !$invalid_method_call_types
|
|
|
|
|
&& $existent_method_ids
|
|
|
|
|
&& ($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;
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
foreach ($class_type->getTypes() as $key => $type) {
|
|
|
|
|
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
|
|
|
|
/**
|
|
|
|
|
* [analyzeAtomicCall description]
|
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
|
|
|
|
* @param PhpParser\Node\Expr\MethodCall $stmt
|
|
|
|
|
* @param Codebase $codebase
|
|
|
|
|
* @param Context $context
|
|
|
|
|
* @param Type\Atomic $lhs_type_part
|
|
|
|
|
* @param ?string $lhs_var_id
|
|
|
|
|
* @param ?Type\Union &$return_type
|
|
|
|
|
* @param bool &$returns_by_ref
|
|
|
|
|
* @param bool &$has_mock
|
|
|
|
|
* @param bool &$has_valid_method_call_type
|
|
|
|
|
* @param bool &$has_mixed_method_call
|
|
|
|
|
* @param array<string> &$invalid_method_call_types
|
|
|
|
|
* @param array<string> &$existent_method_ids
|
2019-01-13 19:07:53 +01:00
|
|
|
|
* @param array<string> &$non_existent_class_method_ids
|
|
|
|
|
* @param array<string> &$non_existent_interface_method_ids
|
2019-01-12 15:13:54 +01:00
|
|
|
|
* @return null|bool
|
|
|
|
|
*/
|
|
|
|
|
private static function analyzeAtomicCall(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
PhpParser\Node\Expr\MethodCall $stmt,
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
Context $context,
|
|
|
|
|
Type\Atomic $lhs_type_part,
|
2019-05-28 06:32:17 +02:00
|
|
|
|
?Type\Atomic\TNamedObject $static_type,
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$lhs_var_id,
|
|
|
|
|
&$return_type,
|
|
|
|
|
&$returns_by_ref,
|
|
|
|
|
&$has_mock,
|
|
|
|
|
&$has_valid_method_call_type,
|
|
|
|
|
&$has_mixed_method_call,
|
|
|
|
|
&$invalid_method_call_types,
|
|
|
|
|
&$existent_method_ids,
|
2019-01-13 19:07:53 +01:00
|
|
|
|
&$non_existent_class_method_ids,
|
2019-05-24 18:48:37 +02:00
|
|
|
|
&$non_existent_interface_method_ids,
|
|
|
|
|
bool &$check_visibility = true
|
2019-01-12 15:13:54 +01:00
|
|
|
|
) {
|
|
|
|
|
$config = $codebase->config;
|
2018-08-02 23:14:53 +02:00
|
|
|
|
|
2019-02-22 03:40:06 +01:00
|
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TTemplateParam
|
2019-01-12 15:13:54 +01:00
|
|
|
|
&& !$lhs_type_part->as->isMixed()
|
|
|
|
|
) {
|
|
|
|
|
$extra_types = $lhs_type_part->extra_types;
|
2018-04-22 04:13:10 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$lhs_type_part = array_values(
|
|
|
|
|
$lhs_type_part->as->getTypes()
|
|
|
|
|
)[0];
|
2018-07-17 04:48:53 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$lhs_type_part->from_docblock = true;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($lhs_type_part instanceof TNamedObject) {
|
|
|
|
|
$lhs_type_part->extra_types = $extra_types;
|
2019-06-20 16:09:03 +02:00
|
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\TObject && $extra_types) {
|
|
|
|
|
$lhs_type_part = array_shift($extra_types);
|
|
|
|
|
if ($extra_types) {
|
|
|
|
|
$lhs_type_part->extra_types = $extra_types;
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
2018-03-21 21:55:31 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$has_mixed_method_call = true;
|
|
|
|
|
}
|
2018-03-21 21:55:31 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$source = $statements_analyzer->getSource();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (!$lhs_type_part instanceof TNamedObject) {
|
|
|
|
|
switch (get_class($lhs_type_part)) {
|
|
|
|
|
case Type\Atomic\TNull::class:
|
|
|
|
|
case Type\Atomic\TFalse::class:
|
|
|
|
|
// handled above
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case Type\Atomic\TInt::class:
|
|
|
|
|
case Type\Atomic\TLiteralInt::class:
|
|
|
|
|
case Type\Atomic\TFloat::class:
|
|
|
|
|
case Type\Atomic\TLiteralFloat::class:
|
|
|
|
|
case Type\Atomic\TBool::class:
|
|
|
|
|
case Type\Atomic\TTrue::class:
|
|
|
|
|
case Type\Atomic\TArray::class:
|
|
|
|
|
case Type\Atomic\TNonEmptyArray::class:
|
|
|
|
|
case Type\Atomic\ObjectLike::class:
|
|
|
|
|
case Type\Atomic\TString::class:
|
|
|
|
|
case Type\Atomic\TSingleLetter::class:
|
|
|
|
|
case Type\Atomic\TLiteralString::class:
|
|
|
|
|
case Type\Atomic\TLiteralClassString::class:
|
2019-02-22 03:40:06 +01:00
|
|
|
|
case Type\Atomic\TTemplateParamClass::class:
|
2019-01-12 15:13:54 +01:00
|
|
|
|
case Type\Atomic\TNumericString::class:
|
|
|
|
|
case Type\Atomic\THtmlEscapedString::class:
|
|
|
|
|
case Type\Atomic\TClassString::class:
|
|
|
|
|
case Type\Atomic\TIterable::class:
|
|
|
|
|
$invalid_method_call_types[] = (string)$lhs_type_part;
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-22 03:40:06 +01:00
|
|
|
|
case Type\Atomic\TTemplateParam::class:
|
2019-01-12 15:13:54 +01:00
|
|
|
|
case Type\Atomic\TEmptyMixed::class:
|
|
|
|
|
case Type\Atomic\TMixed::class:
|
|
|
|
|
case Type\Atomic\TNonEmptyMixed::class:
|
|
|
|
|
case Type\Atomic\TObject::class:
|
2019-01-18 06:56:24 +01:00
|
|
|
|
case Type\Atomic\TObjectWithProperties::class:
|
2019-03-23 14:50:47 +01:00
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
|
) {
|
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
2018-11-25 17:11:33 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$has_mixed_method_call = true;
|
2018-11-25 17:11:33 +01:00
|
|
|
|
|
2019-08-19 04:27:19 +02:00
|
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TObjectWithProperties
|
|
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& isset($lhs_type_part->methods[$stmt->name->name])
|
|
|
|
|
) {
|
|
|
|
|
$existent_method_ids[] = $lhs_type_part->methods[$stmt->name->name];
|
|
|
|
|
} else {
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
|
strtolower($stmt->name->name),
|
|
|
|
|
$context->calling_method_id ?: $statements_analyzer->getFileName()
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-17 17:12:18 +02:00
|
|
|
|
|
2019-08-19 04:27:19 +02:00
|
|
|
|
if ($context->check_methods) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new MixedMethodCall(
|
|
|
|
|
'Cannot determine the type of the object on the left hand side of this expression',
|
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
2018-11-25 17:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2018-11-25 17:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-11-02 18:08:56 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-03-23 14:50:47 +01:00
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
|
) {
|
|
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
|
|
|
|
|
}
|
2018-10-30 15:34:02 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$has_valid_method_call_type = true;
|
2018-10-30 15:34:02 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$fq_class_name = $lhs_type_part->value;
|
|
|
|
|
|
|
|
|
|
$is_mock = ExpressionAnalyzer::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();
|
2019-01-13 15:49:34 +01:00
|
|
|
|
|
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($lhs_var_id === '$this') {
|
|
|
|
|
$does_class_exist = true;
|
|
|
|
|
} else {
|
|
|
|
|
$does_class_exist = ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
new CodeLocation($source, $stmt->var),
|
2019-05-16 00:41:26 +02:00
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
true,
|
|
|
|
|
$lhs_type_part->from_docblock
|
2019-01-12 15:13:54 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$does_class_exist) {
|
2019-05-16 00:41:26 +02:00
|
|
|
|
$non_existent_class_method_ids[] = $fq_class_name . '::*';
|
|
|
|
|
return false;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 18:48:37 +02:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
|
|
$check_visibility = $check_visibility && !$class_storage->override_method_visibility;
|
|
|
|
|
|
|
|
|
|
$intersection_types = $lhs_type_part->getIntersectionTypes();
|
|
|
|
|
|
|
|
|
|
$all_intersection_return_type = null;
|
|
|
|
|
$all_intersection_existent_method_ids = [];
|
|
|
|
|
|
|
|
|
|
if ($intersection_types) {
|
|
|
|
|
foreach ($intersection_types as $intersection_type) {
|
|
|
|
|
$i_non_existent_class_method_ids = [];
|
|
|
|
|
$i_non_existent_interface_method_ids = [];
|
|
|
|
|
|
|
|
|
|
$intersection_return_type = null;
|
|
|
|
|
|
|
|
|
|
self::analyzeAtomicCall(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
|
|
|
|
$codebase,
|
|
|
|
|
$context,
|
|
|
|
|
$intersection_type,
|
2019-05-28 06:32:17 +02:00
|
|
|
|
$lhs_type_part,
|
2019-05-24 18:48:37 +02:00
|
|
|
|
$lhs_var_id,
|
|
|
|
|
$intersection_return_type,
|
|
|
|
|
$returns_by_ref,
|
|
|
|
|
$has_mock,
|
|
|
|
|
$has_valid_method_call_type,
|
|
|
|
|
$has_mixed_method_call,
|
|
|
|
|
$invalid_method_call_types,
|
|
|
|
|
$all_intersection_existent_method_ids,
|
|
|
|
|
$i_non_existent_class_method_ids,
|
|
|
|
|
$i_non_existent_interface_method_ids,
|
|
|
|
|
$check_visibility
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($intersection_return_type) {
|
|
|
|
|
if (!$all_intersection_return_type || $all_intersection_return_type->isMixed()) {
|
|
|
|
|
$all_intersection_return_type = $intersection_return_type;
|
|
|
|
|
} else {
|
|
|
|
|
$all_intersection_return_type = Type::intersectUnionTypes(
|
|
|
|
|
$all_intersection_return_type,
|
|
|
|
|
$intersection_return_type
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2019-04-20 23:49:49 +02:00
|
|
|
|
if (!$context->ignore_variable_method) {
|
2019-04-27 23:38:24 +02:00
|
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
|
strtolower($fq_class_name) . '::',
|
|
|
|
|
$context->calling_method_id ?: $statements_analyzer->getFileName()
|
|
|
|
|
);
|
2019-04-20 23:49:49 +02:00
|
|
|
|
}
|
2019-04-19 17:45:08 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$method_name_lc = strtolower($stmt->name->name);
|
|
|
|
|
|
|
|
|
|
$method_id = $fq_class_name . '::' . $method_name_lc;
|
|
|
|
|
|
|
|
|
|
$intersection_method_id = $intersection_types
|
|
|
|
|
? '(' . $lhs_type_part . ')' . '::' . $stmt->name->name
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
$args = $stmt->args;
|
|
|
|
|
|
2019-04-16 22:07:48 +02:00
|
|
|
|
if (!$codebase->methods->methodExists(
|
|
|
|
|
$method_id,
|
|
|
|
|
$context->calling_method_id,
|
|
|
|
|
$codebase->collect_references ? new CodeLocation($source, $stmt->name) : null,
|
|
|
|
|
null,
|
|
|
|
|
$statements_analyzer->getFilePath()
|
|
|
|
|
)
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|| !MethodAnalyzer::isMethodVisible(
|
|
|
|
|
$method_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$context,
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$statements_analyzer->getSource()
|
|
|
|
|
)
|
|
|
|
|
) {
|
2019-03-17 19:14:03 +01:00
|
|
|
|
$interface_has_method = false;
|
|
|
|
|
|
|
|
|
|
if ($class_storage->abstract && $class_storage->class_implements) {
|
|
|
|
|
foreach ($class_storage->class_implements as $interface_fqcln) {
|
|
|
|
|
$interface_storage = $codebase->classlike_storage_provider->get($interface_fqcln);
|
|
|
|
|
|
|
|
|
|
if (isset($interface_storage->methods[$method_name_lc])) {
|
|
|
|
|
$interface_has_method = true;
|
|
|
|
|
$fq_class_name = $interface_fqcln;
|
|
|
|
|
$method_id = $fq_class_name . '::' . $method_name_lc;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$interface_has_method
|
|
|
|
|
&& $codebase->methods->methodExists(
|
|
|
|
|
$fq_class_name . '::__call',
|
|
|
|
|
$context->calling_method_id
|
|
|
|
|
)
|
|
|
|
|
) {
|
2019-01-12 15:13:54 +01: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(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$method_id,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$generic_params = [];
|
2018-06-19 22:14:51 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (self::checkFunctionLikeArgumentsMatch(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
null,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$pseudo_method_storage,
|
|
|
|
|
null,
|
|
|
|
|
$generic_params,
|
|
|
|
|
new CodeLocation($source, $stmt),
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
2018-04-22 04:44:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($pseudo_method_storage->return_type) {
|
|
|
|
|
$return_type_candidate = clone $pseudo_method_storage->return_type;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-30 15:46:23 +01:00
|
|
|
|
$return_type_candidate = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
|
|
|
|
$return_type_candidate,
|
|
|
|
|
$fq_class_name,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$fq_class_name,
|
|
|
|
|
$class_storage->parent_class
|
2019-01-30 15:46:23 +01:00
|
|
|
|
);
|
|
|
|
|
|
2019-05-24 18:48:37 +02:00
|
|
|
|
if ($all_intersection_return_type) {
|
|
|
|
|
$return_type_candidate = Type::intersectUnionTypes(
|
|
|
|
|
$all_intersection_return_type,
|
|
|
|
|
$return_type_candidate
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($class_storage->sealed_methods) {
|
2019-01-13 19:07:53 +01:00
|
|
|
|
$non_existent_class_method_ids[] = $method_id;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-07 04:58:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$has_valid_method_call_type = true;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$existent_method_ids[] = $method_id;
|
|
|
|
|
|
2019-01-12 15:13:54 +01: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';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$source_source = $statements_analyzer->getSource();
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var \Psalm\Internal\Analyzer\ClassLikeAnalyzer|null
|
|
|
|
|
*/
|
|
|
|
|
$classlike_source = $source_source->getSource();
|
|
|
|
|
$classlike_source_fqcln = $classlike_source ? $classlike_source->getFQCLN() : null;
|
|
|
|
|
|
|
|
|
|
if ($lhs_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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 19:07:53 +01:00
|
|
|
|
$is_interface = false;
|
|
|
|
|
|
|
|
|
|
if ($codebase->interfaceExists($fq_class_name)) {
|
|
|
|
|
$is_interface = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$source_method_id = $source instanceof FunctionLikeAnalyzer
|
|
|
|
|
? $source->getMethodId()
|
|
|
|
|
: null;
|
2018-05-12 06:28:21 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (!$codebase->methods->methodExists(
|
|
|
|
|
$method_id,
|
|
|
|
|
$context->calling_method_id,
|
|
|
|
|
$method_id !== $source_method_id ? new CodeLocation($source, $stmt->name) : null
|
2019-09-04 15:42:12 +02:00
|
|
|
|
)
|
|
|
|
|
|| ($config->use_phpdoc_method_without_magic_or_parent
|
|
|
|
|
&& isset($class_storage->pseudo_methods[$method_name_lc]))
|
|
|
|
|
) {
|
2019-05-27 05:35:03 +02:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-02-01 05:27:25 +01:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
if (($is_interface || $config->use_phpdoc_method_without_magic_or_parent)
|
|
|
|
|
&& isset($class_storage->pseudo_methods[$method_name_lc])
|
|
|
|
|
) {
|
|
|
|
|
$has_valid_method_call_type = true;
|
|
|
|
|
$existent_method_ids[] = $method_id;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
$pseudo_method_storage = $class_storage->pseudo_methods[$method_name_lc];
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$method_id,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-10 01:37:09 +01:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
$generic_params = [];
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
if (self::checkFunctionLikeArgumentsMatch(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
null,
|
|
|
|
|
$pseudo_method_storage->params,
|
|
|
|
|
$pseudo_method_storage,
|
|
|
|
|
null,
|
|
|
|
|
$generic_params,
|
|
|
|
|
new CodeLocation($source, $stmt->name),
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
if ($pseudo_method_storage->return_type) {
|
|
|
|
|
$return_type_candidate = clone $pseudo_method_storage->return_type;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
if ($all_intersection_return_type) {
|
|
|
|
|
$return_type_candidate = Type::intersectUnionTypes(
|
|
|
|
|
$all_intersection_return_type,
|
|
|
|
|
$return_type_candidate
|
|
|
|
|
);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 05:35:03 +02:00
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
|
|
|
|
}
|
2018-05-09 04:01:05 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-27 05:35:03 +02:00
|
|
|
|
|
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
|
|
|
|
|
|
return;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-08-15 15:51:40 +02:00
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$args,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 18:48:37 +02:00
|
|
|
|
if ($all_intersection_return_type && $all_intersection_existent_method_ids) {
|
|
|
|
|
$existent_method_ids = array_merge($existent_method_ids, $all_intersection_existent_method_ids);
|
|
|
|
|
|
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $all_intersection_return_type;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($all_intersection_return_type, $return_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 19:07:53 +01:00
|
|
|
|
if ($is_interface) {
|
|
|
|
|
$non_existent_interface_method_ids[] = $intersection_method_id ?: $method_id;
|
|
|
|
|
} else {
|
|
|
|
|
$non_existent_class_method_ids[] = $intersection_method_id ?: $method_id;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
|
if ($codebase->store_node_types
|
|
|
|
|
&& $method_id
|
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
) {
|
2019-06-23 14:08:00 +02:00
|
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
|
$stmt->name,
|
|
|
|
|
$method_id . '()'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($context->collect_initializations && $context->calling_method_id) {
|
|
|
|
|
list($calling_method_class) = explode('::', $context->calling_method_id);
|
2019-04-16 22:07:48 +02:00
|
|
|
|
$codebase->file_reference_provider->addMethodReferenceToClassMember(
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$calling_method_class . '::__construct',
|
|
|
|
|
strtolower($method_id)
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$existent_method_ids[] = $method_id;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
&& ($context->collect_initializations || $context->collect_mutations)
|
|
|
|
|
&& $stmt->var->name === 'this'
|
|
|
|
|
&& $source instanceof FunctionLikeAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::collectSpecialInformation($source, $stmt->name->name, $context);
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$fq_class_name = $codebase->classlikes->getUnAliasedName($fq_class_name);
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
2019-03-29 03:47:17 +01:00
|
|
|
|
$parent_source = $statements_analyzer->getSource();
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
|
|
|
|
$class_template_params = self::getClassTemplateParams(
|
|
|
|
|
$codebase,
|
2019-03-29 03:47:17 +01:00
|
|
|
|
$codebase->methods->getClassLikeStorageForMethod($method_id),
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$fq_class_name,
|
2019-01-24 22:09:04 +01:00
|
|
|
|
$method_name_lc,
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$lhs_type_part,
|
|
|
|
|
$lhs_var_id
|
|
|
|
|
);
|
|
|
|
|
|
2019-03-29 03:47:17 +01:00
|
|
|
|
if ($lhs_var_id === '$this' && $parent_source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer) {
|
|
|
|
|
$grandparent_source = $parent_source->getSource();
|
|
|
|
|
|
|
|
|
|
if ($grandparent_source instanceof \Psalm\Internal\Analyzer\TraitAnalyzer) {
|
|
|
|
|
$fq_trait_name = $grandparent_source->getFQCLN();
|
|
|
|
|
|
|
|
|
|
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name);
|
|
|
|
|
|
|
|
|
|
if (isset($trait_storage->methods[$method_name_lc])) {
|
|
|
|
|
$trait_method_id = $fq_trait_name . '::' . $method_name_lc;
|
|
|
|
|
|
|
|
|
|
$class_template_params = self::getClassTemplateParams(
|
|
|
|
|
$codebase,
|
|
|
|
|
$codebase->methods->getClassLikeStorageForMethod($trait_method_id),
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
$method_name_lc,
|
|
|
|
|
$lhs_type_part,
|
|
|
|
|
$lhs_var_id
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 21:54:33 +02:00
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
) {
|
|
|
|
|
ArgumentMapPopulator::recordArgumentPositions(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
|
|
|
|
$codebase,
|
|
|
|
|
$method_id
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
|
$method_id,
|
|
|
|
|
$args,
|
|
|
|
|
$class_template_params,
|
|
|
|
|
$context,
|
|
|
|
|
new CodeLocation($source, $stmt->name),
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (strtolower($stmt->name->name)) {
|
|
|
|
|
case '__tostring':
|
|
|
|
|
$return_type = Type::getString();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$call_map_id = strtolower(
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$declaring_method_id ?: $method_id
|
2019-01-12 15:13:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($method_name_lc === '__tostring') {
|
|
|
|
|
$return_type_candidate = Type::getString();
|
|
|
|
|
} else {
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$return_type_candidate = null;
|
2018-04-28 19:46:01 +02:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if ($codebase->methods->return_type_provider->has($fq_class_name)) {
|
|
|
|
|
$return_type_candidate = $codebase->methods->return_type_provider->getReturnType(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
$stmt->name->name,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
$context,
|
2019-03-07 20:56:18 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->name),
|
|
|
|
|
$lhs_type_part instanceof TGenericObject ? $lhs_type_part->type_params : null
|
2019-02-27 23:06:44 +01:00
|
|
|
|
);
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
2018-02-12 02:56:34 +01:00
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
|
if (!$return_type_candidate && $declaring_method_id && $declaring_method_id !== $method_id) {
|
|
|
|
|
list($declaring_fq_class_name, $declaring_method_name) = explode('::', $declaring_method_id);
|
|
|
|
|
|
|
|
|
|
if ($codebase->methods->return_type_provider->has($declaring_fq_class_name)) {
|
|
|
|
|
$return_type_candidate = $codebase->methods->return_type_provider->getReturnType(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$declaring_fq_class_name,
|
|
|
|
|
$declaring_method_name,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
$context,
|
2019-03-12 17:58:04 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->name),
|
|
|
|
|
$lhs_type_part instanceof TGenericObject ? $lhs_type_part->type_params : null,
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
$stmt->name->name
|
2019-03-01 14:57:10 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-29 03:47:17 +01:00
|
|
|
|
$class_storage = $codebase->methods->getClassLikeStorageForMethod($method_id);
|
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if (!$return_type_candidate) {
|
|
|
|
|
if ($call_map_id && CallMap::inCallMap($call_map_id)) {
|
|
|
|
|
if (($class_template_params || $class_storage->stubbed)
|
|
|
|
|
&& 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;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if ($class_template_params) {
|
|
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
|
|
|
|
$class_template_params,
|
|
|
|
|
$codebase
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-15 22:10:48 +02:00
|
|
|
|
$callmap_callables = CallMap::getCallablesFromCallMap($call_map_id);
|
|
|
|
|
|
|
|
|
|
if (!$callmap_callables || $callmap_callables[0]->return_type === null) {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get here');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return_type_candidate = $callmap_callables[0]->return_type;
|
2019-04-11 20:53:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($return_type_candidate->isFalsable()) {
|
|
|
|
|
$return_type_candidate->ignore_falsable_issues = true;
|
2019-02-27 23:06:44 +01:00
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$return_type_candidate = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
|
|
|
|
$return_type_candidate,
|
|
|
|
|
$fq_class_name,
|
2019-05-28 06:32:17 +02:00
|
|
|
|
$static_type,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$class_storage->parent_class
|
2019-02-27 23:06:44 +01:00
|
|
|
|
);
|
2019-09-03 19:49:15 +02:00
|
|
|
|
|
|
|
|
|
if ($fq_class_name === 'DateTimeImmutable'
|
|
|
|
|
&& !$context->inside_conditional
|
|
|
|
|
&& !$context->inside_unset
|
|
|
|
|
) {
|
|
|
|
|
if (!$context->inside_assignment && !$context->inside_call) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new \Psalm\Issue\UnusedMethodCall(
|
|
|
|
|
'The call to ' . $method_id . ' is not used',
|
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->name),
|
|
|
|
|
$method_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
|
|
|
|
$stmt->pure = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 23:06:44 +01:00
|
|
|
|
} else {
|
|
|
|
|
$name_code_location = new CodeLocation($source, $stmt->name);
|
|
|
|
|
|
|
|
|
|
if ($check_visibility) {
|
|
|
|
|
if (MethodAnalyzer::checkMethodVisibility(
|
|
|
|
|
$method_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$context,
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
|
$name_code_location,
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if (MethodAnalyzer::checkMethodNotDeprecatedOrInternal(
|
|
|
|
|
$codebase,
|
|
|
|
|
$context,
|
|
|
|
|
$method_id,
|
|
|
|
|
$name_code_location,
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if (!self::checkMagicGetterOrSetterProperty(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
2019-04-18 16:41:46 +02:00
|
|
|
|
$context,
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$fq_class_name
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$self_fq_class_name = $fq_class_name;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$return_type_candidate = $codebase->methods->getMethodReturnType(
|
|
|
|
|
$method_id,
|
|
|
|
|
$self_fq_class_name,
|
|
|
|
|
$args
|
2019-01-12 15:13:54 +01:00
|
|
|
|
);
|
2018-02-12 02:56:34 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
|
$return_type_candidate = $stmt->inferredType;
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if ($return_type_candidate) {
|
|
|
|
|
$return_type_candidate = clone $return_type_candidate;
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if ($class_template_params) {
|
|
|
|
|
$return_type_candidate->replaceTemplateTypesWithArgTypes(
|
|
|
|
|
$class_template_params,
|
|
|
|
|
$codebase
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return_type_candidate = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
|
|
|
|
$return_type_candidate,
|
|
|
|
|
$self_fq_class_name,
|
2019-05-28 06:32:17 +02:00
|
|
|
|
$static_type,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$class_storage->parent_class
|
2019-02-27 23:06:44 +01:00
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-08-04 16:37:36 +02:00
|
|
|
|
$return_type_candidate->sources = [
|
2019-08-14 06:47:57 +02:00
|
|
|
|
new Source(strtolower($method_id), new CodeLocation($source, $stmt->name))
|
2019-08-04 16:37:36 +02:00
|
|
|
|
];
|
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$return_type_location = $codebase->methods->getMethodReturnTypeLocation(
|
|
|
|
|
$method_id,
|
|
|
|
|
$secondary_return_type_location
|
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if ($secondary_return_type_location) {
|
|
|
|
|
$return_type_location = $secondary_return_type_location;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
// 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_analyzer,
|
|
|
|
|
new CodeLocation($source, $stmt),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
|
|
|
|
$context->phantom_classes
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$returns_by_ref =
|
|
|
|
|
$returns_by_ref
|
|
|
|
|
|| $codebase->methods->getMethodReturnsByRef($method_id);
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$method_storage = $codebase->methods->getUserMethodStorage($method_id);
|
|
|
|
|
|
|
|
|
|
if ($method_storage) {
|
2019-08-31 15:07:00 +02:00
|
|
|
|
if (!$context->collect_mutations && !$context->collect_initializations) {
|
2019-08-31 15:49:32 +02:00
|
|
|
|
$method_pure_compatible = $method_storage->external_mutation_free
|
|
|
|
|
&& (!empty($stmt->var->inferredType->external_mutation_free)
|
|
|
|
|
|| isset($stmt->var->pure));
|
|
|
|
|
|
2019-09-01 04:03:37 +02:00
|
|
|
|
if ($context->pure
|
|
|
|
|
&& !$method_storage->mutation_free
|
|
|
|
|
&& !$method_pure_compatible
|
|
|
|
|
) {
|
2019-08-30 22:40:32 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
2019-08-31 15:07:00 +02:00
|
|
|
|
new ImpureMethodCall(
|
2019-09-01 04:03:37 +02:00
|
|
|
|
'Cannot call an mutation-free method ' . $method_id . ' from a pure context',
|
2019-08-31 15:07:00 +02:00
|
|
|
|
new CodeLocation($source, $stmt->name)
|
2019-08-30 22:40:32 +02:00
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
2019-08-31 15:49:32 +02:00
|
|
|
|
} elseif ($context->mutation_free
|
|
|
|
|
&& !$method_storage->mutation_free
|
|
|
|
|
&& !$method_pure_compatible
|
|
|
|
|
) {
|
2019-08-31 15:07:00 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new ImpureMethodCall(
|
|
|
|
|
'Cannot call an possibly-mutating method '
|
|
|
|
|
. $method_id . ' from a mutation-free context',
|
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} elseif ($context->external_mutation_free
|
|
|
|
|
&& !$method_storage->mutation_free
|
|
|
|
|
&& $fq_class_name !== $context->self
|
2019-08-31 15:49:32 +02:00
|
|
|
|
&& !$method_pure_compatible
|
2019-08-31 15:07:00 +02:00
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new ImpureMethodCall(
|
|
|
|
|
'Cannot call an possibly-mutating method '
|
|
|
|
|
. $method_id . ' from a mutation-free context',
|
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} elseif (($method_storage->mutation_free
|
2019-08-31 16:02:11 +02:00
|
|
|
|
|| ($method_storage->external_mutation_free
|
|
|
|
|
&& (isset($stmt->var->external_mutation_free) || isset($stmt->var->pure))))
|
2019-08-31 15:07:00 +02:00
|
|
|
|
&& $codebase->find_unused_variables
|
|
|
|
|
&& !$context->inside_conditional
|
|
|
|
|
&& !$context->inside_unset
|
|
|
|
|
) {
|
|
|
|
|
if (!$context->inside_assignment && !$context->inside_call) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new \Psalm\Issue\UnusedMethodCall(
|
|
|
|
|
'The call to ' . $method_id . ' is not used',
|
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->name),
|
|
|
|
|
$method_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
|
|
|
|
$stmt->pure = true;
|
|
|
|
|
}
|
2019-08-30 22:40:32 +02:00
|
|
|
|
}
|
2019-08-31 20:54:59 +02:00
|
|
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call
|
|
|
|
|
&& !$method_storage->mutation_free
|
|
|
|
|
&& !$method_pure_compatible
|
|
|
|
|
) {
|
|
|
|
|
$context->removeAllObjectVars();
|
2019-09-08 00:34:18 +02:00
|
|
|
|
} elseif ($method_storage->this_property_mutations) {
|
|
|
|
|
foreach ($method_storage->this_property_mutations as $name => $_) {
|
|
|
|
|
$context->remove($lhs_var_id . '->' . $name);
|
|
|
|
|
}
|
2019-08-31 20:54:59 +02:00
|
|
|
|
}
|
2019-07-18 07:31:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
if ($method_storage->assertions) {
|
|
|
|
|
self::applyAssertionsToContext(
|
|
|
|
|
$stmt->name,
|
2019-09-06 03:00:02 +02:00
|
|
|
|
ExpressionAnalyzer::getArrayVarId($stmt->var, null, $statements_analyzer),
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$method_storage->assertions,
|
|
|
|
|
$args,
|
|
|
|
|
$class_template_params ?: [],
|
|
|
|
|
$context,
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($method_storage->if_true_assertions) {
|
|
|
|
|
$stmt->ifTrueAssertions = array_map(
|
|
|
|
|
function (Assertion $assertion) use ($class_template_params) : Assertion {
|
|
|
|
|
return $assertion->getUntemplatedCopy($class_template_params ?: []);
|
|
|
|
|
},
|
|
|
|
|
$method_storage->if_true_assertions
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($method_storage->if_false_assertions) {
|
|
|
|
|
$stmt->ifFalseAssertions = array_map(
|
|
|
|
|
function (Assertion $assertion) use ($class_template_params) : Assertion {
|
|
|
|
|
return $assertion->getUntemplatedCopy($class_template_params ?: []);
|
|
|
|
|
},
|
|
|
|
|
$method_storage->if_false_assertions
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-08 20:17:28 +02:00
|
|
|
|
|
|
|
|
|
if ($return_type_candidate && $codebase->taint && $method_id) {
|
|
|
|
|
if ($method_storage && $method_storage->pure) {
|
|
|
|
|
$code_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
|
|
|
|
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$method_source = new Source(
|
2019-08-08 20:17:28 +02:00
|
|
|
|
strtolower(
|
|
|
|
|
$method_id
|
|
|
|
|
. '-' . $code_location->file_name
|
|
|
|
|
. ':' . $code_location->raw_file_start
|
|
|
|
|
),
|
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2019-08-14 06:47:57 +02:00
|
|
|
|
$method_source = new Source(
|
2019-08-08 20:17:28 +02:00
|
|
|
|
strtolower($method_id),
|
|
|
|
|
new CodeLocation($source, $stmt->name)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 05:16:05 +02:00
|
|
|
|
if ($tainted_source = $codebase->taint->hasPreviousSource($method_source)) {
|
|
|
|
|
$return_type_candidate->tainted = $tainted_source->taint;
|
2019-08-08 20:17:28 +02:00
|
|
|
|
$return_type_candidate->sources = [$method_source];
|
2019-08-13 05:16:05 +02:00
|
|
|
|
$method_source->taint = $tainted_source->taint;
|
2019-08-08 20:17:28 +02: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
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (!$args && $lhs_var_id) {
|
|
|
|
|
if ($config->memoize_method_calls) {
|
|
|
|
|
$method_var_id = $lhs_var_id . '->' . $method_name_lc . '()';
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
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-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-02 18:02:32 +02:00
|
|
|
|
|
|
|
|
|
if ($codebase->methods_to_rename) {
|
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
|
|
foreach ($codebase->methods_to_rename as $original_method_id => $new_method_name) {
|
|
|
|
|
if ($declaring_method_id && strtolower($declaring_method_id) === $original_method_id) {
|
|
|
|
|
$file_manipulations = [
|
|
|
|
|
new \Psalm\FileManipulation(
|
|
|
|
|
(int) $stmt->name->getAttribute('startFilePos'),
|
|
|
|
|
(int) $stmt->name->getAttribute('endFilePos') + 1,
|
|
|
|
|
$new_method_name
|
|
|
|
|
)
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
|
$file_manipulations
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($config->after_method_checks) {
|
|
|
|
|
$file_manipulations = [];
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
|
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
|
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
2019-01-12 15:13:54 +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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
|
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($return_type_candidate) {
|
2019-05-24 18:48:37 +02:00
|
|
|
|
if ($all_intersection_return_type) {
|
|
|
|
|
$return_type_candidate = Type::intersectUnionTypes(
|
|
|
|
|
$all_intersection_return_type,
|
|
|
|
|
$return_type_candidate
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
} elseif ($all_intersection_return_type) {
|
|
|
|
|
if (!$return_type) {
|
|
|
|
|
$return_type = $all_intersection_return_type;
|
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::combineUnionTypes($all_intersection_return_type, $return_type);
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
} else {
|
|
|
|
|
$return_type = Type::getMixed();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
/**
|
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,
|
|
|
|
|
string $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
|
|
|
|
|
) {
|
|
|
|
|
$calling_class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
2019-07-05 04:26:40 +02:00
|
|
|
|
$non_trait_class_storage = $class_storage->is_trait
|
|
|
|
|
? $calling_class_storage
|
|
|
|
|
: $class_storage;
|
|
|
|
|
|
2019-01-24 22:09:04 +01:00
|
|
|
|
$template_types = $class_storage->template_types;
|
|
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
|
if ($calling_class_storage->template_type_extends
|
|
|
|
|
&& $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) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-01-24 22:09:04 +01:00
|
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
|
if ($overridden_storage->return_type->isNull()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-01-24 22:09:04 +01:00
|
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
|
list($fq_overridden_class) = explode('::', $overridden_method_id);
|
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-03-22 21:29:30 +01:00
|
|
|
|
if (!$template_types) {
|
2019-01-24 22:09:04 +01:00
|
|
|
|
$template_types = $overridden_class_storage->template_types;
|
2019-03-22 21:29:30 +01:00
|
|
|
|
} elseif ($overridden_class_storage->template_types) {
|
|
|
|
|
$template_types = array_merge($overridden_class_storage->template_types, $template_types);
|
2019-01-24 22:09:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 21:29:30 +01:00
|
|
|
|
$class_storage = $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 = [];
|
|
|
|
|
$e = $calling_class_storage->template_type_extends;
|
|
|
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
2019-02-22 05:01:34 +01:00
|
|
|
|
if ($calling_class_storage->template_types && $class_storage === $calling_class_storage) {
|
2019-01-12 15:13:54 +01:00
|
|
|
|
$i = 0;
|
|
|
|
|
foreach ($calling_class_storage->template_types as $type_name => $_) {
|
|
|
|
|
if (isset($lhs_type_part->type_params[$i])) {
|
2019-03-22 20:59:10 +01:00
|
|
|
|
$class_template_params[$type_name][$calling_class_storage->name] = [
|
2019-03-22 23:02:33 +01:00
|
|
|
|
$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
|
|
|
|
|
2019-01-12 15:13:54 +01:00
|
|
|
|
if ($class_storage !== $calling_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;
|
|
|
|
|
|
|
|
|
|
foreach ($input_type_extends->getTypes() as $type_extends_atomic) {
|
|
|
|
|
if ($type_extends_atomic instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
|
if (isset($calling_class_storage->template_types[$type_extends_atomic->param_name])) {
|
|
|
|
|
$mapped_offset = array_search(
|
|
|
|
|
$type_extends_atomic->param_name,
|
|
|
|
|
array_keys($calling_class_storage->template_types)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($type_extends_atomic->defining_class
|
|
|
|
|
&& isset(
|
|
|
|
|
$calling_class_storage
|
|
|
|
|
->template_type_extends
|
2019-06-25 05:31:06 +02:00
|
|
|
|
[$type_extends_atomic->defining_class]
|
2019-03-16 16:15:25 +01:00
|
|
|
|
[$type_extends_atomic->param_name]
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$mapped_offset = array_search(
|
|
|
|
|
$type_extends_atomic->param_name,
|
|
|
|
|
array_keys($calling_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
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
|
$class_template_params[$type_name][$class_storage->name] = [
|
2019-03-22 23:02:33 +01:00
|
|
|
|
$output_type_extends ?: Type::getMixed()
|
2019-03-16 16:15:25 +01:00
|
|
|
|
];
|
2019-01-12 15:13:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($class_template_params[$type_name])) {
|
2019-03-22 23:02:33 +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++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-03-22 20:59:10 +01:00
|
|
|
|
foreach ($template_types as $type_name => $type_map) {
|
2019-03-23 00:11:53 +01:00
|
|
|
|
foreach ($type_map as list($type)) {
|
2019-03-22 20:59:10 +01:00
|
|
|
|
if ($class_storage !== $calling_class_storage
|
2019-06-25 05:31:06 +02:00
|
|
|
|
&& isset($e[$class_storage->name][$type_name])
|
2019-03-22 20:59:10 +01:00
|
|
|
|
) {
|
2019-06-25 05:31:06 +02:00
|
|
|
|
$input_type_extends = $e[$class_storage->name][$type_name];
|
2019-03-22 20:59:10 +01:00
|
|
|
|
|
|
|
|
|
$output_type_extends = null;
|
|
|
|
|
|
|
|
|
|
foreach ($input_type_extends->getTypes() as $type_extends_atomic) {
|
|
|
|
|
if ($type_extends_atomic instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
|
$output_type_extends = $type_extends_atomic->as;
|
|
|
|
|
} else {
|
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
|
$type_extends_atomic->as,
|
|
|
|
|
$output_type_extends
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-03-16 16:15:25 +01:00
|
|
|
|
} else {
|
2019-03-22 20:59:10 +01:00
|
|
|
|
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-03-16 16:15:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
|
$class_template_params[$type_name][$class_storage->name] = [
|
2019-03-22 23:02:33 +01:00
|
|
|
|
$output_type_extends ?: Type::getMixed()
|
2019-03-22 20:59:10 +01:00
|
|
|
|
];
|
|
|
|
|
}
|
2019-01-12 15:13:54 +01:00
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
|
if ($lhs_var_id !== '$this') {
|
|
|
|
|
if (!isset($class_template_params[$type_name])) {
|
2019-03-22 23:02:33 +01:00
|
|
|
|
$class_template_params[$type_name][$class_storage->name] = [$type];
|
2019-03-22 20:59:10 +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
|
|
|
|
|
|
|
|
|
return $class_template_params;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
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,
|
2019-04-18 16:41:46 +02:00
|
|
|
|
Context $context,
|
2018-02-10 01:37:09 +01:00
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-19 17:45:08 +02:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
2018-02-10 01:37:09 +01:00
|
|
|
|
$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;
|
2019-04-19 17:45:08 +02:00
|
|
|
|
|
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
|
|
|
|
|
2019-04-18 16:41:46 +02:00
|
|
|
|
$codebase->properties->propertyExists(
|
|
|
|
|
$property_id,
|
|
|
|
|
$method_name === '__get',
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$context,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
);
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
) {
|
2019-03-06 00:27:25 +01:00
|
|
|
|
// fall through
|
2018-02-10 01:37:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
new Type\Atomic\TNamedObject($fq_class_name),
|
|
|
|
|
$class_storage->parent_class
|
2018-02-22 01:34:21 +01:00
|
|
|
|
);
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results = new \Psalm\Internal\Analyzer\TypeComparisonResult();
|
|
|
|
|
|
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,
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_results
|
2018-02-22 01:34:21 +01:00
|
|
|
|
);
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if ($union_comparison_results->type_coerced) {
|
|
|
|
|
if ($union_comparison_results->type_coerced_from_mixed) {
|
2018-02-22 01:34:21 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new MixedPropertyTypeCoercion(
|
2018-02-22 01:34:21 +01:00
|
|
|
|
$prop_name . ' expects \'' . $pseudo_set_type . '\', '
|
|
|
|
|
. ' parent type `' . $second_arg_type . '` provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
|
$property_id
|
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(
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new PropertyTypeCoercion(
|
2018-02-22 01:34:21 +01:00
|
|
|
|
$prop_name . ' expects \'' . $pseudo_set_type . '\', '
|
|
|
|
|
. ' parent type `' . $second_arg_type . '` provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
|
$property_id
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
|
if (!$type_match_found && !$union_comparison_results->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
|
|
|
|
)) {
|
2019-03-06 00:27:25 +01:00
|
|
|
|
// fall through
|
2018-05-03 19:56:30 +02:00
|
|
|
|
}
|
|
|
|
|
} 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
|
|
|
|
)) {
|
2019-03-06 00:27:25 +01:00
|
|
|
|
// fall through
|
2018-05-03 19:56:30 +02:00
|
|
|
|
}
|
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
|
|
|
|
)
|
|
|
|
|
) {
|
2019-03-06 00:27:25 +01:00
|
|
|
|
// fall through
|
2018-02-10 01:37:09 +01:00
|
|
|
|
}
|
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
|
|
|
|
}
|