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