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
|
|
|
|
*
|
2018-02-08 02:26:26 +01:00
|
|
|
* @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()
|
|
|
|
)) {
|
2018-02-08 02:26:26 +01:00
|
|
|
// 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
|
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
|
2018-04-19 18:16:00 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($stmt->name->name === 'class') {
|
2018-04-04 04:20:00 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:18:34 +01:00
|
|
|
$class_constants = $codebase->classlikes->getConstantsForClass(
|
2018-01-14 18:09:40 +01:00
|
|
|
$fq_class_name,
|
|
|
|
$class_visibility
|
|
|
|
);
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
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) {
|
2018-02-09 00:18:34 +01:00
|
|
|
$all_class_constants = $codebase->classlikes->getConstantsForClass(
|
2018-01-14 18:09:40 +01:00
|
|
|
$fq_class_name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($all_class_constants && isset($all_class_constants[$stmt->name->name])) {
|
2018-01-14 18:09:40 +01:00
|
|
|
IssueBuffer::add(
|
|
|
|
new InaccessibleClassConstant(
|
|
|
|
'Constant ' . $const_id . ' is not visible in this context',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
IssueBuffer::add(
|
|
|
|
new UndefinedConstant(
|
|
|
|
'Constant ' . $const_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
$stmt->inferredType = isset($class_constants[$stmt->name->name])
|
2018-01-25 16:47:15 +01:00
|
|
|
&& $first_part_lc !== 'static'
|
2018-04-17 18:16:25 +02:00
|
|
|
? $class_constants[$stmt->name->name]
|
2018-01-24 19:50:33 +01:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
}
|