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

226 lines
8.0 KiB
PHP
Raw Normal View History

2018-01-14 18:09:40 +01:00
<?php
namespace Psalm\Checker\Statements\Expression\Fetch;
use PhpParser;
use Psalm\Checker\ClassLikeChecker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\StatementsChecker;
use Psalm\Checker\TraitChecker;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\InaccessibleClassConstant;
use Psalm\Issue\ParentNotFound;
use Psalm\Issue\UndefinedConstant;
use Psalm\IssueBuffer;
use Psalm\Type;
class ConstFetchChecker
{
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\ConstFetch $stmt
* @param Context $context
*
* @return void
2018-01-14 18:09:40 +01:00
*/
public static function analyze(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\ConstFetch $stmt,
Context $context
) {
$const_name = implode('\\', $stmt->name->parts);
switch (strtolower($const_name)) {
case 'null':
$stmt->inferredType = Type::getNull();
break;
case 'false':
// false is a subtype of bool
$stmt->inferredType = Type::getFalse();
break;
case 'true':
$stmt->inferredType = Type::getTrue();
break;
case 'stdin':
$stmt->inferredType = Type::getResource();
break;
default:
$const_type = $statements_checker->getConstType(
$statements_checker,
$const_name,
$stmt->name instanceof PhpParser\Node\Name\FullyQualified,
$context
);
if ($const_type) {
$stmt->inferredType = clone $const_type;
} elseif ($context->check_consts) {
if (IssueBuffer::accepts(
new UndefinedConstant(
'Const ' . $const_name . ' is not defined',
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
2018-01-14 18:09:40 +01:00
}
}
}
}
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\ClassConstFetch $stmt
* @param Context $context
*
* @return null|false
*/
public static function analyzeClassConst(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\ClassConstFetch $stmt,
Context $context
) {
if ($context->check_consts
&& $stmt->class instanceof PhpParser\Node\Name
&& $stmt->name instanceof PhpParser\Node\Identifier
2018-01-14 18:09:40 +01:00
) {
2018-01-23 21:46:14 +01:00
$first_part_lc = strtolower($stmt->class->parts[0]);
if ($first_part_lc === 'self' || $first_part_lc === 'static') {
2018-01-14 18:09:40 +01:00
if (!$context->self) {
throw new \UnexpectedValueException('$context->self cannot be null');
}
$fq_class_name = (string)$context->self;
2018-01-23 21:46:14 +01:00
} elseif ($first_part_lc === 'parent') {
2018-01-14 18:09:40 +01:00
$fq_class_name = $statements_checker->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_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
return;
}
} else {
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
$stmt->class,
$statements_checker->getAliases()
);
if (!$context->inside_class_exists || $stmt->name->name !== 'class') {
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
$statements_checker,
$fq_class_name,
new CodeLocation($statements_checker->getSource(), $stmt->class),
$statements_checker->getSuppressedIssues(),
false
) === false) {
return false;
}
2018-01-14 18:09:40 +01:00
}
}
if ($stmt->name->name === 'class') {
$stmt->inferredType = Type::getClassString($fq_class_name);
2018-01-14 18:09:40 +01:00
return null;
}
$project_checker = $statements_checker->getFileChecker()->project_checker;
2018-02-01 06:50:01 +01:00
$codebase = $project_checker->codebase;
2018-01-14 18:09:40 +01:00
// if we're ignoring that the class doesn't exist, exit anyway
2018-02-01 06:50:01 +01:00
if (!$codebase->classOrInterfaceExists($fq_class_name)) {
2018-01-14 18:09:40 +01:00
$stmt->inferredType = Type::getMixed();
return null;
}
$const_id = $fq_class_name . '::' . $stmt->name;
if ($fq_class_name === $context->self
|| (
$statements_checker->getSource()->getSource() instanceof TraitChecker &&
$fq_class_name === $statements_checker->getSource()->getFQCLN()
)
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
} elseif ($context->self &&
2018-02-01 06:50:01 +01:00
$codebase->classExtends($context->self, $fq_class_name)
2018-01-14 18:09:40 +01:00
) {
$class_visibility = \ReflectionProperty::IS_PROTECTED;
} else {
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}
$class_constants = $codebase->classlikes->getConstantsForClass(
2018-01-14 18:09:40 +01:00
$fq_class_name,
$class_visibility
);
if (!isset($class_constants[$stmt->name->name]) && $first_part_lc !== 'static') {
2018-01-14 18:09:40 +01:00
$all_class_constants = [];
if ($fq_class_name !== $context->self) {
$all_class_constants = $codebase->classlikes->getConstantsForClass(
2018-01-14 18:09:40 +01:00
$fq_class_name,
\ReflectionProperty::IS_PRIVATE
);
}
if ($all_class_constants && isset($all_class_constants[$stmt->name->name])) {
if (IssueBuffer::accepts(
2018-01-14 18:09:40 +01:00
new InaccessibleClassConstant(
'Constant ' . $const_id . ' is not visible in this context',
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
2018-01-14 18:09:40 +01:00
} else {
if (IssueBuffer::accepts(
2018-01-14 18:09:40 +01:00
new UndefinedConstant(
'Constant ' . $const_id . ' is not defined',
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
2018-01-14 18:09:40 +01:00
}
return false;
}
if (isset($class_constants[$stmt->name->name]) && $first_part_lc !== 'static') {
$stmt->inferredType = clone $class_constants[$stmt->name->name];
} else {
$stmt->inferredType = Type::getMixed();
}
2018-01-14 18:09:40 +01:00
return null;
}
$stmt->inferredType = Type::getMixed();
if ($stmt->class instanceof PhpParser\Node\Expr) {
if (ExpressionChecker::analyze($statements_checker, $stmt->class, $context) === false) {
return false;
}
}
return null;
}
}