Skip to content

Commit

Permalink
Make PropertyHook::getStmts() less incorrect
Browse files Browse the repository at this point in the history
Still missing the assignment to the correct property, but at
least we're not returning from a non-void function now...
  • Loading branch information
nikic committed Dec 27, 2024
1 parent d20a197 commit 48fd76e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/PhpParser/Node/PropertyHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpParser\Node;

use PhpParser\Modifiers;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeAbstract;

Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions test/PhpParser/Node/PropertyHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 48fd76e

Please sign in to comment.