Skip to content

Commit c7176ad

Browse files
committed
Fixed __FUNCTION__ and __METHOD__ magic constants does not working as expected
1 parent 74821bb commit c7176ad

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Aop/ProxyCallVistor.php

+20
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
namespace Hyperf\Di\Aop;
1414

1515
use PhpParser\Node;
16+
use PhpParser\Node\Expr\Assign;
1617
use PhpParser\Node\Expr\ClassConstFetch;
1718
use PhpParser\Node\Expr\Closure;
1819
use PhpParser\Node\Expr\FuncCall;
1920
use PhpParser\Node\Expr\StaticCall;
2021
use PhpParser\Node\Expr\StaticPropertyFetch;
22+
use PhpParser\Node\Expr\Variable;
2123
use PhpParser\Node\Identifier;
2224
use PhpParser\Node\Name;
2325
use PhpParser\Node\Scalar\MagicConst\Function_ as MagicConstFunction;
@@ -137,6 +139,14 @@ public function leaveNode(Node $node)
137139
return $node;
138140
}
139141
break;
142+
case $node instanceof Node\Scalar\MagicConst\Function_:
143+
// Rewrite __FUNCTION__ to $__function__ variable.
144+
return new Variable('__function__');
145+
break;
146+
case $node instanceof Node\Scalar\MagicConst\Method:
147+
// Rewrite __METHOD__ to $__method__ variable.
148+
return new Variable('__method__');
149+
break;
140150
}
141151
}
142152

@@ -238,15 +248,25 @@ private function rewriteMethod(ClassMethod $node): ClassMethod
238248
}
239249
return $params;
240250
}),
251+
'uses' => [
252+
new Variable('__function__'),
253+
new Variable('__method__'),
254+
],
241255
'stmts' => $node->stmts,
242256
]),
243257
]);
258+
$magicConstFunction = new Expression(new Assign(new Variable('__function__'), new Node\Scalar\MagicConst\Function_()));
259+
$magicConstMethod = new Expression(new Assign(new Variable('__method__'), new Node\Scalar\MagicConst\Method()));
244260
if ($shouldReturn) {
245261
$node->stmts = [
262+
$magicConstFunction,
263+
$magicConstMethod,
246264
new Return_($staticCall),
247265
];
248266
} else {
249267
$node->stmts = [
268+
$magicConstFunction,
269+
$magicConstMethod,
250270
new Expression($staticCall),
251271
];
252272
}

0 commit comments

Comments
 (0)