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\MethodAnalyzer;
|
|
|
|
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;
|
2018-02-18 00:53:02 +01:00
|
|
|
use Psalm\Issue\InterfaceInstantiation;
|
2018-03-06 18:19:50 +01:00
|
|
|
use Psalm\Issue\InvalidStringClass;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\TooManyArguments;
|
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;
|
|
|
|
|
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
|
|
|
$project_analyzer = $statements_analyzer->getFileAnalyzer()->project_analyzer;
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
$config = $codebase->config;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
$late_static = false;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if ($context->calling_method_id
|
|
|
|
&& !$stmt->class instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
) {
|
|
|
|
$codebase->file_reference_provider->addReferenceToClassMethod(
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
|
|
|
if ($context->check_classes) {
|
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
$fq_class_name,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2018-01-29 00:29:38 +01:00
|
|
|
false
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-18 00:53:02 +01:00
|
|
|
|
|
|
|
if ($codebase->interfaceExists($fq_class_name)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InterfaceInstantiation(
|
|
|
|
'Interface ' . $fq_class_name . ' cannot be instantiated',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->class)
|
2018-02-18 00:53:02 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-02-18 00:53:02 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ($stmt->class->parts[0]) {
|
|
|
|
case 'self':
|
|
|
|
$fq_class_name = $context->self;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'parent':
|
|
|
|
$fq_class_name = $context->parent;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'static':
|
|
|
|
// @todo maybe we can do better here
|
|
|
|
$fq_class_name = $context->self;
|
|
|
|
$late_static = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
if ($codebase->server_mode && $fq_class_name) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt->class,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$generic_params = null;
|
|
|
|
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
null,
|
|
|
|
$stmt->args,
|
|
|
|
$generic_params,
|
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer
|
2018-01-29 18:22:50 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:20:54 +01:00
|
|
|
if (isset($stmt->class->inferredType)) {
|
2018-05-20 06:27:53 +02:00
|
|
|
$new_type = null;
|
|
|
|
|
2018-03-06 17:20:54 +01:00
|
|
|
foreach ($stmt->class->inferredType->getTypes() as $lhs_type_part) {
|
|
|
|
// this is always OK
|
2018-05-21 18:40:39 +02:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TLiteralClassString
|
|
|
|
|| $lhs_type_part instanceof Type\Atomic\TClassString
|
|
|
|
) {
|
2018-04-04 04:20:00 +02:00
|
|
|
if (!isset($stmt->inferredType)) {
|
2018-05-21 18:40:39 +02:00
|
|
|
$class_name = $lhs_type_part instanceof Type\Atomic\TClassString
|
|
|
|
? 'object'
|
|
|
|
: $lhs_type_part->value;
|
|
|
|
|
2018-05-20 06:27:53 +02:00
|
|
|
if ($new_type) {
|
|
|
|
$new_type = Type::combineUnionTypes(
|
|
|
|
$new_type,
|
2018-05-21 18:40:39 +02:00
|
|
|
Type::parseString($class_name)
|
2018-05-20 06:27:53 +02:00
|
|
|
);
|
|
|
|
} else {
|
2018-05-21 18:40:39 +02:00
|
|
|
$new_type = Type::parseString($class_name);
|
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
|
2018-04-20 16:52:23 +02:00
|
|
|
|| $lhs_type_part instanceof Type\Atomic\TGenericParam
|
|
|
|
) {
|
2018-05-20 06:27:53 +02:00
|
|
|
// do nothing
|
2018-08-02 20:43:13 +02:00
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\TFalse
|
|
|
|
&& $stmt->class->inferredType->ignore_falsable_issues
|
|
|
|
) {
|
|
|
|
// do nothing
|
2018-05-20 06:27:53 +02:00
|
|
|
} elseif ($lhs_type_part instanceof Type\Atomic\TNull
|
2018-03-06 20:11:32 +01:00
|
|
|
&& $stmt->class->inferredType->ignore_nullable_issues
|
|
|
|
) {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($new_type) {
|
|
|
|
$stmt->inferredType = $new_type;
|
2018-03-06 17:20:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 18:22:50 +01:00
|
|
|
return null;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_class_name) {
|
|
|
|
$stmt->inferredType = new Type\Union([new TNamedObject($fq_class_name)]);
|
|
|
|
|
|
|
|
if (strtolower($fq_class_name) !== 'stdclass' &&
|
|
|
|
$context->check_classes &&
|
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:01:14 +01:00
|
|
|
$storage = $project_analyzer->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 && !$late_static) {
|
|
|
|
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
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedClass(
|
|
|
|
$fq_class_name . ' is marked deprecated',
|
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
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
if ($codebase->methods->methodExists(
|
2018-01-29 00:29:38 +01:00
|
|
|
$fq_class_name . '::__construct',
|
2018-09-26 00:37:24 +02:00
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
$context->collect_references ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
|
|
|
$method_id = $fq_class_name . '::__construct';
|
|
|
|
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$method_id,
|
|
|
|
$stmt->args,
|
|
|
|
$found_generic_params,
|
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (MethodAnalyzer::checkMethodVisibility(
|
2018-01-29 00:29:38 +01:00
|
|
|
$method_id,
|
|
|
|
$context->self,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$generic_params = null;
|
|
|
|
|
|
|
|
if ($storage->template_types) {
|
|
|
|
foreach ($storage->template_types as $template_name => $_) {
|
|
|
|
if (isset($found_generic_params[$template_name])) {
|
|
|
|
$generic_params[] = $found_generic_params[$template_name];
|
|
|
|
} else {
|
|
|
|
$generic_params[] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_class_name === 'ArrayIterator' && isset($stmt->args[0]->value->inferredType)) {
|
|
|
|
$first_arg_type = $stmt->args[0]->value->inferredType;
|
|
|
|
|
|
|
|
if ($first_arg_type->hasGeneric()) {
|
|
|
|
$key_type = null;
|
|
|
|
$value_type = null;
|
|
|
|
|
|
|
|
foreach ($first_arg_type->getTypes() as $type) {
|
|
|
|
if ($type instanceof Type\Atomic\TArray) {
|
|
|
|
$first_type_param = count($type->type_params) ? $type->type_params[0] : null;
|
|
|
|
$last_type_param = $type->type_params[count($type->type_params) - 1];
|
|
|
|
|
|
|
|
if ($value_type === null) {
|
|
|
|
$value_type = clone $last_type_param;
|
|
|
|
} else {
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $last_type_param);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$key_type || !$first_type_param) {
|
|
|
|
$key_type = $first_type_param ? clone $first_type_param : Type::getMixed();
|
|
|
|
} else {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $first_type_param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key_type === null) {
|
|
|
|
throw new \UnexpectedValueException('$key_type cannot be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value_type === null) {
|
|
|
|
throw new \UnexpectedValueException('$value_type cannot be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = new Type\Union([
|
|
|
|
new Type\Atomic\TGenericObject(
|
|
|
|
$fq_class_name,
|
|
|
|
[
|
|
|
|
$key_type,
|
|
|
|
$value_type,
|
|
|
|
]
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} elseif ($generic_params) {
|
|
|
|
$stmt->inferredType = new Type\Union([
|
|
|
|
new Type\Atomic\TGenericObject(
|
|
|
|
$fq_class_name,
|
|
|
|
$generic_params
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) {
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|