Skip to content

Commit efb3c93

Browse files
committed
Fixed bug that the private property which injected by parent class does not exists in class.
1 parent c8732d2 commit efb3c93

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/Aop/PropertyHandlerTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function __handlePropertyHandler(string $className)
5454
$parentReflectionClass = $reflectionClass;
5555
while ($parentReflectionClass = $parentReflectionClass->getParentClass()) {
5656
$parentClassProperties = ReflectionManager::reflectPropertyNames($parentReflectionClass->getName());
57-
$parentClassProperties = array_filter($parentClassProperties, static function($property) use ($reflectionClass){
57+
$parentClassProperties = array_filter($parentClassProperties, static function ($property) use ($reflectionClass) {
5858
return $reflectionClass->hasProperty($property);
5959
});
6060
$parentClassProperties = array_diff($parentClassProperties, $handled);

tests/InjectTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
use HyperfTest\Di\Stub\Inject\Origin3Class;
3737
use HyperfTest\Di\Stub\Inject\Origin4Class;
3838
use HyperfTest\Di\Stub\Inject\Origin5Class;
39+
use HyperfTest\Di\Stub\Inject\Origin6Class;
3940
use HyperfTest\Di\Stub\Inject\OriginClass;
4041
use HyperfTest\Di\Stub\Inject\Parent2Class;
4142
use HyperfTest\Di\Stub\Inject\Parent3Class;
43+
use HyperfTest\Di\Stub\Inject\Parent4Class;
4244
use HyperfTest\Di\Stub\Inject\ParentClass;
4345
use HyperfTest\Di\Stub\Inject\Tar;
4446
use Mockery;
@@ -222,6 +224,31 @@ public function testInjectParentNoRunParent()
222224
$this->assertInstanceOf(Foo::class, $origin->getFoo());
223225
}
224226

227+
public function testInjectParentPrivateProperty()
228+
{
229+
$this->getContainer();
230+
$ast = new Ast();
231+
$classes = [
232+
Parent4Class::class,
233+
Origin6Class::class,
234+
];
235+
236+
if (! is_dir($dir = BASE_PATH . '/runtime/container/proxy/')) {
237+
mkdir($dir, 0777, true);
238+
}
239+
240+
foreach ($classes as $class) {
241+
$code = $ast->proxy($class);
242+
$id = md5($class);
243+
file_put_contents($file = $dir . $id . '.proxy.php', $code);
244+
require_once $file;
245+
}
246+
247+
$origin = new Origin6Class();
248+
$this->assertInstanceOf(Foo::class, $origin->getFoo());
249+
$this->assertInstanceOf(Bar::class, $origin->getBar());
250+
}
251+
225252
public function testInjectException()
226253
{
227254
$this->getContainer();
@@ -283,6 +310,8 @@ protected function getContainer()
283310
Origin5Class::class,
284311
Parent2Class::class,
285312
Parent3Class::class,
313+
Parent4Class::class,
314+
Origin6Class::class,
286315
];
287316

288317
foreach ($classes as $class) {

tests/Stub/Inject/Origin6Class.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Inject;
13+
14+
use Hyperf\Di\Annotation\Inject;
15+
16+
class Origin6Class extends Parent4Class
17+
{
18+
/**
19+
* @Inject
20+
* @var Bar
21+
*/
22+
private $bar;
23+
24+
public function getBar()
25+
{
26+
return $this->bar;
27+
}
28+
}

tests/Stub/Inject/Parent4Class.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Inject;
13+
14+
use Hyperf\Di\Annotation\Inject;
15+
16+
class Parent4Class
17+
{
18+
/**
19+
* @Inject
20+
* @var Foo
21+
*/
22+
private $foo;
23+
24+
public function getFoo()
25+
{
26+
return $this->foo;
27+
}
28+
}

0 commit comments

Comments
 (0)