mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 12:55:26 +01:00
Merge pull request #6041 from orklah/magicConstantsResolution
Improve resolution of __METHOD__ and __FUNCTION__
This commit is contained in:
commit
1466c3a3fc
@ -4,7 +4,8 @@ namespace Psalm\Internal\Analyzer\Statements\Expression;
|
||||
use PhpParser;
|
||||
use Psalm\CodeLocation;
|
||||
use Psalm\Context;
|
||||
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
||||
use Psalm\Internal\Analyzer\FunctionAnalyzer;
|
||||
use Psalm\Internal\Analyzer\MethodAnalyzer;
|
||||
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
||||
use Psalm\Issue\UndefinedConstant;
|
||||
use Psalm\IssueBuffer;
|
||||
@ -66,8 +67,17 @@ class MagicConstAnalyzer
|
||||
|| $stmt instanceof PhpParser\Node\Scalar\MagicConst\Function_
|
||||
) {
|
||||
$source = $statements_analyzer->getSource();
|
||||
if ($source instanceof FunctionLikeAnalyzer) {
|
||||
$statements_analyzer->node_data->setType($stmt, Type::getString($source->getId()));
|
||||
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()));
|
||||
} else {
|
||||
$statements_analyzer->node_data->setType($stmt, new Type\Union([new Type\Atomic\TCallableString]));
|
||||
}
|
||||
|
@ -1046,6 +1046,30 @@ class ConstantTest extends TestCase
|
||||
}
|
||||
}'
|
||||
],
|
||||
'FuncAndMethInAllContexts' => [
|
||||
'<?php
|
||||
/** @return \'getMethInFunc\' */
|
||||
function getMethInFunc(): string{
|
||||
return __METHOD__;
|
||||
}
|
||||
|
||||
/** @return \'getFuncInFunc\' */
|
||||
function getFuncInFunc(): string{
|
||||
return __FUNCTION__;
|
||||
}
|
||||
|
||||
class A{
|
||||
/** @return \'A::getMethInMeth\' */
|
||||
function getMethInMeth(): string{
|
||||
return __METHOD__;
|
||||
}
|
||||
|
||||
/** @return \'getFuncInMeth\' */
|
||||
function getFuncInMeth(): string{
|
||||
return __FUNCTION__;
|
||||
}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user