1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 11:26:55 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php

606 lines
26 KiB
PHP
Raw Normal View History

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;
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
2018-01-29 00:29:38 +01:00
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\AbstractInstantiation;
use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\ImpureMethodCall;
use Psalm\Issue\InterfaceInstantiation;
use Psalm\Issue\InternalClass;
use Psalm\Issue\InvalidStringClass;
use Psalm\Issue\MixedMethodCall;
2018-01-29 00:29:38 +01:00
use Psalm\Issue\TooManyArguments;
use Psalm\Issue\UndefinedClass;
2018-01-29 00:29:38 +01:00
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\TNamedObject;
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
/**
* @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
{
/**
2018-11-11 18:01:14 +01:00
* @param StatementsAnalyzer $statements_analyzer
2018-01-29 00:29:38 +01:00
* @param PhpParser\Node\Expr\New_ $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\New_ $stmt,
Context $context
) {
$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
$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();
if ($context->calling_method_id
&& !$stmt->class instanceof PhpParser\Node\Name\FullyQualified
) {
$codebase->file_reference_provider->addMethodReferenceToClassMember(
$context->calling_method_id,
2018-11-11 18:01:14 +01:00
'use:' . $stmt->class->parts[0] . ':' . \md5($statements_analyzer->getFilePath())
);
}
2018-11-06 03:57:36 +01:00
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
2018-01-29 00:29:38 +01:00
$stmt->class,
$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);
} elseif ($context->self !== null) {
2018-01-29 00:29:38 +01:00
switch ($stmt->class->parts[0]) {
case 'self':
$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
$class_storage = $codebase->classlike_storage_provider->get($context->self);
$fq_class_name = $class_storage->name;
$can_extend = true;
2020-03-24 23:00:20 +01:00
$from_static = true;
2018-01-29 00:29:38 +01:00
break;
}
}
if ($codebase->store_node_types
&& $fq_class_name
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
2018-11-11 18:01:14 +01:00
$statements_analyzer->getFilePath(),
$stmt->class,
$codebase->classlikes->classExists($fq_class_name)
? $fq_class_name
: '*' . implode('\\', $stmt->class->parts)
);
}
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);
if ($stmt_class_type = $statements_analyzer->node_data->getType($stmt->class)) {
$has_single_class = $stmt_class_type->isSingleStringLiteral();
if ($has_single_class) {
$fq_class_name = $stmt_class_type->getSingleStringLiteral()->value;
} else {
if (self::checkMethodArgs(
null,
$stmt->args,
null,
$context,
new CodeLocation($statements_analyzer->getSource(), $stmt),
$statements_analyzer
) === false) {
return false;
}
}
$new_type = null;
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) {
if (!$statements_analyzer->node_data->getType($stmt)) {
2019-02-22 03:40:06 +01:00
$new_type_part = new Type\Atomic\TTemplateParam(
$lhs_type_part->param_name,
$lhs_type_part->as_type
? new Type\Union([$lhs_type_part->as_type])
2019-06-25 15:06:23 +02:00
: Type::parseString($lhs_type_part->as),
$lhs_type_part->defining_class
);
if ($new_type) {
$new_type = Type::combineUnionTypes(
$new_type,
new Type\Union([$new_type_part])
);
} else {
$new_type = new Type\Union([$new_type_part]);
}
}
if ($lhs_type_part->as_type) {
$codebase->methods->methodExists(
new \Psalm\Internal\MethodIdentifier(
$lhs_type_part->as_type->value,
'__construct'
),
$context->calling_method_id,
$codebase->collect_locations
? new CodeLocation($statements_analyzer->getSource(), $stmt) : null,
$statements_analyzer,
$statements_analyzer->getFilePath()
);
}
continue;
}
if ($lhs_type_part instanceof Type\Atomic\TLiteralClassString
|| $lhs_type_part instanceof Type\Atomic\TClassString
|| $lhs_type_part instanceof Type\Atomic\GetClassT
) {
if (!$statements_analyzer->node_data->getType($stmt)) {
if ($lhs_type_part instanceof Type\Atomic\TClassString) {
$generated_type = $lhs_type_part->as_type
? clone $lhs_type_part->as_type
: new Type\Atomic\TObject();
} elseif ($lhs_type_part instanceof Type\Atomic\GetClassT) {
$generated_type = new Type\Atomic\TObject();
2019-12-07 20:05:37 +01:00
if ($lhs_type_part->as_type->hasObjectType()
&& $lhs_type_part->as_type->isSingle()
) {
foreach ($lhs_type_part->as_type->getAtomicTypes() as $typeof_type_atomic) {
if ($typeof_type_atomic instanceof Type\Atomic\TNamedObject) {
$generated_type = new Type\Atomic\TNamedObject(
$typeof_type_atomic->value
);
}
}
}
} else {
$generated_type = new Type\Atomic\TNamedObject(
$lhs_type_part->value
);
}
if ($lhs_type_part instanceof Type\Atomic\TClassString) {
$can_extend = true;
}
if ($generated_type instanceof Type\Atomic\TObject) {
if (IssueBuffer::accepts(
new MixedMethodCall(
'Cannot call constructor on an unknown class',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
if ($new_type) {
$new_type = Type::combineUnionTypes(
$new_type,
new Type\Union([$generated_type])
);
} else {
$new_type = new Type\Union([$generated_type]);
}
}
continue;
}
if ($lhs_type_part instanceof Type\Atomic\TString) {
if ($config->allow_string_standin_for_class
&& !$lhs_type_part instanceof Type\Atomic\TNumericString
) {
// do nothing
} elseif (IssueBuffer::accepts(
new InvalidStringClass(
'String cannot be used as a class',
2018-11-11 18:01:14 +01:00
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($lhs_type_part instanceof Type\Atomic\TMixed
2019-02-22 03:40:06 +01:00
|| $lhs_type_part instanceof Type\Atomic\TTemplateParam
) {
if (IssueBuffer::accepts(
new MixedMethodCall(
'Cannot call constructor on an unknown class',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($lhs_type_part instanceof Type\Atomic\TFalse
&& $stmt_class_type->ignore_falsable_issues
) {
// do nothing
} elseif ($lhs_type_part instanceof Type\Atomic\TNull
&& $stmt_class_type->ignore_nullable_issues
) {
// do nothing
} elseif (IssueBuffer::accepts(
new UndefinedClass(
'Type ' . $lhs_type_part . ' cannot be called as a class',
2018-11-11 18:01:14 +01:00
new CodeLocation($statements_analyzer->getSource(), $stmt),
(string)$lhs_type_part
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
if ($new_type) {
$new_type = Type::combineUnionTypes(
$new_type,
Type::getObject()
);
} else {
$new_type = Type::getObject();
}
}
if (!$has_single_class) {
if ($new_type) {
$statements_analyzer->node_data->setType($stmt, $new_type);
}
return null;
}
} else {
return null;
}
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,
$context->calling_method_id
2019-06-01 16:32:49 +02:00
);
}
if ($context->check_classes) {
if ($context->isPhantomClass($fq_class_name)) {
return null;
}
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$fq_class_name,
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
false
) === false) {
2019-06-16 00:34:18 +02:00
return;
}
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()
)) {
}
return null;
}
}
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;
}
$statements_analyzer->node_data->setType(
$stmt,
2020-03-24 23:00:20 +01:00
new Type\Union([$result_atomic_type])
);
2018-01-29 00:29:38 +01:00
if (strtolower($fq_class_name) !== 'stdclass' &&
$codebase->classlikes->classExists($fq_class_name)
2018-01-29 00:29:38 +01:00
) {
$storage = $codebase->classlike_storage_provider->get($fq_class_name);
2018-01-29 00:29:38 +01:00
// if we're not calling this constructor via new static()
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
)) {
2019-06-16 00:34:18 +02:00
return;
2018-01-29 00:29:38 +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',
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
}
}
if ($storage->psalm_internal && $context->self) {
if (! NamespaceAnalyzer::isWithin($context->self, $storage->psalm_internal)) {
if (IssueBuffer::accepts(
new InternalClass(
$fq_class_name . ' is marked internal to ' . $storage->psalm_internal,
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}
if ($storage->internal && $context->self) {
if (! NamespaceAnalyzer::nameSpaceRootsMatch($context->self, $fq_class_name)) {
if (IssueBuffer::accepts(
new InternalClass(
$fq_class_name . ' is marked internal',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}
$method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, '__construct');
if ($codebase->methods->methodExists(
$method_id,
$context->calling_method_id,
$codebase->collect_locations ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null,
$statements_analyzer,
$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
&& !$context->collect_mutations
) {
ArgumentMapPopulator::recordArgumentPositions(
$statements_analyzer,
$stmt,
$codebase,
(string) $method_id
);
}
$template_result = new \Psalm\Internal\Type\TemplateResult([], []);
2018-01-29 00:29:38 +01:00
if (self::checkMethodArgs(
$method_id,
$stmt->args,
$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-03-12 04:38:49 +01:00
if (MethodVisibilityAnalyzer::analyze(
2018-01-29 00:29:38 +01:00
$method_id,
$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;
}
if ($context->pure) {
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
if ($declaring_method_id) {
$method_storage = $codebase->methods->getStorage($declaring_method_id);
2020-02-22 04:15:25 +01:00
if (!$method_storage->external_mutation_free && !$context->inside_throw) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an impure constructor from a pure context',
new CodeLocation($statements_analyzer, $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}
}
$generic_param_types = null;
2018-01-29 00:29:38 +01:00
if ($storage->template_types) {
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
$declaring_fq_class_name = $declaring_method_id
? $declaring_method_id->fq_class_name
: $fq_class_name;
foreach ($storage->template_types as $template_name => $base_type) {
if (isset($template_result->generic_params[$template_name][$fq_class_name])) {
$generic_param_type
= $template_result->generic_params[$template_name][$fq_class_name][0];
} elseif ($storage->template_type_extends && $template_result->generic_params) {
$generic_param_type = self::getGenericParamForOffset(
$declaring_fq_class_name,
2019-01-13 00:18:23 +01:00
$template_name,
$storage->template_type_extends,
$template_result->generic_params
2019-01-13 00:18:23 +01:00
);
2018-01-29 00:29:38 +01:00
} else {
$generic_param_type = array_values($base_type)[0][0];
2018-01-29 00:29:38 +01:00
}
$generic_param_type->had_template = true;
$generic_param_types[] = $generic_param_type;
2018-01-29 00:29:38 +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;
$statements_analyzer->node_data->setType(
$stmt,
2020-03-24 23:00:20 +01:00
new Type\Union([$result_atomic_type])
);
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),
$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
}
}
if ($storage->external_mutation_free) {
/** @psalm-suppress UndefinedPropertyAssignment */
$stmt->external_mutation_free = true;
$stmt_type = $statements_analyzer->node_data->getType($stmt);
if ($stmt_type) {
2020-03-14 06:09:12 +01:00
$stmt_type->reference_free = true;
}
}
2018-01-29 00:29:38 +01:00
}
}
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
$context->removeAllObjectVars();
}
return null;
}
2019-01-13 00:18:23 +01:00
/**
* @param string $template_name
* @param array<string, array<int|string, Type\Union>> $template_type_extends
* @param array<string, array<string, array{Type\Union}>> $found_generic_params
* @return Type\Union
2019-01-13 00:18:23 +01:00
*/
private static function getGenericParamForOffset(
string $fq_class_name,
2019-01-13 00:18:23 +01:00
string $template_name,
array $template_type_extends,
array $found_generic_params,
bool $mapped = false
2019-01-13 00:18:23 +01:00
) {
if (isset($found_generic_params[$template_name][$fq_class_name])) {
if (!$mapped && isset($template_type_extends[$fq_class_name][$template_name])) {
foreach ($template_type_extends[$fq_class_name][$template_name]->getAtomicTypes() as $t) {
if ($t instanceof Type\Atomic\TTemplateParam) {
if ($t->param_name !== $template_name) {
return $t->as;
}
}
}
}
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) {
foreach ($extended_type->getAtomicTypes() as $extended_atomic_type) {
if (is_string($extended_template_name)
&& $extended_atomic_type instanceof Type\Atomic\TTemplateParam
&& $extended_atomic_type->param_name === $template_name
&& $extended_template_name !== $template_name
) {
return self::getGenericParamForOffset(
$fq_class_name,
$extended_template_name,
$template_type_extends,
$found_generic_params,
true
);
}
2019-01-13 00:18:23 +01:00
}
}
}
return Type::getMixed();
2019-01-13 00:18:23 +01:00
}
2018-01-29 00:29:38 +01:00
}