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: /** @var AttributeGroup[] PHP attribute groups */
15: public array $attrGroups;
16: /** @var int Modifiers */
17: public int $flags;
18: /** @var bool Whether hook returns by reference */
19: public bool $byRef;
20: /** @var Identifier Hook name */
21: public Identifier $name;
22: /** @var Param[] Parameters */
23: public array $params;
24: /** @var null|Expr|Stmt[] Hook body */
25: public $body;
26:
27: /**
28: * Constructs a property hook node.
29: *
30: * @param string|Identifier $name Hook name
31: * @param null|Expr|Stmt[] $body Hook body
32: * @param array{
33: * flags?: int,
34: * byRef?: bool,
35: * params?: Param[],
36: * attrGroups?: AttributeGroup[],
37: * } $subNodes Array of the following optional subnodes:
38: * 'flags => 0 : Flags
39: * 'byRef' => false : Whether hook returns by reference
40: * 'params' => array(): Parameters
41: * 'attrGroups' => array(): PHP attribute groups
42: * @param array<string, mixed> $attributes Additional attributes
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: * Whether the property hook is final.
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: