1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 14:38:37 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/MagicConstAnalyzer.php

109 lines
4.4 KiB
PHP
Raw Normal View History

2020-05-18 21:13:27 +02:00
<?php
2020-05-18 21:13:27 +02:00
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FunctionAnalyzer;
use Psalm\Internal\Analyzer\MethodAnalyzer;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\Analyzer\StatementsAnalyzer;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Analyzer\TraitAnalyzer;
2020-05-18 21:13:27 +02:00
use Psalm\Issue\UndefinedConstant;
use Psalm\IssueBuffer;
use Psalm\Type;
2021-12-13 04:45:57 +01:00
use Psalm\Type\Atomic\TCallableString;
use Psalm\Type\Atomic\TNonEmptyString;
2021-12-13 16:28:14 +01:00
use Psalm\Type\Union;
2020-05-18 21:13:27 +02:00
use function dirname;
2020-05-18 21:13:27 +02:00
2022-01-03 07:55:32 +01:00
/**
* @internal
*/
2020-05-18 21:13:27 +02:00
class MagicConstAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Scalar\MagicConst $stmt,
Context $context
): void {
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Line) {
$statements_analyzer->node_data->setType($stmt, Type::getInt());
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Class_) {
$codebase = $statements_analyzer->getCodebase();
if (!$context->self) {
IssueBuffer::maybeAdd(
2020-05-18 21:13:27 +02:00
new UndefinedConstant(
'Cannot get __class__ outside a class',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
2020-05-18 21:13:27 +02:00
$statements_analyzer->node_data->setType($stmt, Type::getClassString());
} else {
if ($codebase->alter_code) {
$codebase->classlikes->handleClassLikeReferenceInMigration(
$codebase,
$statements_analyzer,
$stmt,
$context->self,
$context->calling_method_id
);
}
$statements_analyzer->node_data->setType($stmt, Type::getLiteralClassString($context->self));
}
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Namespace_) {
$namespace = $statements_analyzer->getNamespace();
2022-12-17 05:00:34 +01:00
if ($namespace === null) {
IssueBuffer::maybeAdd(
2020-05-18 21:13:27 +02:00
new UndefinedConstant(
'Cannot get __namespace__ outside a namespace',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
2022-12-17 05:00:34 +01:00
);
2020-05-18 21:13:27 +02:00
}
$statements_analyzer->node_data->setType($stmt, Type::getString($namespace));
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Method
|| $stmt instanceof PhpParser\Node\Scalar\MagicConst\Function_
) {
$source = $statements_analyzer->getSource();
if ($source instanceof MethodAnalyzer) {
if ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Function_) {
$statements_analyzer->node_data->setType($stmt, Type::getString($source->getMethodName()));
} else {
$statements_analyzer->node_data->setType(
$stmt,
Type::getString($source->getCorrectlyCasedMethodId())
);
}
} elseif ($source instanceof FunctionAnalyzer) {
$statements_analyzer->node_data->setType($stmt, Type::getString($source->getCorrectlyCasedMethodId()));
2020-05-18 21:13:27 +02:00
} else {
2021-12-13 16:28:14 +01:00
$statements_analyzer->node_data->setType($stmt, new Union([new TCallableString]));
2020-05-18 21:13:27 +02:00
}
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Dir) {
$statements_analyzer->node_data->setType(
$stmt,
Type::getString(dirname($statements_analyzer->getSource()->getFilePath()))
);
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\File) {
$statements_analyzer->node_data->setType(
$stmt,
Type::getString($statements_analyzer->getSource()->getFilePath())
);
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Trait_) {
2021-12-03 20:11:20 +01:00
if ($statements_analyzer->getSource() instanceof TraitAnalyzer) {
2021-12-13 16:28:14 +01:00
$statements_analyzer->node_data->setType($stmt, new Union([new TNonEmptyString()]));
} else {
$statements_analyzer->node_data->setType($stmt, Type::getString());
}
2020-05-18 21:13:27 +02:00
}
}
}