ClassMethod.php 4.2 KB

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