2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\Fetch;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\TraitAnalyzer;
|
2018-07-06 19:35:36 +02:00
|
|
|
use Psalm\Codebase;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2018-08-10 19:25:25 +02:00
|
|
|
use Psalm\Issue\DeprecatedConstant;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\InaccessibleClassConstant;
|
|
|
|
use Psalm\Issue\ParentNotFound;
|
|
|
|
use Psalm\Issue\UndefinedConstant;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class ConstFetchAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @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(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\ConstFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$const_name = implode('\\', $stmt->name->parts);
|
2019-03-03 22:43:24 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
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:
|
2018-11-11 18:01:14 +01:00
|
|
|
$const_type = $statements_analyzer->getConstType(
|
2018-01-14 18:09:40 +01:00
|
|
|
$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',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
2018-02-08 02:26:26 +01:00
|
|
|
// fall through
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr\ClassConstFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
public static function analyzeClassConst(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\ClassConstFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
if ($context->check_consts
|
|
|
|
|| ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class')
|
|
|
|
) {
|
|
|
|
$first_part_lc = strtolower($stmt->class->parts[0]);
|
2018-01-23 21:46:14 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if ($first_part_lc === 'self' || $first_part_lc === 'static') {
|
|
|
|
if (!$context->self) {
|
|
|
|
throw new \UnexpectedValueException('$context->self cannot be null');
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
$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;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
return;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2019-01-05 21:12:42 +01:00
|
|
|
} else {
|
|
|
|
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
|
|
|
$stmt->class,
|
|
|
|
$statements_analyzer->getAliases()
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
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(),
|
2019-03-16 03:12:35 +01:00
|
|
|
false,
|
|
|
|
true
|
2019-01-05 21:12:42 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
2018-04-19 18:16:00 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
|
|
|
|
$stmt->inferredType = Type::getLiteralClassString($fq_class_name);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->class,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
// if we're ignoring that the class doesn't exist, exit anyway
|
|
|
|
if (!$codebase->classOrInterfaceExists($fq_class_name)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types) {
|
2019-01-05 21:12:42 +01:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->class,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$const_id = $fq_class_name . '::' . $stmt->name;
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types) {
|
2019-01-05 21:12:42 +01:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$const_id
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
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
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if (!isset($class_constants[$stmt->name->name]) && $first_part_lc !== 'static') {
|
|
|
|
$all_class_constants = [];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if ($fq_class_name !== $context->self) {
|
|
|
|
$all_class_constants = $codebase->classlikes->getConstantsForClass(
|
|
|
|
$fq_class_name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedConstant(
|
|
|
|
'Constant ' . $const_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if ($context->calling_method_id) {
|
2019-04-16 22:07:48 +02:00
|
|
|
$codebase->file_reference_provider->addMethodReferenceToClassMember(
|
2019-01-05 21:12:42 +01:00
|
|
|
$context->calling_method_id,
|
|
|
|
strtolower($fq_class_name) . '::' . $stmt->name->name
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
$class_const_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
if (isset($class_const_storage->deprecated_constants[$stmt->name->name])) {
|
2018-05-03 19:20:42 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2019-01-05 21:12:42 +01:00
|
|
|
new DeprecatedConstant(
|
|
|
|
'Constant ' . $const_id . ' is deprecated',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-05-03 19:20:42 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-03 19:20:42 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
if (isset($class_constants[$stmt->name->name]) && $first_part_lc !== 'static') {
|
|
|
|
$stmt->inferredType = clone $class_constants[$stmt->name->name];
|
2019-01-13 17:54:39 +01:00
|
|
|
$context->vars_in_scope[$const_id] = $stmt->inferredType;
|
2019-01-05 21:12:42 +01:00
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2018-08-10 19:25:25 +02:00
|
|
|
}
|
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
return null;
|
2018-05-12 05:14:44 +02:00
|
|
|
}
|
2019-01-05 21:12:42 +01:00
|
|
|
} elseif ($stmt->name instanceof PhpParser\Node\Identifier && $stmt->name->name === 'class') {
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context);
|
|
|
|
$lhs_type = $stmt->class->inferredType;
|
|
|
|
|
2019-01-20 04:45:58 +01:00
|
|
|
$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();
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-05 21:12:42 +01:00
|
|
|
return;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-06 19:35:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Codebase $codebase
|
2018-07-06 20:14:24 +02:00
|
|
|
* @param ?string $fq_const_name
|
2018-07-06 19:35:36 +02:00
|
|
|
* @param string $const_name
|
|
|
|
*
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
|
|
|
public static function getGlobalConstType(
|
|
|
|
Codebase $codebase,
|
|
|
|
$fq_const_name,
|
|
|
|
$const_name
|
|
|
|
) {
|
2018-07-10 23:40:34 +02:00
|
|
|
if ($const_name === 'STDERR'
|
|
|
|
|| $const_name === 'STDOUT'
|
|
|
|
|| $const_name === 'STDIN'
|
|
|
|
) {
|
|
|
|
return Type::getResource();
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:35:36 +02:00
|
|
|
$predefined_constants = $codebase->config->getPredefinedConstants();
|
|
|
|
|
2019-01-20 15:52:26 +01:00
|
|
|
if (isset($predefined_constants[$fq_const_name])
|
|
|
|
|| isset($predefined_constants[$const_name])
|
|
|
|
) {
|
|
|
|
switch ($const_name) {
|
2018-07-06 19:35:36 +02:00
|
|
|
case 'PHP_VERSION':
|
|
|
|
case 'DIRECTORY_SEPARATOR':
|
|
|
|
case 'PATH_SEPARATOR':
|
|
|
|
case 'PEAR_EXTENSION_DIR':
|
|
|
|
case 'PEAR_INSTALL_DIR':
|
|
|
|
case 'PHP_BINARY':
|
|
|
|
case 'PHP_BINDIR':
|
|
|
|
case 'PHP_CONFIG_FILE_PATH':
|
|
|
|
case 'PHP_CONFIG_FILE_SCAN_DIR':
|
|
|
|
case 'PHP_DATADIR':
|
|
|
|
case 'PHP_EOL':
|
|
|
|
case 'PHP_EXTENSION_DIR':
|
|
|
|
case 'PHP_EXTRA_VERSION':
|
|
|
|
case 'PHP_LIBDIR':
|
|
|
|
case 'PHP_LOCALSTATEDIR':
|
|
|
|
case 'PHP_MANDIR':
|
|
|
|
case 'PHP_OS':
|
|
|
|
case 'PHP_OS_FAMILY':
|
|
|
|
case 'PHP_PREFIX':
|
|
|
|
case 'PHP_SAPI':
|
|
|
|
case 'PHP_SYSCONFDIR':
|
|
|
|
return Type::getString();
|
|
|
|
|
|
|
|
case 'PHP_MAJOR_VERSION':
|
|
|
|
case 'PHP_MINOR_VERSION':
|
|
|
|
case 'PHP_RELEASE_VERSION':
|
|
|
|
case 'PHP_DEBUG':
|
|
|
|
case 'PHP_FLOAT_DIG':
|
|
|
|
case 'PHP_INT_MAX':
|
|
|
|
case 'PHP_INT_MIN':
|
|
|
|
case 'PHP_INT_SIZE':
|
|
|
|
case 'PHP_MAXPATHLEN':
|
|
|
|
case 'PHP_VERSION_ID':
|
|
|
|
case 'PHP_ZTS':
|
|
|
|
return Type::getInt();
|
|
|
|
|
|
|
|
case 'PHP_FLOAT_EPSILON':
|
|
|
|
case 'PHP_FLOAT_MAX':
|
|
|
|
case 'PHP_FLOAT_MIN':
|
|
|
|
return Type::getFloat();
|
|
|
|
}
|
|
|
|
|
2019-01-20 15:52:26 +01:00
|
|
|
if (isset($predefined_constants[$fq_const_name])) {
|
|
|
|
return ClassLikeAnalyzer::getTypeFromValue($predefined_constants[$fq_const_name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ClassLikeAnalyzer::getTypeFromValue($predefined_constants[$const_name]);
|
2018-07-06 19:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$stubbed_const_type = $codebase->getStubbedConstantType(
|
|
|
|
$fq_const_name ?: $const_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($stubbed_const_type) {
|
|
|
|
return $stubbed_const_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|