| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser\Node; |
| 4: | |
| 5: | use PhpParser\Modifiers; |
| 6: | use PhpParser\Node\Expr\Assign; |
| 7: | use PhpParser\Node\Expr\PropertyFetch; |
| 8: | use PhpParser\Node\Expr\Variable; |
| 9: | use PhpParser\Node\Stmt\Expression; |
| 10: | use PhpParser\Node\Stmt\Return_; |
| 11: | use PhpParser\NodeAbstract; |
| 12: | |
| 13: | class PropertyHook extends NodeAbstract implements FunctionLike { |
| 14: | |
| 15: | public array $attrGroups; |
| 16: | |
| 17: | public int $flags; |
| 18: | |
| 19: | public bool $byRef; |
| 20: | |
| 21: | public Identifier $name; |
| 22: | |
| 23: | public array $params; |
| 24: | |
| 25: | public $body; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function __construct($name, $body, array $subNodes = [], array $attributes = []) { |
| 45: | $this->attributes = $attributes; |
| 46: | $this->name = \is_string($name) ? new Identifier($name) : $name; |
| 47: | $this->body = $body; |
| 48: | $this->flags = $subNodes['flags'] ?? 0; |
| 49: | $this->byRef = $subNodes['byRef'] ?? false; |
| 50: | $this->params = $subNodes['params'] ?? []; |
| 51: | $this->attrGroups = $subNodes['attrGroups'] ?? []; |
| 52: | } |
| 53: | |
| 54: | public function returnsByRef(): bool { |
| 55: | return $this->byRef; |
| 56: | } |
| 57: | |
| 58: | public function getParams(): array { |
| 59: | return $this->params; |
| 60: | } |
| 61: | |
| 62: | public function getReturnType() { |
| 63: | return null; |
| 64: | } |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function isFinal(): bool { |
| 70: | return (bool) ($this->flags & Modifiers::FINAL); |
| 71: | } |
| 72: | |
| 73: | public function getStmts(): ?array { |
| 74: | if ($this->body instanceof Expr) { |
| 75: | $name = $this->name->toLowerString(); |
| 76: | if ($name === 'get') { |
| 77: | return [new Return_($this->body)]; |
| 78: | } |
| 79: | if ($name === 'set') { |
| 80: | if (!$this->hasAttribute('propertyName')) { |
| 81: | throw new \LogicException( |
| 82: | 'Can only use getStmts() on a "set" hook if the "propertyName" attribute is set'); |
| 83: | } |
| 84: | |
| 85: | $propName = $this->getAttribute('propertyName'); |
| 86: | $prop = new PropertyFetch(new Variable('this'), (string) $propName); |
| 87: | return [new Expression(new Assign($prop, $this->body))]; |
| 88: | } |
| 89: | throw new \LogicException('Unknown property hook "' . $name . '"'); |
| 90: | } |
| 91: | return $this->body; |
| 92: | } |
| 93: | |
| 94: | public function getAttrGroups(): array { |
| 95: | return $this->attrGroups; |
| 96: | } |
| 97: | |
| 98: | public function getType(): string { |
| 99: | return 'PropertyHook'; |
| 100: | } |
| 101: | |
| 102: | public function getSubNodeNames(): array { |
| 103: | return ['attrGroups', 'flags', 'byRef', 'name', 'params', 'body']; |
| 104: | } |
| 105: | } |
| 106: | |