1<?php declare(strict_types=1);
2
3namespace PhpParser\Node;
4
5use PhpParser\Node\Stmt\Return_;
6use PhpParser\NodeAbstract;
7
8class PropertyHook extends NodeAbstract implements FunctionLike {
9    /** @var AttributeGroup[] PHP attribute groups */
10    public array $attrGroups;
11    /** @var int Modifiers */
12    public int $flags;
13    /** @var bool Whether hook returns by reference */
14    public bool $byRef;
15    /** @var Identifier Hook name */
16    public Identifier $name;
17    /** @var Param[] Parameters */
18    public array $params;
19    /** @var null|Expr|Stmt[] Hook body */
20    public $body;
21
22    /**
23     * Constructs a property hook node.
24     *
25     * @param string|Identifier $name Hook name
26     * @param null|Expr|Stmt[] $body Hook body
27     * @param array{
28     *     flags?: int,
29     *     byRef?: bool,
30     *     params?: Param[],
31     *     attrGroups?: AttributeGroup[],
32     * } $subNodes Array of the following optional subnodes:
33     *             'byRef'      => false  : Whether hook returns by reference
34     *             'params'     => array(): Parameters
35     *             'attrGroups' => array(): PHP attribute groups
36     * @param array<string, mixed> $attributes Additional attributes
37     */
38    public function __construct($name, $body, array $subNodes = [], array $attributes = []) {
39        $this->attributes = $attributes;
40        $this->name = \is_string($name) ? new Identifier($name) : $name;
41        $this->body = $body;
42        $this->flags = $subNodes['flags'] ?? 0;
43        $this->byRef = $subNodes['byRef'] ?? false;
44        $this->params = $subNodes['params'] ?? [];
45        $this->attrGroups = $subNodes['attrGroups'] ?? [];
46    }
47
48    public function returnsByRef(): bool {
49        return $this->byRef;
50    }
51
52    public function getParams(): array {
53        return $this->params;
54    }
55
56    public function getReturnType() {
57        return null;
58    }
59
60    public function getStmts(): ?array {
61        if ($this->body instanceof Expr) {
62            return [new Return_($this->body)];
63        }
64        return $this->body;
65    }
66
67    public function getAttrGroups(): array {
68        return $this->attrGroups;
69    }
70
71    public function getType(): string {
72        return 'PropertyHook';
73    }
74
75    public function getSubNodeNames(): array {
76        return ['attrGroups', 'flags', 'byRef', 'name', 'params', 'body'];
77    }
78}
79