1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 19:36:59 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ClassConstFetchAnalyzer.php

430 lines
17 KiB
PHP
Raw Normal View History

2019-09-14 19:12:54 +02:00
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\Fetch;
use PhpParser;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
2019-09-14 19:12:54 +02:00
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\CircularReference;
2019-09-14 19:12:54 +02:00
use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\DeprecatedConstant;
use Psalm\Issue\InternalClass;
2019-09-14 19:12:54 +02:00
use Psalm\Issue\InaccessibleClassConstant;
use Psalm\Issue\NonStaticSelfCall;
2019-09-14 19:12:54 +02:00
use Psalm\Issue\ParentNotFound;
use Psalm\Issue\UndefinedConstant;
use Psalm\IssueBuffer;
use Psalm\Type;
use function strtolower;
use function explode;
/**
* @internal
*/
class ClassConstFetchAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\ClassConstFetch $stmt,
Context $context
2020-05-18 21:13:27 +02:00
) : bool {
2019-09-14 19:12:54 +02:00
$codebase = $statements_analyzer->getCodebase();
if ($stmt->class instanceof PhpParser\Node\Name) {
$first_part_lc = strtolower($stmt->class->parts[0]);
if ($first_part_lc === 'self' || $first_part_lc === 'static') {
if (!$context->self) {
if (IssueBuffer::accepts(
new NonStaticSelfCall(
'Cannot use ' . $first_part_lc . ' outside class context',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
return false;
}
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
$fq_class_name = $context->self;
2019-09-14 19:12:54 +02:00
} elseif ($first_part_lc === 'parent') {
$fq_class_name = $statements_analyzer->getParentFQCLN();
if ($fq_class_name === null) {
if (IssueBuffer::accepts(
new ParentNotFound(
'Cannot check property fetch on parent as this class does not extend another',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
return false;
}
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
} else {
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
$stmt->class,
$statements_analyzer->getAliases()
);
if ($stmt->name instanceof PhpParser\Node\Identifier) {
if ((!$context->inside_class_exists || $stmt->name->name !== 'class')
&& !isset($context->phantom_classes[strtolower($fq_class_name)])
) {
2019-09-14 19:12:54 +02:00
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$fq_class_name,
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
$context->self,
$context->calling_method_id,
2019-09-14 19:12:54 +02:00
$statements_analyzer->getSuppressedIssues(),
false,
true
) === false) {
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
}
}
}
$moved_class = false;
2019-11-15 22:50:43 +01:00
if ($codebase->alter_code
2019-11-16 03:47:53 +01:00
&& !\in_array($stmt->class->parts[0], ['parent', 'static'])
2019-11-15 22:50:43 +01:00
) {
2019-09-14 19:12:54 +02:00
$moved_class = $codebase->classlikes->handleClassLikeReferenceInMigration(
$codebase,
$statements_analyzer,
$stmt->class,
$fq_class_name,
$context->calling_method_id,
2019-11-17 01:59:08 +01:00
false,
$stmt->class->parts[0] === 'self'
2019-09-14 19:12:54 +02:00
);
}
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
2020-03-28 19:45:58 +01:00
if ($codebase->classlikes->classExists($fq_class_name)) {
$fq_class_name = $codebase->classlikes->getUnAliasedName($fq_class_name);
$const_class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
$fq_class_name = $const_class_storage->name;
2019-09-14 19:12:54 +02:00
if ($const_class_storage->deprecated && $fq_class_name !== $context->self) {
2019-09-14 19:12:54 +02:00
if (IssueBuffer::accepts(
new DeprecatedClass(
'Class ' . $fq_class_name . ' is deprecated',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}
if ($first_part_lc === 'static') {
$static_named_object = new Type\Atomic\TNamedObject($fq_class_name);
$static_named_object->was_static = true;
$statements_analyzer->node_data->setType(
$stmt,
new Type\Union([
new Type\Atomic\TClassString($fq_class_name, $static_named_object)
])
);
2019-09-14 19:12:54 +02:00
} else {
$statements_analyzer->node_data->setType($stmt, Type::getLiteralClassString($fq_class_name));
2019-09-14 19:12:54 +02:00
}
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt->class,
$fq_class_name
);
}
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
// if we're ignoring that the class doesn't exist, exit anyway
2020-03-28 19:45:58 +01:00
if (!$codebase->classlikes->classOrInterfaceExists($fq_class_name)) {
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
2019-09-14 19:12:54 +02:00
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt->class,
$fq_class_name
);
}
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
$const_id = $fq_class_name . '::' . $stmt->name;
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt->name,
$const_id
);
}
if ($fq_class_name === $context->self
|| (
$statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer &&
$fq_class_name === $statements_analyzer->getSource()->getFQCLN()
)
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
} elseif ($context->self &&
($codebase->classlikes->classExtends($context->self, $fq_class_name)
|| $codebase->classlikes->classExtends($fq_class_name, $context->self))
2019-09-14 19:12:54 +02:00
) {
$class_visibility = \ReflectionProperty::IS_PROTECTED;
} else {
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}
try {
$class_constant_type = $codebase->classlikes->getClassConstantType(
$fq_class_name,
$stmt->name->name,
$class_visibility,
$statements_analyzer
);
} catch (\InvalidArgumentException $_) {
2020-05-18 21:13:27 +02:00
return true;
} catch (\Psalm\Exception\CircularReferenceException $e) {
if (IssueBuffer::accepts(
new CircularReference(
'Constant ' . $const_id . ' contains a circular reference',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
2020-05-18 21:13:27 +02:00
return true;
}
2019-09-14 19:12:54 +02:00
if (!$class_constant_type) {
2019-09-14 19:12:54 +02:00
if ($fq_class_name !== $context->self) {
$class_constant_type = $codebase->classlikes->getClassConstantType(
2019-09-14 19:12:54 +02:00
$fq_class_name,
$stmt->name->name,
\ReflectionProperty::IS_PRIVATE,
$statements_analyzer
2019-09-14 19:12:54 +02:00
);
}
if ($class_constant_type) {
2019-09-14 19:12:54 +02:00
if (IssueBuffer::accepts(
new InaccessibleClassConstant(
'Constant ' . $const_id . ' is not visible in this context',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($context->check_consts) {
if (IssueBuffer::accepts(
new UndefinedConstant(
'Constant ' . $const_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
if ($context->calling_method_id) {
2019-09-14 19:12:54 +02:00
$codebase->file_reference_provider->addMethodReferenceToClassMember(
$context->calling_method_id,
2019-09-14 19:12:54 +02:00
strtolower($fq_class_name) . '::' . $stmt->name->name
);
}
$declaring_const_id = strtolower($fq_class_name) . '::' . $stmt->name->name;
if ($codebase->alter_code && !$moved_class) {
foreach ($codebase->class_constant_transforms as $original_pattern => $transformation) {
if ($declaring_const_id === $original_pattern) {
[$new_fq_class_name, $new_const_name] = explode('::', $transformation);
2019-09-14 19:12:54 +02:00
$file_manipulations = [];
if (strtolower($new_fq_class_name) !== strtolower($fq_class_name)) {
$file_manipulations[] = new \Psalm\FileManipulation(
(int) $stmt->class->getAttribute('startFilePos'),
(int) $stmt->class->getAttribute('endFilePos') + 1,
Type::getStringFromFQCLN(
$new_fq_class_name,
$statements_analyzer->getNamespace(),
$statements_analyzer->getAliasedClassesFlipped(),
null
)
);
}
$file_manipulations[] = new \Psalm\FileManipulation(
(int) $stmt->name->getAttribute('startFilePos'),
(int) $stmt->name->getAttribute('endFilePos') + 1,
$new_const_name
);
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
}
}
}
$const_class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
2019-09-14 19:12:54 +02:00
if ($context->self
&& !$context->collect_initializations
&& !$context->collect_mutations
&& $const_class_storage->internal
&& !NamespaceAnalyzer::isWithin($context->self, $const_class_storage->internal)
) {
if (IssueBuffer::accepts(
new InternalClass(
$fq_class_name . ' is internal to ' . $const_class_storage->internal
. ' but called from ' . $context->self,
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
if ($const_class_storage->deprecated && $fq_class_name !== $context->self) {
2019-09-14 19:12:54 +02:00
if (IssueBuffer::accepts(
new DeprecatedClass(
'Class ' . $fq_class_name . ' is deprecated',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($const_class_storage->constants[$stmt->name->name]->deprecated) {
2019-09-14 19:12:54 +02:00
if (IssueBuffer::accepts(
new DeprecatedConstant(
'Constant ' . $const_id . ' is deprecated',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
if ($first_part_lc !== 'static' || $const_class_storage->final) {
$stmt_type = clone $class_constant_type;
$statements_analyzer->node_data->setType($stmt, $stmt_type);
$context->vars_in_scope[$const_id] = $stmt_type;
2019-09-14 19:12:54 +02:00
} else {
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
2019-09-14 19:12:54 +02:00
}
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
$was_inside_use = $context->inside_use;
$context->inside_use = true;
2019-09-14 19:12:54 +02:00
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context);
$context->inside_use = $was_inside_use;
$lhs_type = $statements_analyzer->node_data->getType($stmt->class);
2019-09-14 19:12:54 +02:00
$class_string_types = [];
$has_mixed_or_object = false;
if ($lhs_type) {
foreach ($lhs_type->getAtomicTypes() as $lhs_atomic_type) {
2019-09-14 19:12:54 +02:00
if ($lhs_atomic_type instanceof Type\Atomic\TNamedObject) {
$class_string_types[] = new Type\Atomic\TClassString(
$lhs_atomic_type->value,
clone $lhs_atomic_type
);
} elseif ($lhs_atomic_type instanceof Type\Atomic\TObject
|| $lhs_atomic_type instanceof Type\Atomic\TMixed
) {
$has_mixed_or_object = true;
}
}
}
if ($has_mixed_or_object) {
$statements_analyzer->node_data->setType($stmt, new Type\Union([new Type\Atomic\TClassString()]));
2019-09-14 19:12:54 +02:00
} elseif ($class_string_types) {
$statements_analyzer->node_data->setType($stmt, new Type\Union($class_string_types));
2019-09-14 19:12:54 +02:00
} else {
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
2019-09-14 19:12:54 +02:00
}
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
2019-09-14 19:12:54 +02:00
$was_inside_use = $context->inside_use;
$context->inside_use = true;
2019-09-14 19:12:54 +02:00
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context) === false) {
return false;
}
$context->inside_use = $was_inside_use;
2020-05-18 21:13:27 +02:00
return true;
2019-09-14 19:12:54 +02:00
}
2020-05-19 18:56:23 +02:00
public static function analyzeClassConstAssignment(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Stmt\ClassConst $stmt,
Context $context
): void {
foreach ($stmt->consts as $const) {
ExpressionAnalyzer::analyze($statements_analyzer, $const->value, $context);
}
}
2019-09-14 19:12:54 +02:00
}