diff --git a/lib/PhpParser/Node/PropertyHook.php b/lib/PhpParser/Node/PropertyHook.php index fab0e9d39a..d8a77fe261 100644 --- a/lib/PhpParser/Node/PropertyHook.php +++ b/lib/PhpParser/Node/PropertyHook.php @@ -3,6 +3,7 @@ namespace PhpParser\Node; use PhpParser\Modifiers; +use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\NodeAbstract; @@ -68,7 +69,15 @@ public function isFinal(): bool { public function getStmts(): ?array { if ($this->body instanceof Expr) { - return [new Return_($this->body)]; + $name = $this->name->toLowerString(); + if ($name === 'get') { + return [new Return_($this->body)]; + } + if ($name === 'set') { + // TODO: This should generate $this->prop = $expr, but we don't know the property name. + return [new Expression($this->body)]; + } + throw new \LogicException('Unknown property hook "' . $name . '"'); } return $this->body; } diff --git a/test/PhpParser/Node/PropertyHookTest.php b/test/PhpParser/Node/PropertyHookTest.php index c8bd07cffb..42ca1cd2cf 100644 --- a/test/PhpParser/Node/PropertyHookTest.php +++ b/test/PhpParser/Node/PropertyHookTest.php @@ -3,6 +3,9 @@ namespace PhpParser\Node; use PhpParser\Modifiers; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt\Expression; +use PhpParser\Node\Stmt\Return_; class PropertyHookTest extends \PHPUnit\Framework\TestCase { /** @@ -31,4 +34,24 @@ public static function provideModifiers() { ['final'], ]; } + + public function testGetStmts(): void { + $expr = new Variable('test'); + $get = new PropertyHook('get', $expr); + $this->assertEquals([new Return_($expr)], $get->getStmts()); + + // TODO: This is incorrect. + $set = new PropertyHook('set', $expr); + $this->assertEquals([new Expression($expr)], $set->getStmts()); + } + + public function testSetStmtsUnknownHook(): void + { + $expr = new Variable('test'); + $get = new PropertyHook('foobar', $expr); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Unknown property hook "foobar"'); + $get->getStmts(); + } }