Skip to content

Commit a6b0bd1

Browse files
Fixed bug that ProxyTrait::__getParamsMap can not work when using trait alias. (#4885)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 108ad90 commit a6b0bd1

7 files changed

+108
-5
lines changed

src/Aop/ProxyCallVisitor.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ private function rewriteMethod(ClassMethod $node): ClassMethod
158158
new Arg($this->getMagicConst()),
159159
// __FUNCTION__
160160
new Arg(new MagicConstFunction()),
161-
// self::getParamMap(OriginalClass::class, __FUNCTION, func_get_args())
161+
// self::getParamMap(__CLASS__, __FUNCTION__, func_get_args())
162+
// self::getParamMap(__TRAIT__, __FUNCTION__, func_get_args())
162163
new Arg(new StaticCall(new Name('self'), '__getParamsMap', [
163-
new Arg(new MagicConstClass()),
164+
new Arg($this->getMagicConst()),
164165
new Arg(new MagicConstFunction()),
165166
new Arg(new FuncCall(new Name('func_get_args'))),
166167
])),

src/ReflectionManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function reflectMethod(string $className, string $method): Reflect
3838
$key = $className . '::' . $method;
3939
if (! isset(static::$container['method'][$key])) {
4040
// TODO check interface_exist
41-
if (! class_exists($className)) {
41+
if (! class_exists($className) && ! trait_exists($className)) {
4242
throw new InvalidArgumentException("Class {$className} not exist");
4343
}
4444
static::$container['method'][$key] = static::reflectClass($className)->getMethod($method);

tests/AstTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function getString() : string
238238
{
239239
$__function__ = __FUNCTION__;
240240
$__method__ = __METHOD__;
241-
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__CLASS__, __FUNCTION__, func_get_args()), function () use($__function__, $__method__) {
241+
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__TRAIT__, __FUNCTION__, func_get_args()), function () use($__function__, $__method__) {
242242
return uniqid();
243243
});
244244
}
@@ -253,7 +253,7 @@ public function getString() : string
253253
{
254254
$__function__ = __FUNCTION__;
255255
$__method__ = __METHOD__;
256-
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__CLASS__, __FUNCTION__, func_get_args()), function () use($__function__, $__method__) {
256+
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__TRAIT__, __FUNCTION__, func_get_args()), function () use($__function__, $__method__) {
257257
return uniqid();
258258
});
259259
}

tests/ProxyTraitTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ public function testGetParamsMap()
4848
$this->assertEquals(['id', 'str', 'num'], $obj->get3(1, 'hy')['order']);
4949
}
5050

51+
public function testGetParamsMapOnTraitAlias()
52+
{
53+
$obj = new ProxyTraitObject();
54+
55+
$this->assertEquals(['id' => null, 'str' => ''], $obj->getOnTrait(null)['keys']);
56+
$this->assertEquals(['id', 'str'], $obj->getOnTrait(null)['order']);
57+
58+
$this->assertEquals(['id' => 1, 'str' => ''], $obj->get2OnTrait()['keys']);
59+
$this->assertEquals(['id', 'str'], $obj->get2OnTrait()['order']);
60+
61+
$this->assertEquals(['id' => null, 'str' => ''], $obj->get2OnTrait(null)['keys']);
62+
$this->assertEquals(['id', 'str'], $obj->get2OnTrait(null)['order']);
63+
64+
$this->assertEquals(['id' => 1, 'str' => '', 'num' => 1.0], $obj->get3OnTrait()['keys']);
65+
$this->assertEquals(['id', 'str', 'num'], $obj->get3OnTrait()['order']);
66+
67+
$this->assertEquals(['id' => 1, 'str' => 'hy', 'num' => 1.0], $obj->get3OnTrait(1, 'hy')['keys']);
68+
$this->assertEquals(['id', 'str', 'num'], $obj->get3OnTrait(1, 'hy')['order']);
69+
}
70+
5171
public function testProceedingJoinPointGetInstance()
5272
{
5373
$aspect = [];

tests/ReflectionTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Hyperf\Di\ReflectionManager;
1515
use HyperfTest\Di\Stub\Ast\Bar;
16+
use HyperfTest\Di\Stub\Ast\FooTrait;
1617
use HyperfTest\Di\Stub\Foo;
1718
use HyperfTest\Di\Stub\FooInterface;
1819
use HyperfTest\Di\Stub\Inject\Foo3Trait;
@@ -89,6 +90,14 @@ public function testReflectionMethod()
8990
$this->assertSame(ReflectionManager::getContainer()['method'][Foo::class . '::getFoo'], $reflection);
9091
}
9192

93+
public function testReflectionTraitMethod()
94+
{
95+
$reflection = ReflectionManager::reflectMethod(FooTrait::class, 'getString');
96+
$this->assertInstanceOf(ReflectionMethod::class, $reflection);
97+
$this->assertTrue($reflection->isPublic());
98+
$this->assertSame(ReflectionManager::getContainer()['method'][FooTrait::class . '::getString'], $reflection);
99+
}
100+
92101
public function testReflectionManagerGetAllClasses()
93102
{
94103
$reflections = ReflectionManager::getAllClasses([__DIR__ . '/Stub']);

tests/Stub/ProxyTraitObject.php

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
class ProxyTraitObject
1717
{
1818
use ProxyTrait;
19+
use ProxyTraitOnTrait {
20+
ProxyTraitOnTrait::get as getOnTrait;
21+
ProxyTraitOnTrait::get2 as get2OnTrait;
22+
ProxyTraitOnTrait::get3 as get3OnTrait;
23+
ProxyTraitOnTrait::incr as incrOnTrait;
24+
ProxyTraitOnTrait::getName as getNameOnTrait;
25+
ProxyTraitOnTrait::getName2 as getName2OnTrait;
26+
}
1927

2028
/**
2129
* @var string

tests/Stub/ProxyTraitOnTrait.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Di\Stub;
13+
14+
use Hyperf\Di\Aop\ProxyTrait;
15+
16+
trait ProxyTraitOnTrait
17+
{
18+
use ProxyTrait;
19+
20+
/**
21+
* @var string
22+
*/
23+
public $name;
24+
25+
public function __construct(string $name = 'Hyperf')
26+
{
27+
$this->name = $name;
28+
}
29+
30+
public function get(?int $id, string $str = '')
31+
{
32+
return $this->__getParamsMap(__TRAIT__, 'get', func_get_args());
33+
}
34+
35+
public function get2(?int $id = 1, string $str = '')
36+
{
37+
return $this->__getParamsMap(__TRAIT__, 'get2', func_get_args());
38+
}
39+
40+
public function get3(?int $id = 1, string $str = '', float $num = 1.0)
41+
{
42+
return $this->__getParamsMap(__TRAIT__, 'get3', func_get_args());
43+
}
44+
45+
public function incr()
46+
{
47+
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__TRAIT__, __FUNCTION__, func_get_args()), function () {
48+
return 1;
49+
});
50+
}
51+
52+
public function getName()
53+
{
54+
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__TRAIT__, __FUNCTION__, func_get_args()), function () {
55+
return 'HyperfCloud';
56+
});
57+
}
58+
59+
public function getName2()
60+
{
61+
return self::__proxyCall(__TRAIT__, __FUNCTION__, self::__getParamsMap(__TRAIT__, __FUNCTION__, func_get_args()), function () {
62+
return 'HyperfCloud';
63+
});
64+
}
65+
}

0 commit comments

Comments
 (0)