1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Node\Stmt;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\FunctionLike;
7:
8: class ClassMethod extends Node\Stmt implements FunctionLike
9: {
10: /** @var int Flags */
11: public $flags;
12: /** @var bool Whether to return by reference */
13: public $byRef;
14: /** @var Node\Identifier Name */
15: public $name;
16: /** @var Node\Param[] Parameters */
17: public $params;
18: /** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
19: public $returnType;
20: /** @var Node\Stmt[]|null Statements */
21: public $stmts;
22: /** @var Node\AttributeGroup[] PHP attribute groups */
23: public $attrGroups;
24:
25: private static $magicNames = [
26: '__construct' => true,
27: '__destruct' => true,
28: '__call' => true,
29: '__callstatic' => true,
30: '__get' => true,
31: '__set' => true,
32: '__isset' => true,
33: '__unset' => true,
34: '__sleep' => true,
35: '__wakeup' => true,
36: '__tostring' => true,
37: '__set_state' => true,
38: '__clone' => true,
39: '__invoke' => true,
40: '__debuginfo' => true,
41: '__serialize' => true,
42: '__unserialize' => true,
43: ];
44:
45: /**
46: * Constructs a class method node.
47: *
48: * @param string|Node\Identifier $name Name
49: * @param array $subNodes Array of the following optional subnodes:
50: * 'flags => MODIFIER_PUBLIC: Flags
51: * 'byRef' => false : Whether to return by reference
52: * 'params' => array() : Parameters
53: * 'returnType' => null : Return type
54: * 'stmts' => array() : Statements
55: * 'attrGroups' => array() : PHP attribute groups
56: * @param array $attributes Additional attributes
57: */
58: public function __construct($name, array $subNodes = [], array $attributes = []) {
59: $this->attributes = $attributes;
60: $this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
61: $this->byRef = $subNodes['byRef'] ?? false;
62: $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
63: $this->params = $subNodes['params'] ?? [];
64: $returnType = $subNodes['returnType'] ?? null;
65: $this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
66: $this->stmts = array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : [];
67: $this->attrGroups = $subNodes['attrGroups'] ?? [];
68: }
69:
70: public function getSubNodeNames() : array {
71: return ['attrGroups', 'flags', 'byRef', 'name', 'params', 'returnType', 'stmts'];
72: }
73:
74: public function returnsByRef() : bool {
75: return $this->byRef;
76: }
77:
78: public function getParams() : array {
79: return $this->params;
80: }
81:
82: public function getReturnType() {
83: return $this->returnType;
84: }
85:
86: public function getStmts() {
87: return $this->stmts;
88: }
89:
90: public function getAttrGroups() : array {
91: return $this->attrGroups;
92: }
93:
94: /**
95: * Whether the method is explicitly or implicitly public.
96: *
97: * @return bool
98: */
99: public function isPublic() : bool {
100: return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
101: || ($this->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0;
102: }
103:
104: /**
105: * Whether the method is protected.
106: *
107: * @return bool
108: */
109: public function isProtected() : bool {
110: return (bool) ($this->flags & Class_::MODIFIER_PROTECTED);
111: }
112:
113: /**
114: * Whether the method is private.
115: *
116: * @return bool
117: */
118: public function isPrivate() : bool {
119: return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
120: }
121:
122: /**
123: * Whether the method is abstract.
124: *
125: * @return bool
126: */
127: public function isAbstract() : bool {
128: return (bool) ($this->flags & Class_::MODIFIER_ABSTRACT);
129: }
130:
131: /**
132: * Whether the method is final.
133: *
134: * @return bool
135: */
136: public function isFinal() : bool {
137: return (bool) ($this->flags & Class_::MODIFIER_FINAL);
138: }
139:
140: /**
141: * Whether the method is static.
142: *
143: * @return bool
144: */
145: public function isStatic() : bool {
146: return (bool) ($this->flags & Class_::MODIFIER_STATIC);
147: }
148:
149: /**
150: * Whether the method is magic.
151: *
152: * @return bool
153: */
154: public function isMagic() : bool {
155: return isset(self::$magicNames[$this->name->toLowerString()]);
156: }
157:
158: public function getType() : string {
159: return 'Stmt_ClassMethod';
160: }
161: }
162: