Skip to content

Commit a992300

Browse files
fix call Non-static method statically (#2788)
* fix call Non-static method (__handlePropertyHandler) statically * Fixed test cases. * Update CHANGELOG-2.0.md Co-authored-by: 李铭昕 <[email protected]>
1 parent 980d859 commit a992300

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Aop/PropertyHandlerVisitor.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public function leaveNode(Node $node)
6565
if ($this->visitorMetadata->hasExtends) {
6666
$constructor->stmts[] = $this->buildCallParentConstructorStatement();
6767
}
68-
$constructor->stmts[] = $this->buildStaticCallStatement();
68+
$constructor->stmts[] = $this->buildMethodCallStatement();
6969
$node->stmts = array_merge([$this->buildProxyTraitUseStatement()], [$constructor], $node->stmts);
7070
$this->visitorMetadata->hasConstructor = true;
7171
} else {
7272
if ($node instanceof Node\Stmt\ClassMethod && $node->name->toString() === '__construct') {
73-
$node->stmts = array_merge([$this->buildStaticCallStatement()], $node->stmts);
73+
$node->stmts = array_merge([$this->buildMethodCallStatement()], $node->stmts);
7474
}
7575
if ($node instanceof Node\Stmt\Class_ && ! $node->isAnonymous()) {
7676
$node->stmts = array_merge([$this->buildProxyTraitUseStatement()], $node->stmts);
@@ -115,9 +115,9 @@ protected function buildCallParentConstructorStatement(): Node\Stmt
115115
]);
116116
}
117117

118-
protected function buildStaticCallStatement(): Node\Stmt\Expression
118+
protected function buildMethodCallStatement(): Node\Stmt\Expression
119119
{
120-
return new Node\Stmt\Expression(new Node\Expr\StaticCall(new Name('self'), '__handlePropertyHandler', [
120+
return new Node\Stmt\Expression(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), '__handlePropertyHandler', [
121121
new Node\Arg(new Node\Scalar\MagicConst\Class_()),
122122
]));
123123
}

tests/AstTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Foo
6363
use \Hyperf\Di\Aop\PropertyHandlerTrait;
6464
function __construct()
6565
{
66-
self::__handlePropertyHandler(__CLASS__);
66+
$this->__handlePropertyHandler(__CLASS__);
6767
}
6868
}', $code);
6969
}
@@ -83,7 +83,7 @@ class Bar2 extends Bar
8383
use \Hyperf\Di\Aop\PropertyHandlerTrait;
8484
public function __construct(int $id)
8585
{
86-
self::__handlePropertyHandler(__CLASS__);
86+
$this->__handlePropertyHandler(__CLASS__);
8787
parent::__construct($id);
8888
}
8989
public static function build()
@@ -112,7 +112,7 @@ public function getBar() : Bar
112112
{
113113
public function __construct()
114114
{
115-
self::__handlePropertyHandler(__CLASS__);
115+
$this->__handlePropertyHandler(__CLASS__);
116116
$this->id = 9501;
117117
}
118118
};
@@ -141,7 +141,7 @@ class Bar4
141141
use \Hyperf\Di\Aop\PropertyHandlerTrait;
142142
function __construct()
143143
{
144-
self::__handlePropertyHandler(__CLASS__);
144+
$this->__handlePropertyHandler(__CLASS__);
145145
}
146146
public function toMethodString() : string
147147
{
@@ -185,7 +185,7 @@ function __construct(int $id)
185185
if (method_exists(parent::class, \'__construct\')) {
186186
parent::__construct(...func_get_args());
187187
}
188-
self::__handlePropertyHandler(__CLASS__);
188+
$this->__handlePropertyHandler(__CLASS__);
189189
}
190190
public function getId() : int
191191
{

0 commit comments

Comments
 (0)