1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Move class const analysis

This commit is contained in:
Matthew Brown 2019-09-14 13:12:54 -04:00
parent 0b39884974
commit d941294a84
3 changed files with 353 additions and 324 deletions

View File

@ -0,0 +1,351 @@
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\Fetch;
use PhpParser;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\DeprecatedConstant;
use Psalm\Issue\InaccessibleClassConstant;
use Psalm\Issue\ParentNotFound;
use Psalm\Issue\UndefinedConstant;
use Psalm\IssueBuffer;
use Psalm\Type;
use function implode;
use function strtolower;
use function explode;
/**
* @internal
*/
class ClassConstFetchAnalyzer
{
/**
* @param StatementsAnalyzer $statements_analyzer
* @param PhpParser\Node\Expr\ClassConstFetch $stmt
* @param Context $context
*
* @return null|false
*/
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\ClassConstFetch $stmt,
Context $context
) {
$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) {
throw new \UnexpectedValueException('$context->self cannot be null');
}
$fq_class_name = (string)$context->self;
} 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;
}
return;
}
} 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') {
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$fq_class_name,
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
$statements_analyzer->getSuppressedIssues(),
false,
true
) === false) {
return;
}
}
}
}
$moved_class = false;
if ($codebase->alter_code) {
$moved_class = $codebase->classlikes->handleClassLikeReferenceInMigration(
$codebase,
$statements_analyzer,
$stmt->class,
$fq_class_name,
$context->calling_method_id
);
}
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
if ($codebase->classExists($fq_class_name)) {
$class_const_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($class_const_storage->deprecated && $fq_class_name !== $context->self) {
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') {
$stmt->inferredType = new Type\Union([
new Type\Atomic\TClassString($fq_class_name, new Type\Atomic\TNamedObject($fq_class_name))
]);
} else {
$stmt->inferredType = Type::getLiteralClassString($fq_class_name);
}
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt->class,
$fq_class_name
);
}
return null;
}
// if we're ignoring that the class doesn't exist, exit anyway
if (!$codebase->classOrInterfaceExists($fq_class_name)) {
$stmt->inferredType = Type::getMixed();
return null;
}
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) {
return;
}
$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->classExtends($context->self, $fq_class_name)
) {
$class_visibility = \ReflectionProperty::IS_PROTECTED;
} else {
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}
$class_constants = $codebase->classlikes->getConstantsForClass(
$fq_class_name,
$class_visibility
);
if (!isset($class_constants[$stmt->name->name])) {
$all_class_constants = [];
if ($fq_class_name !== $context->self) {
$all_class_constants = $codebase->classlikes->getConstantsForClass(
$fq_class_name,
\ReflectionProperty::IS_PRIVATE
);
}
if ($all_class_constants && isset($all_class_constants[$stmt->name->name])) {
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
}
}
return;
}
if ($context->calling_method_id) {
$codebase->file_reference_provider->addMethodReferenceToClassMember(
$context->calling_method_id,
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) {
list($new_fq_class_name, $new_const_name) = explode('::', $transformation);
$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);
}
}
}
$class_const_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($class_const_storage->deprecated && $fq_class_name !== $context->self) {
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 (isset($class_const_storage->deprecated_constants[$stmt->name->name])) {
if (IssueBuffer::accepts(
new DeprecatedConstant(
'Constant ' . $const_id . ' is deprecated',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
if (isset($class_constants[$stmt->name->name])
&& ($first_part_lc !== 'static' || $class_const_storage->final)
) {
$stmt->inferredType = clone $class_constants[$stmt->name->name];
$context->vars_in_scope[$const_id] = $stmt->inferredType;
} else {
$stmt->inferredType = Type::getMixed();
}
return null;
}
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context);
$lhs_type = $stmt->class->inferredType;
$class_string_types = [];
$has_mixed_or_object = false;
if ($lhs_type) {
foreach ($lhs_type->getTypes() as $lhs_atomic_type) {
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) {
$stmt->inferredType = new Type\Union([new Type\Atomic\TClassString()]);
} elseif ($class_string_types) {
$stmt->inferredType = new Type\Union($class_string_types);
} else {
$stmt->inferredType = Type::getMixed();
}
return;
}
$stmt->inferredType = Type::getMixed();
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context) === false) {
return false;
}
return null;
}
}

View File

@ -81,329 +81,6 @@ class ConstFetchAnalyzer
}
}
/**
* @param StatementsAnalyzer $statements_analyzer
* @param PhpParser\Node\Expr\ClassConstFetch $stmt
* @param Context $context
*
* @return null|false
*/
public static function analyzeClassConst(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\ClassConstFetch $stmt,
Context $context
) {
$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) {
throw new \UnexpectedValueException('$context->self cannot be null');
}
$fq_class_name = (string)$context->self;
} 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;
}
return;
}
} 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') {
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$fq_class_name,
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
$statements_analyzer->getSuppressedIssues(),
false,
true
) === false) {
return;
}
}
}
}
$moved_class = false;
if ($codebase->alter_code) {
$moved_class = $codebase->classlikes->handleClassLikeReferenceInMigration(
$codebase,
$statements_analyzer,
$stmt->class,
$fq_class_name,
$context->calling_method_id
);
}
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
if ($codebase->classExists($fq_class_name)) {
$class_const_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($class_const_storage->deprecated && $fq_class_name !== $context->self) {
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') {
$stmt->inferredType = new Type\Union([
new Type\Atomic\TClassString($fq_class_name, new Type\Atomic\TNamedObject($fq_class_name))
]);
} else {
$stmt->inferredType = Type::getLiteralClassString($fq_class_name);
}
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt->class,
$fq_class_name
);
}
return null;
}
// if we're ignoring that the class doesn't exist, exit anyway
if (!$codebase->classOrInterfaceExists($fq_class_name)) {
$stmt->inferredType = Type::getMixed();
return null;
}
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) {
return;
}
$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->classExtends($context->self, $fq_class_name)
) {
$class_visibility = \ReflectionProperty::IS_PROTECTED;
} else {
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}
$class_constants = $codebase->classlikes->getConstantsForClass(
$fq_class_name,
$class_visibility
);
if (!isset($class_constants[$stmt->name->name])) {
$all_class_constants = [];
if ($fq_class_name !== $context->self) {
$all_class_constants = $codebase->classlikes->getConstantsForClass(
$fq_class_name,
\ReflectionProperty::IS_PRIVATE
);
}
if ($all_class_constants && isset($all_class_constants[$stmt->name->name])) {
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
}
}
return;
}
if ($context->calling_method_id) {
$codebase->file_reference_provider->addMethodReferenceToClassMember(
$context->calling_method_id,
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) {
list($new_fq_class_name, $new_const_name) = explode('::', $transformation);
$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);
}
}
}
$class_const_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($class_const_storage->deprecated && $fq_class_name !== $context->self) {
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 (isset($class_const_storage->deprecated_constants[$stmt->name->name])) {
if (IssueBuffer::accepts(
new DeprecatedConstant(
'Constant ' . $const_id . ' is deprecated',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
if (isset($class_constants[$stmt->name->name])
&& ($first_part_lc !== 'static' || $class_const_storage->final)
) {
$stmt->inferredType = clone $class_constants[$stmt->name->name];
$context->vars_in_scope[$const_id] = $stmt->inferredType;
} else {
$stmt->inferredType = Type::getMixed();
}
return null;
}
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context);
$lhs_type = $stmt->class->inferredType;
$class_string_types = [];
$has_mixed_or_object = false;
if ($lhs_type) {
foreach ($lhs_type->getTypes() as $lhs_atomic_type) {
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) {
$stmt->inferredType = new Type\Union([new Type\Atomic\TClassString()]);
} elseif ($class_string_types) {
$stmt->inferredType = new Type\Union($class_string_types);
} else {
$stmt->inferredType = Type::getMixed();
}
return;
}
$stmt->inferredType = Type::getMixed();
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context) === false) {
return false;
}
return null;
}
/**
* @param Codebase $codebase
* @param ?string $fq_const_name

View File

@ -17,6 +17,7 @@ use Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\NewAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\StaticCallAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ArrayFetchAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ClassConstFetchAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\PropertyFetchAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\VariableFetchAnalyzer;
@ -233,7 +234,7 @@ class ExpressionAnalyzer
self::analyzeIsset($statements_analyzer, $stmt, $context);
$stmt->inferredType = Type::getBool();
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
if (ConstFetchAnalyzer::analyzeClassConst($statements_analyzer, $stmt, $context) === false) {
if (ClassConstFetchAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
return false;
}
} elseif ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {