From ec2ac9e253f906812c89fbbe97ff0c2527a78858 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 17 Sep 2026 09:32:52 +0800 Subject: [PATCH] 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 --- src/Parser/ConstantExpressionTrait.php | 5 ++- .../namespace/magic-function-method.phpt | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/compiler/namespace/magic-function-method.phpt diff --git a/src/Parser/ConstantExpressionTrait.php b/src/Parser/ConstantExpressionTrait.php index 86e6adfa..bd787851 100644 --- a/src/Parser/ConstantExpressionTrait.php +++ b/src/Parser/ConstantExpressionTrait.php @@ -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) . '"'; diff --git a/tests/compiler/namespace/magic-function-method.phpt b/tests/compiler/namespace/magic-function-method.phpt new file mode 100644 index 00000000..4aa4c2ea --- /dev/null +++ b/tests/compiler/namespace/magic-function-method.phpt @@ -0,0 +1,34 @@ +--TEST-- +__FUNCTION__ uses the short name in namespaced methods +--FILE-- +instanceMethod(); + \MagicFunctionMethod\Example::staticMethod(); + \MagicFunctionMethod\namespacedFunction(); + } +} +?> +--EXPECT-- +instanceMethod +MagicFunctionMethod\Example::instanceMethod +staticMethod +MagicFunctionMethod\Example::staticMethod +MagicFunctionMethod\namespacedFunction