ClassMethod.php 4.6 KB

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