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\ClassAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
2019-05-14 23:30:16 +02:00
|
|
|
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-09-21 05:59:52 +02:00
|
|
|
use Psalm\Internal\ControlFlow\ControlFlowNode;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\AbstractInstantiation;
|
|
|
|
use Psalm\Issue\DeprecatedClass;
|
2019-07-18 07:31:48 +02:00
|
|
|
use Psalm\Issue\ImpureMethodCall;
|
2018-02-18 00:53:02 +01:00
|
|
|
use Psalm\Issue\InterfaceInstantiation;
|
2018-12-02 00:37:49 +01:00
|
|
|
use Psalm\Issue\InternalClass;
|
2018-03-06 18:19:50 +01:00
|
|
|
use Psalm\Issue\InvalidStringClass;
|
2019-01-02 12:58:49 +01:00
|
|
|
use Psalm\Issue\MixedMethodCall;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\TooManyArguments;
|
2020-08-06 01:39:27 +02:00
|
|
|
use Psalm\Issue\UnsafeInstantiation;
|
2018-03-06 18:19:50 +01:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function in_array;
|
|
|
|
use function strtolower;
|
|
|
|
use function implode;
|
|
|
|
use function array_values;
|
|
|
|
use function is_string;
|
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 NewAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
{
|
|
|
|
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\New_ $stmt,
|
|
|
|
Context $context
|
2020-05-18 21:13:27 +02:00
|
|
|
) : bool {
|
2018-01-29 00:29:38 +01:00
|
|
|
$fq_class_name = null;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
$config = $codebase->config;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-04 18:28:00 +01:00
|
|
|
$can_extend = false;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-03-24 23:00:20 +01:00
|
|
|
$from_static = false;
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
if (!in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$aliases = $statements_analyzer->getAliases();
|
2018-10-27 05:04:38 +02:00
|
|
|
|
2020-03-26 17:35:27 +01:00
|
|
|
if ($context->calling_method_id
|
2018-10-27 05:04:38 +02:00
|
|
|
&& !$stmt->class instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
) {
|
2019-04-16 22:07:48 +02:00
|
|
|
$codebase->file_reference_provider->addMethodReferenceToClassMember(
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
'use:' . $stmt->class->parts[0] . ':' . \md5($statements_analyzer->getFilePath())
|
2018-10-27 05:04:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->class,
|
2018-10-27 05:04:38 +02:00
|
|
|
$aliases
|
2018-01-29 00:29:38 +01:00
|
|
|
);
|
|
|
|
|
2018-11-29 06:05:56 +01:00
|
|
|
$fq_class_name = $codebase->classlikes->getUnAliasedName($fq_class_name);
|
2020-02-15 02:54:26 +01:00
|
|
|
} elseif ($context->self !== null) {
|
2018-01-29 00:29:38 +01:00
|
|
|
switch ($stmt->class->parts[0]) {
|
|
|
|
case 'self':
|
2020-02-15 02:54:26 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($context->self);
|
|
|
|
$fq_class_name = $class_storage->name;
|
2018-01-29 00:29:38 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'parent':
|
|
|
|
$fq_class_name = $context->parent;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'static':
|
|
|
|
// @todo maybe we can do better here
|
2020-02-15 02:54:26 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($context->self);
|
|
|
|
$fq_class_name = $class_storage->name;
|
2020-08-18 17:25:11 +02:00
|
|
|
|
|
|
|
if (!$class_storage->final) {
|
|
|
|
$can_extend = true;
|
|
|
|
$from_static = true;
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& $fq_class_name
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
2018-10-26 22:17:15 +02:00
|
|
|
$codebase->analyzer->addNodeReference(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt->class,
|
2019-06-21 23:10:35 +02:00
|
|
|
$codebase->classlikes->classExists($fq_class_name)
|
|
|
|
? $fq_class_name
|
|
|
|
: '*' . implode('\\', $stmt->class->parts)
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
} elseif ($stmt->class instanceof PhpParser\Node\Stmt\Class_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->analyze([$stmt->class], $context);
|
|
|
|
$fq_class_name = ClassAnalyzer::getAnonymousClassName($stmt->class, $statements_analyzer->getFilePath());
|
2018-01-29 00:29:38 +01:00
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context);
|
2018-01-29 18:22:50 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_class_type = $statements_analyzer->node_data->getType($stmt->class)) {
|
|
|
|
$has_single_class = $stmt_class_type->isSingleStringLiteral();
|
2019-01-02 12:58:49 +01:00
|
|
|
|
|
|
|
if ($has_single_class) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$fq_class_name = $stmt_class_type->getSingleStringLiteral()->value;
|
2019-01-02 12:58:49 +01:00
|
|
|
} else {
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
null,
|
|
|
|
$stmt->args,
|
2019-11-04 03:27:40 +01:00
|
|
|
null,
|
2019-01-02 12:58:49 +01:00
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 06:27:53 +02:00
|
|
|
$new_type = null;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_class_type->getAtomicTypes() as $lhs_type_part) {
|
2019-02-22 03:40:06 +01:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TTemplateParamClass) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer->node_data->getType($stmt)) {
|
2019-02-22 03:40:06 +01:00
|
|
|
$new_type_part = new Type\Atomic\TTemplateParam(
|
2018-12-18 05:29:27 +01:00
|
|
|
$lhs_type_part->param_name,
|
2019-01-20 04:45:58 +01:00
|
|
|
$lhs_type_part->as_type
|
|
|
|
? new Type\Union([$lhs_type_part->as_type])
|
2020-08-06 16:18:55 +02:00
|
|
|
: Type::getObject(),
|
2019-06-25 15:06:23 +02:00
|
|
|
$lhs_type_part->defining_class
|
2018-12-18 05:29:27 +01:00
|
|
|
);
|
|
|
|
|
2020-08-06 16:18:55 +02:00
|
|
|
if (!$lhs_type_part->as_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedMethodCall(
|
|
|
|
'Cannot call constructor on an unknown class',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
if ($new_type) {
|
|
|
|
$new_type = Type::combineUnionTypes(
|
|
|
|
$new_type,
|
|
|
|
new Type\Union([$new_type_part])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$new_type = new Type\Union([$new_type_part]);
|
|
|
|
}
|
2020-08-06 16:18:55 +02:00
|
|
|
|
|
|
|
if ($lhs_type_part->as_type
|
|
|
|
&& $codebase->classlikes->classExists($lhs_type_part->as_type->value)
|
|
|
|
) {
|
|
|
|
$as_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
$lhs_type_part->as_type->value
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$as_storage->preserve_constructor_signature) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnsafeInstantiation(
|
|
|
|
'Cannot safely instantiate class ' . $lhs_type_part->as_type->value
|
2020-08-10 18:26:25 +02:00
|
|
|
. ' with "new $class_name" as'
|
2020-08-06 16:18:55 +02:00
|
|
|
. ' its constructor might change in child classes',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 05:29:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:42:01 +01:00
|
|
|
if ($lhs_type_part->as_type) {
|
|
|
|
$codebase->methods->methodExists(
|
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$lhs_type_part->as_type->value,
|
|
|
|
'__construct'
|
|
|
|
),
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2020-03-28 21:30:56 +01:00
|
|
|
$codebase->collect_locations
|
2020-03-12 16:42:01 +01:00
|
|
|
? new CodeLocation($statements_analyzer->getSource(), $stmt) : null,
|
|
|
|
$statements_analyzer,
|
|
|
|
$statements_analyzer->getFilePath()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-02 15:00:45 +01:00
|
|
|
|
2018-05-21 18:40:39 +02:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TLiteralClassString
|
|
|
|
|| $lhs_type_part instanceof Type\Atomic\TClassString
|
2019-12-07 20:03:20 +01:00
|
|
|
|| $lhs_type_part instanceof Type\Atomic\GetClassT
|
2018-05-21 18:40:39 +02:00
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer->node_data->getType($stmt)) {
|
2019-12-07 20:03:20 +01:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TClassString) {
|
2020-03-24 23:30:08 +01:00
|
|
|
$generated_type = $lhs_type_part->as_type
|
|
|
|
? clone $lhs_type_part->as_type
|
|
|
|
: new Type\Atomic\TObject();
|
2020-08-06 16:18:55 +02:00
|
|
|
|
|
|
|
if ($lhs_type_part->as_type
|
|
|
|
&& $codebase->classlikes->classExists($lhs_type_part->as_type->value)
|
|
|
|
) {
|
|
|
|
$as_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
$lhs_type_part->as_type->value
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$as_storage->preserve_constructor_signature) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnsafeInstantiation(
|
|
|
|
'Cannot safely instantiate class ' . $lhs_type_part->as_type->value
|
2020-08-10 18:26:25 +02:00
|
|
|
. ' with "new $class_name" as'
|
2020-08-06 16:18:55 +02:00
|
|
|
. ' its constructor might change in child classes',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 20:03:20 +01:00
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\GetClassT) {
|
2020-03-24 23:30:08 +01:00
|
|
|
$generated_type = new Type\Atomic\TObject();
|
2019-12-07 20:03:20 +01:00
|
|
|
|
2019-12-07 20:05:37 +01:00
|
|
|
if ($lhs_type_part->as_type->hasObjectType()
|
2019-12-07 20:03:20 +01:00
|
|
|
&& $lhs_type_part->as_type->isSingle()
|
|
|
|
) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($lhs_type_part->as_type->getAtomicTypes() as $typeof_type_atomic) {
|
2019-12-07 20:03:20 +01:00
|
|
|
if ($typeof_type_atomic instanceof Type\Atomic\TNamedObject) {
|
2020-03-24 23:30:08 +01:00
|
|
|
$generated_type = new Type\Atomic\TNamedObject(
|
|
|
|
$typeof_type_atomic->value
|
|
|
|
);
|
2019-12-07 20:03:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-24 23:30:08 +01:00
|
|
|
$generated_type = new Type\Atomic\TNamedObject(
|
|
|
|
$lhs_type_part->value
|
|
|
|
);
|
2019-12-07 20:03:20 +01:00
|
|
|
}
|
2018-05-21 18:40:39 +02:00
|
|
|
|
2019-01-04 18:28:00 +01:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TClassString) {
|
|
|
|
$can_extend = true;
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:30:08 +01:00
|
|
|
if ($generated_type instanceof Type\Atomic\TObject) {
|
2019-01-02 12:58:49 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedMethodCall(
|
|
|
|
'Cannot call constructor on an unknown class',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 06:27:53 +02:00
|
|
|
if ($new_type) {
|
|
|
|
$new_type = Type::combineUnionTypes(
|
|
|
|
$new_type,
|
2020-03-24 23:30:08 +01:00
|
|
|
new Type\Union([$generated_type])
|
2018-05-20 06:27:53 +02:00
|
|
|
);
|
|
|
|
} else {
|
2020-03-24 23:30:08 +01:00
|
|
|
$new_type = new Type\Union([$generated_type]);
|
2018-05-20 06:27:53 +02:00
|
|
|
}
|
2018-04-04 04:20:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:20:54 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TString) {
|
|
|
|
if ($config->allow_string_standin_for_class
|
|
|
|
&& !$lhs_type_part instanceof Type\Atomic\TNumericString
|
|
|
|
) {
|
2018-05-20 06:27:53 +02:00
|
|
|
// do nothing
|
|
|
|
} elseif (IssueBuffer::accepts(
|
2018-03-06 18:19:50 +01:00
|
|
|
new InvalidStringClass(
|
|
|
|
'String cannot be used as a class',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-03-06 18:19:50 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-06 18:19:50 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-05-20 06:27:53 +02:00
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\TMixed
|
2019-02-22 03:40:06 +01:00
|
|
|
|| $lhs_type_part instanceof Type\Atomic\TTemplateParam
|
2018-04-20 16:52:23 +02:00
|
|
|
) {
|
2019-01-02 15:00:45 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedMethodCall(
|
|
|
|
'Cannot call constructor on an unknown class',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-08-02 20:43:13 +02:00
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\TFalse
|
2019-11-25 17:44:54 +01:00
|
|
|
&& $stmt_class_type->ignore_falsable_issues
|
2018-08-02 20:43:13 +02:00
|
|
|
) {
|
|
|
|
// do nothing
|
2018-05-20 06:27:53 +02:00
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\TNull
|
2019-11-25 17:44:54 +01:00
|
|
|
&& $stmt_class_type->ignore_nullable_issues
|
2018-03-06 20:11:32 +01:00
|
|
|
) {
|
2018-05-20 06:27:53 +02:00
|
|
|
// do nothing
|
|
|
|
} elseif (IssueBuffer::accepts(
|
2018-03-06 18:19:50 +01:00
|
|
|
new UndefinedClass(
|
2018-03-06 17:20:54 +01:00
|
|
|
'Type ' . $lhs_type_part . ' cannot be called as a class',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-03-21 15:17:57 +01:00
|
|
|
(string)$lhs_type_part
|
2018-03-06 17:20:54 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-06 17:20:54 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-04-04 04:20:00 +02:00
|
|
|
|
2018-05-20 06:27:53 +02:00
|
|
|
if ($new_type) {
|
|
|
|
$new_type = Type::combineUnionTypes(
|
|
|
|
$new_type,
|
|
|
|
Type::getObject()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$new_type = Type::getObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 12:58:49 +01:00
|
|
|
if (!$has_single_class) {
|
|
|
|
if ($new_type) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $new_type);
|
2019-01-02 12:58:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
ArgumentsAnalyzer::analyze(
|
2020-05-03 04:13:59 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-03-06 17:20:54 +01:00
|
|
|
}
|
2019-01-02 12:58:49 +01:00
|
|
|
} else {
|
2020-05-19 04:57:00 +02:00
|
|
|
ArgumentsAnalyzer::analyze(
|
2020-05-03 04:13:59 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-03-06 17:20:54 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_class_name) {
|
2019-11-15 22:50:43 +01:00
|
|
|
if ($codebase->alter_code
|
|
|
|
&& $stmt->class instanceof PhpParser\Node\Name
|
|
|
|
&& !in_array($stmt->class->parts[0], ['parent', 'static'])
|
|
|
|
) {
|
2019-06-04 22:36:32 +02:00
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->class,
|
2019-06-01 16:32:49 +02:00
|
|
|
$fq_class_name,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id
|
2019-06-01 16:32:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-02 12:58:49 +01:00
|
|
|
if ($context->check_classes) {
|
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
2020-05-19 04:57:00 +02:00
|
|
|
ArgumentsAnalyzer::analyze(
|
2020-05-03 04:13:59 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2019-01-02 12:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
|
|
|
$statements_analyzer,
|
|
|
|
$fq_class_name,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
|
2020-02-11 22:39:33 +01:00
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2019-01-02 12:58:49 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
|
|
|
false
|
|
|
|
) === false) {
|
2020-05-19 04:57:00 +02:00
|
|
|
ArgumentsAnalyzer::analyze(
|
2020-05-03 04:13:59 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2019-01-02 12:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($codebase->interfaceExists($fq_class_name)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InterfaceInstantiation(
|
|
|
|
'Interface ' . $fq_class_name . ' cannot be instantiated',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->class)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2019-01-02 12:58:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:00:20 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
$result_atomic_type = new Type\Atomic\TAnonymousClassInstance($fq_class_name);
|
|
|
|
} else {
|
|
|
|
$result_atomic_type = new TNamedObject($fq_class_name);
|
|
|
|
$result_atomic_type->was_static = $from_static;
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:46:52 +01:00
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
2020-03-24 23:00:20 +01:00
|
|
|
new Type\Union([$result_atomic_type])
|
2019-12-31 14:46:52 +01:00
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
if (strtolower($fq_class_name) !== 'stdclass' &&
|
2018-02-18 00:53:02 +01:00
|
|
|
$codebase->classlikes->classExists($fq_class_name)
|
2018-01-29 00:29:38 +01:00
|
|
|
) {
|
2018-11-11 18:19:53 +01:00
|
|
|
$storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-08-06 01:39:27 +02:00
|
|
|
if ($from_static && !$storage->preserve_constructor_signature) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnsafeInstantiation(
|
2020-08-10 18:26:25 +02:00
|
|
|
'Cannot safely instantiate class ' . $fq_class_name . ' with "new static" as'
|
2020-08-06 15:02:24 +02:00
|
|
|
. ' its constructor might change in child classes',
|
2020-08-06 01:39:27 +02:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
2020-08-06 01:49:09 +02:00
|
|
|
// fall through
|
2020-08-06 01:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
// if we're not calling this constructor via new static()
|
2019-01-04 18:28:00 +01:00
|
|
|
if ($storage->abstract && !$can_extend) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new AbstractInstantiation(
|
|
|
|
'Unable to instantiate a abstract class ' . $fq_class_name,
|
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
|
|
|
)) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($storage->deprecated && strtolower($fq_class_name) !== strtolower((string) $context->self)) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedClass(
|
|
|
|
$fq_class_name . ' is marked deprecated',
|
2019-06-06 20:48:33 +02:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$fq_class_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
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 16:23:49 +02:00
|
|
|
|
2020-07-24 21:38:56 +02:00
|
|
|
if ($context->self
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& !NamespaceAnalyzer::isWithin($context->self, $storage->internal)
|
|
|
|
) {
|
2020-07-24 15:32:54 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalClass(
|
2020-07-24 21:38:56 +02:00
|
|
|
$fq_class_name . ' is internal to ' . $storage->internal
|
|
|
|
. ' but called from ' . $context->self,
|
2020-07-24 15:32:54 +02:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$fq_class_name
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2019-05-11 16:23:49 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-02 00:37:49 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, '__construct');
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
if ($codebase->methods->methodExists(
|
2020-02-15 02:54:26 +01:00
|
|
|
$method_id,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2020-03-28 21:30:56 +01:00
|
|
|
$codebase->collect_locations ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null,
|
2020-03-06 19:02:34 +01:00
|
|
|
$statements_analyzer,
|
2019-04-16 22:07:48 +02:00
|
|
|
$statements_analyzer->getFilePath()
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
2019-10-20 05:03:59 +02:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& !$context->collect_initializations
|
2019-07-01 21:54:33 +02:00
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
|
|
|
ArgumentMapPopulator::recordArgumentPositions(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$codebase,
|
2020-02-15 02:54:26 +01:00
|
|
|
(string) $method_id
|
2019-07-01 21:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult([], []);
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
2019-11-04 03:27:40 +01:00
|
|
|
$template_result,
|
2018-01-29 00:29:38 +01:00
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:57:00 +02:00
|
|
|
if (Method\MethodVisibilityAnalyzer::analyze(
|
2018-01-29 00:29:38 +01:00
|
|
|
$method_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-23 19:39:08 +02:00
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
2019-07-18 07:31:48 +02:00
|
|
|
|
2020-08-23 19:39:08 +02:00
|
|
|
if ($declaring_method_id) {
|
|
|
|
$method_storage = $codebase->methods->getStorage($declaring_method_id);
|
2019-07-18 07:31:48 +02:00
|
|
|
|
2020-08-23 19:39:08 +02:00
|
|
|
if (!$method_storage->external_mutation_free && !$context->inside_throw) {
|
|
|
|
if ($context->pure) {
|
2019-07-18 07:31:48 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpureMethodCall(
|
|
|
|
'Cannot call an impure constructor from a pure context',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2020-08-28 18:42:55 +02:00
|
|
|
} elseif ($statements_analyzer->getSource()
|
2020-08-23 16:28:26 +02:00
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
2020-08-28 18:42:55 +02:00
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
2020-08-23 16:28:26 +02:00
|
|
|
) {
|
2020-08-25 01:24:27 +02:00
|
|
|
$statements_analyzer->getSource()->inferred_has_mutation = true;
|
2020-08-23 16:28:26 +02:00
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
2019-07-18 07:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
$generic_param_types = null;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
if ($storage->template_types) {
|
2019-03-22 20:59:10 +01:00
|
|
|
$declaring_fq_class_name = $declaring_method_id
|
2020-02-15 02:54:26 +01:00
|
|
|
? $declaring_method_id->fq_class_name
|
2019-03-22 20:59:10 +01:00
|
|
|
: $fq_class_name;
|
|
|
|
|
2019-04-26 13:54:12 +02:00
|
|
|
foreach ($storage->template_types as $template_name => $base_type) {
|
2020-04-07 06:13:56 +02:00
|
|
|
if (isset($template_result->upper_bounds[$template_name][$fq_class_name])) {
|
2019-11-04 03:27:40 +01:00
|
|
|
$generic_param_type
|
2020-04-07 06:13:56 +02:00
|
|
|
= $template_result->upper_bounds[$template_name][$fq_class_name][0];
|
|
|
|
} elseif ($storage->template_type_extends && $template_result->upper_bounds) {
|
2019-07-09 20:48:19 +02:00
|
|
|
$generic_param_type = self::getGenericParamForOffset(
|
2019-03-22 20:59:10 +01:00
|
|
|
$declaring_fq_class_name,
|
2019-01-13 00:18:23 +01:00
|
|
|
$template_name,
|
|
|
|
$storage->template_type_extends,
|
2020-04-07 06:13:56 +02:00
|
|
|
$template_result->upper_bounds
|
2019-01-13 00:18:23 +01:00
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
} else {
|
2020-08-25 17:52:21 +02:00
|
|
|
if ($fq_class_name === 'SplObjectStorage') {
|
|
|
|
$generic_param_type = Type::getEmpty();
|
|
|
|
} else {
|
|
|
|
$generic_param_type = array_values($base_type)[0][0];
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-07-09 20:48:19 +02:00
|
|
|
|
|
|
|
$generic_param_type->had_template = true;
|
|
|
|
|
|
|
|
$generic_param_types[] = $generic_param_type;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
if ($generic_param_types) {
|
2020-03-24 23:00:20 +01:00
|
|
|
$result_atomic_type = new Type\Atomic\TGenericObject(
|
|
|
|
$fq_class_name,
|
|
|
|
$generic_param_types
|
|
|
|
);
|
|
|
|
|
|
|
|
$result_atomic_type->was_static = $from_static;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
2020-03-24 23:00:20 +01:00
|
|
|
new Type\Union([$result_atomic_type])
|
2019-11-25 17:44:54 +01:00
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt->args) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TooManyArguments(
|
|
|
|
'Class ' . $fq_class_name . ' has no __construct, but arguments were passed',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-08-16 22:49:33 +02:00
|
|
|
$fq_class_name . '::__construct'
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 15:49:32 +02:00
|
|
|
|
|
|
|
if ($storage->external_mutation_free) {
|
2019-08-31 16:02:11 +02:00
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
|
|
|
$stmt->external_mutation_free = true;
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = $statements_analyzer->node_data->getType($stmt);
|
|
|
|
|
|
|
|
if ($stmt_type) {
|
2020-03-14 06:09:12 +01:00
|
|
|
$stmt_type->reference_free = true;
|
2019-11-25 17:44:54 +01:00
|
|
|
}
|
2019-08-31 15:49:32 +02:00
|
|
|
}
|
2020-06-19 07:22:51 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
if ($statements_analyzer->control_flow_graph
|
2020-07-02 17:53:44 +02:00
|
|
|
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
2020-06-19 07:22:51 +02:00
|
|
|
&& ($stmt_type = $statements_analyzer->node_data->getType($stmt))
|
|
|
|
) {
|
|
|
|
$code_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
|
|
|
|
2020-06-30 03:08:43 +02:00
|
|
|
$method_storage = null;
|
|
|
|
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
if ($declaring_method_id) {
|
|
|
|
$method_storage = $codebase->methods->getStorage($declaring_method_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage->external_mutation_free
|
|
|
|
|| ($method_storage && $method_storage->specialize_call)
|
|
|
|
) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$method_source = ControlFlowNode::getForMethodReturn(
|
2020-06-19 07:22:51 +02:00
|
|
|
(string) $method_id,
|
|
|
|
$fq_class_name . '::__construct',
|
|
|
|
$storage->location,
|
|
|
|
$code_location
|
|
|
|
);
|
|
|
|
} else {
|
2020-09-21 05:59:52 +02:00
|
|
|
$method_source = ControlFlowNode::getForMethodReturn(
|
2020-06-19 07:22:51 +02:00
|
|
|
(string) $method_id,
|
|
|
|
$fq_class_name . '::__construct',
|
|
|
|
$storage->location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$statements_analyzer->control_flow_graph->addNode($method_source);
|
2020-06-19 07:22:51 +02:00
|
|
|
|
|
|
|
$stmt_type->parent_nodes = [$method_source];
|
|
|
|
}
|
2020-05-03 04:13:59 +02:00
|
|
|
} else {
|
2020-05-19 04:57:00 +02:00
|
|
|
ArgumentsAnalyzer::analyze(
|
2020-05-03 04:13:59 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$context
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-01-13 00:18:23 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-16 16:15:25 +01:00
|
|
|
* @param array<string, array<int|string, Type\Union>> $template_type_extends
|
2019-03-22 20:59:10 +01:00
|
|
|
* @param array<string, array<string, array{Type\Union}>> $found_generic_params
|
2019-01-13 00:18:23 +01:00
|
|
|
*/
|
|
|
|
private static function getGenericParamForOffset(
|
2019-03-22 20:59:10 +01:00
|
|
|
string $fq_class_name,
|
2019-01-13 00:18:23 +01:00
|
|
|
string $template_name,
|
|
|
|
array $template_type_extends,
|
2019-12-14 17:38:09 +01:00
|
|
|
array $found_generic_params,
|
|
|
|
bool $mapped = false
|
2020-09-04 22:26:33 +02:00
|
|
|
): Type\Union {
|
2019-03-22 20:59:10 +01:00
|
|
|
if (isset($found_generic_params[$template_name][$fq_class_name])) {
|
2019-12-14 17:38:09 +01:00
|
|
|
if (!$mapped && isset($template_type_extends[$fq_class_name][$template_name])) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($template_type_extends[$fq_class_name][$template_name]->getAtomicTypes() as $t) {
|
2019-12-14 17:38:09 +01:00
|
|
|
if ($t instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
if ($t->param_name !== $template_name) {
|
|
|
|
return $t->as;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
return $found_generic_params[$template_name][$fq_class_name][0];
|
2019-01-13 00:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($template_type_extends as $type_map) {
|
|
|
|
foreach ($type_map as $extended_template_name => $extended_type) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($extended_type->getAtomicTypes() as $extended_atomic_type) {
|
2019-03-16 16:15:25 +01:00
|
|
|
if (is_string($extended_template_name)
|
|
|
|
&& $extended_atomic_type instanceof Type\Atomic\TTemplateParam
|
|
|
|
&& $extended_atomic_type->param_name === $template_name
|
2019-03-17 17:20:57 +01:00
|
|
|
&& $extended_template_name !== $template_name
|
2019-03-16 16:15:25 +01:00
|
|
|
) {
|
|
|
|
return self::getGenericParamForOffset(
|
2019-03-22 20:59:10 +01:00
|
|
|
$fq_class_name,
|
2019-03-16 16:15:25 +01:00
|
|
|
$extended_template_name,
|
|
|
|
$template_type_extends,
|
2019-12-14 17:38:09 +01:00
|
|
|
$found_generic_params,
|
|
|
|
true
|
2019-03-16 16:15:25 +01:00
|
|
|
);
|
|
|
|
}
|
2019-01-13 00:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
return Type::getMixed();
|
2019-01-13 00:18:23 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|