xref: /PHP-Parser/lib/PhpParser/Node/Param.php (revision 4f9dc8b0)
1<?php declare(strict_types=1);
2
3namespace PhpParser\Node;
4
5use PhpParser\Modifiers;
6use PhpParser\Node;
7use PhpParser\NodeAbstract;
8
9class Param extends NodeAbstract {
10    /** @var null|Identifier|Name|ComplexType Type declaration */
11    public ?Node $type;
12    /** @var bool Whether parameter is passed by reference */
13    public bool $byRef;
14    /** @var bool Whether this is a variadic argument */
15    public bool $variadic;
16    /** @var Expr\Variable|Expr\Error Parameter variable */
17    public Expr $var;
18    /** @var null|Expr Default value */
19    public ?Expr $default;
20    /** @var int Optional visibility flags */
21    public int $flags;
22    /** @var AttributeGroup[] PHP attribute groups */
23    public array $attrGroups;
24    /** @var PropertyHook[] Property hooks for promoted properties */
25    public array $hooks;
26
27    /**
28     * Constructs a parameter node.
29     *
30     * @param Expr\Variable|Expr\Error $var Parameter variable
31     * @param null|Expr $default Default value
32     * @param null|Identifier|Name|ComplexType $type Type declaration
33     * @param bool $byRef Whether is passed by reference
34     * @param bool $variadic Whether this is a variadic argument
35     * @param array<string, mixed> $attributes Additional attributes
36     * @param int $flags Optional visibility flags
37     * @param list<AttributeGroup> $attrGroups PHP attribute groups
38     * @param PropertyHook[] $hooks Property hooks for promoted properties
39     */
40    public function __construct(
41        Expr $var, ?Expr $default = null, ?Node $type = null,
42        bool $byRef = false, bool $variadic = false,
43        array $attributes = [],
44        int $flags = 0,
45        array $attrGroups = [],
46        array $hooks = []
47    ) {
48        $this->attributes = $attributes;
49        $this->type = $type;
50        $this->byRef = $byRef;
51        $this->variadic = $variadic;
52        $this->var = $var;
53        $this->default = $default;
54        $this->flags = $flags;
55        $this->attrGroups = $attrGroups;
56        $this->hooks = $hooks;
57    }
58
59    public function getSubNodeNames(): array {
60        return ['attrGroups', 'flags', 'type', 'byRef', 'variadic', 'var', 'default', 'hooks'];
61    }
62
63    public function getType(): string {
64        return 'Param';
65    }
66
67    /**
68     * Whether this parameter uses constructor property promotion.
69     */
70    public function isPromoted(): bool {
71        return $this->flags !== 0 || $this->hooks !== [];
72    }
73
74    public function isPublic(): bool {
75        $public = (bool) ($this->flags & Modifiers::PUBLIC);
76        if ($public) {
77            return true;
78        }
79
80        if ($this->hooks === []) {
81            return false;
82        }
83
84        return ($this->flags & Modifiers::VISIBILITY_MASK) === 0;
85    }
86
87    public function isProtected(): bool {
88        return (bool) ($this->flags & Modifiers::PROTECTED);
89    }
90
91    public function isPrivate(): bool {
92        return (bool) ($this->flags & Modifiers::PRIVATE);
93    }
94
95    public function isReadonly(): bool {
96        return (bool) ($this->flags & Modifiers::READONLY);
97    }
98
99    /**
100     * Whether the promoted property has explicit public(set) visibility.
101     */
102    public function isPublicSet(): bool {
103        return (bool) ($this->flags & Modifiers::PUBLIC_SET);
104    }
105
106    /**
107     * Whether the promoted property has explicit protected(set) visibility.
108     */
109    public function isProtectedSet(): bool {
110        return (bool) ($this->flags & Modifiers::PROTECTED_SET);
111    }
112
113    /**
114     * Whether the promoted property has explicit private(set) visibility.
115     */
116    public function isPrivateSet(): bool {
117        return (bool) ($this->flags & Modifiers::PRIVATE_SET);
118    }
119}
120