fix(parser): correct __FUNCTION__ constant resolution in namespaced methods

- Fixed __FUNCTION__ to use short name instead of full namespaced name in method context
- Added conditional logic to prepend namespace only when not in method definition
- Added test case to verify proper behavior of __FUNCTION__ and __METHOD__ constants
- Ensured magic constants return correct values based on their usage context
- Updated parser logic to distinguish between function and method definitions properly
master
韩天峰 3 weeks ago
parent 9bc716a54c
commit ec2ac9e253
  1. 5
      src/Parser/ConstantExpressionTrait.php
  2. 34
      tests/compiler/namespace/magic-function-method.phpt

@ -139,7 +139,10 @@ trait ConstantExpressionTrait
{
$class = $this->classDef?->getNamespacedName(false)
?? (($this->namespace ? $this->namespace . '\\' : '') . $this->class);
$function = ($this->namespace ? $this->namespace . '\\' : '') . $this->function;
$function = $this->function;
if ($function !== '' && $this->methodDef === null && $this->namespace !== '') {
$function = $this->namespace . '\\' . $function;
}
switch ($expr->getType()) {
case 'Scalar_MagicConst_Dir':
return '"' . $this->escapeString($this->dir) . '"';

@ -0,0 +1,34 @@
--TEST--
__FUNCTION__ uses the short name in namespaced methods
--FILE--
<?php
namespace MagicFunctionMethod {
class Example {
public function instanceMethod(): void {
echo __FUNCTION__, "\n", __METHOD__, "\n";
}
public static function staticMethod(): void {
echo __FUNCTION__, "\n", __METHOD__, "\n";
}
}
function namespacedFunction(): void {
echo __FUNCTION__, "\n";
}
}
namespace {
function main(): void {
(new \MagicFunctionMethod\Example())->instanceMethod();
\MagicFunctionMethod\Example::staticMethod();
\MagicFunctionMethod\namespacedFunction();
}
}
?>
--EXPECT--
instanceMethod
MagicFunctionMethod\Example::instanceMethod
staticMethod
MagicFunctionMethod\Example::staticMethod
MagicFunctionMethod\namespacedFunction
Loading…
Cancel
Save