1
0
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:
Bruce Weirdan 2021-07-04 04:09:47 +03:00 committed by GitHub
commit 1466c3a3fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View File

@ -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]));
}

View File

@ -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__;
}
}'
],
];
}