Skip to content

Commit 48fd76e

Browse files
committed
Make PropertyHook::getStmts() less incorrect
Still missing the assignment to the correct property, but at least we're not returning from a non-void function now...
1 parent d20a197 commit 48fd76e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/PhpParser/Node/PropertyHook.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpParser\Node;
44

55
use PhpParser\Modifiers;
6+
use PhpParser\Node\Stmt\Expression;
67
use PhpParser\Node\Stmt\Return_;
78
use PhpParser\NodeAbstract;
89

@@ -68,7 +69,15 @@ public function isFinal(): bool {
6869

6970
public function getStmts(): ?array {
7071
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 . '"');
7281
}
7382
return $this->body;
7483
}

test/PhpParser/Node/PropertyHookTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace PhpParser\Node;
44

55
use PhpParser\Modifiers;
6+
use PhpParser\Node\Expr\Variable;
7+
use PhpParser\Node\Stmt\Expression;
8+
use PhpParser\Node\Stmt\Return_;
69

710
class PropertyHookTest extends \PHPUnit\Framework\TestCase {
811
/**
@@ -31,4 +34,24 @@ public static function provideModifiers() {
3134
['final'],
3235
];
3336
}
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+
}
3457
}

0 commit comments

Comments
 (0)