ClassMethod.php 4.6 KB

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