File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 3
3
namespace PhpParser \Node ;
4
4
5
5
use PhpParser \Modifiers ;
6
+ use PhpParser \Node \Stmt \Expression ;
6
7
use PhpParser \Node \Stmt \Return_ ;
7
8
use PhpParser \NodeAbstract ;
8
9
@@ -68,7 +69,15 @@ public function isFinal(): bool {
68
69
69
70
public function getStmts (): ?array {
70
71
if ($ this ->body instanceof Expr) {
71
- return [new Return_ ($ this ->body )];
72
+ $ name = $ this ->name ->toLowerString ();
73
+ if ($ name === 'get ' ) {
74
+ return [new Return_ ($ this ->body )];
75
+ }
76
+ if ($ name === 'set ' ) {
77
+ // TODO: This should generate $this->prop = $expr, but we don't know the property name.
78
+ return [new Expression ($ this ->body )];
79
+ }
80
+ throw new \LogicException ('Unknown property hook " ' . $ name . '" ' );
72
81
}
73
82
return $ this ->body ;
74
83
}
Original file line number Diff line number Diff line change 3
3
namespace PhpParser \Node ;
4
4
5
5
use PhpParser \Modifiers ;
6
+ use PhpParser \Node \Expr \Variable ;
7
+ use PhpParser \Node \Stmt \Expression ;
8
+ use PhpParser \Node \Stmt \Return_ ;
6
9
7
10
class PropertyHookTest extends \PHPUnit \Framework \TestCase {
8
11
/**
@@ -31,4 +34,24 @@ public static function provideModifiers() {
31
34
['final ' ],
32
35
];
33
36
}
37
+
38
+ public function testGetStmts (): void {
39
+ $ expr = new Variable ('test ' );
40
+ $ get = new PropertyHook ('get ' , $ expr );
41
+ $ this ->assertEquals ([new Return_ ($ expr )], $ get ->getStmts ());
42
+
43
+ // TODO: This is incorrect.
44
+ $ set = new PropertyHook ('set ' , $ expr );
45
+ $ this ->assertEquals ([new Expression ($ expr )], $ set ->getStmts ());
46
+ }
47
+
48
+ public function testSetStmtsUnknownHook (): void
49
+ {
50
+ $ expr = new Variable ('test ' );
51
+ $ get = new PropertyHook ('foobar ' , $ expr );
52
+
53
+ $ this ->expectException (\LogicException::class);
54
+ $ this ->expectExceptionMessage ('Unknown property hook "foobar" ' );
55
+ $ get ->getStmts ();
56
+ }
34
57
}
You can’t perform that action at this time.
0 commit comments