1 <?php declare(strict_types=1);
3 namespace PhpParser\Node\Stmt;
6 use PhpParser\Node\FunctionLike;
8 class ClassMethod extends Node\Stmt implements FunctionLike
12 /** @var bool Whether to return by reference */
14 /** @var Node\Identifier Name */
16 /** @var Node\Param[] Parameters */
18 /** @var null|Node\Identifier|Node\Name|Node\NullableType Return type */
20 /** @var Node\Stmt[]|null Statements */
23 private static $magicNames = [
24 '__construct' => true,
27 '__callstatic' => true,
35 '__set_state' => true,
38 '__debuginfo' => true,
42 * Constructs a class method node.
44 * @param string|Node\Identifier $name Name
45 * @param array $subNodes Array of the following optional subnodes:
46 * 'flags => MODIFIER_PUBLIC: Flags
47 * 'byRef' => false : Whether to return by reference
48 * 'params' => array() : Parameters
49 * 'returnType' => null : Return type
50 * 'stmts' => array() : Statements
51 * @param array $attributes Additional attributes
53 public function __construct($name, array $subNodes = [], array $attributes = []) {
54 parent::__construct($attributes);
55 $this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
56 $this->byRef = $subNodes['byRef'] ?? false;
57 $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
58 $this->params = $subNodes['params'] ?? [];
59 $returnType = $subNodes['returnType'] ?? null;
60 $this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
61 $this->stmts = array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : [];
64 public function getSubNodeNames() : array {
65 return ['flags', 'byRef', 'name', 'params', 'returnType', 'stmts'];
68 public function returnsByRef() : bool {
72 public function getParams() : array {
76 public function getReturnType() {
77 return $this->returnType;
80 public function getStmts() {
85 * Whether the method is explicitly or implicitly public.
89 public function isPublic() : bool {
90 return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
91 || ($this->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0;
95 * Whether the method is protected.
99 public function isProtected() : bool {
100 return (bool) ($this->flags & Class_::MODIFIER_PROTECTED);
104 * Whether the method is private.
108 public function isPrivate() : bool {
109 return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
113 * Whether the method is abstract.
117 public function isAbstract() : bool {
118 return (bool) ($this->flags & Class_::MODIFIER_ABSTRACT);
122 * Whether the method is final.
126 public function isFinal() : bool {
127 return (bool) ($this->flags & Class_::MODIFIER_FINAL);
131 * Whether the method is static.
135 public function isStatic() : bool {
136 return (bool) ($this->flags & Class_::MODIFIER_STATIC);
140 * Whether the method is magic.
144 public function isMagic() : bool {
145 return isset(self::$magicNames[$this->name->toLowerString()]);
148 public function getType() : string {
149 return 'Stmt_ClassMethod';