ClassMethod.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node;
  4. use PhpParser\Node\FunctionLike;
  5. use PhpParser\Error;
  6. class ClassMethod extends Node\Stmt implements FunctionLike
  7. {
  8. /** @var int Type */
  9. public $type;
  10. /** @var bool Whether to return by reference */
  11. public $byRef;
  12. /** @var string Name */
  13. public $name;
  14. /** @var Node\Param[] Parameters */
  15. public $params;
  16. /** @var null|string|Node\Name Return type */
  17. public $returnType;
  18. /** @var Node[] Statements */
  19. public $stmts;
  20. /**
  21. * Constructs a class method node.
  22. *
  23. * @param string $name Name
  24. * @param array $subNodes Array of the following optional subnodes:
  25. * 'type' => MODIFIER_PUBLIC: Type
  26. * 'byRef' => false : Whether to return by reference
  27. * 'params' => array() : Parameters
  28. * 'returnType' => null : Return type
  29. * 'stmts' => array() : Statements
  30. * @param array $attributes Additional attributes
  31. */
  32. public function __construct($name, array $subNodes = array(), array $attributes = array()) {
  33. parent::__construct($attributes);
  34. $this->type = isset($subNodes['type']) ? $subNodes['type'] : 0;
  35. $this->byRef = isset($subNodes['byRef']) ? $subNodes['byRef'] : false;
  36. $this->name = $name;
  37. $this->params = isset($subNodes['params']) ? $subNodes['params'] : array();
  38. $this->returnType = isset($subNodes['returnType']) ? $subNodes['returnType'] : null;
  39. $this->stmts = array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : array();
  40. if ($this->type & Class_::MODIFIER_STATIC) {
  41. switch (strtolower($this->name)) {
  42. case '__construct':
  43. throw new Error(sprintf('Constructor %s() cannot be static', $this->name));
  44. case '__destruct':
  45. throw new Error(sprintf('Destructor %s() cannot be static', $this->name));
  46. case '__clone':
  47. throw new Error(sprintf('Clone method %s() cannot be static', $this->name));
  48. }
  49. }
  50. }
  51. public function getSubNodeNames() {
  52. return array('type', 'byRef', 'name', 'params', 'returnType', 'stmts');
  53. }
  54. public function returnsByRef() {
  55. return $this->byRef;
  56. }
  57. public function getParams() {
  58. return $this->params;
  59. }
  60. public function getReturnType() {
  61. return $this->returnType;
  62. }
  63. public function getStmts() {
  64. return $this->stmts;
  65. }
  66. public function isPublic() {
  67. return ($this->type & Class_::MODIFIER_PUBLIC) !== 0
  68. || ($this->type & Class_::VISIBILITY_MODIFER_MASK) === 0;
  69. }
  70. public function isProtected() {
  71. return (bool) ($this->type & Class_::MODIFIER_PROTECTED);
  72. }
  73. public function isPrivate() {
  74. return (bool) ($this->type & Class_::MODIFIER_PRIVATE);
  75. }
  76. public function isAbstract() {
  77. return (bool) ($this->type & Class_::MODIFIER_ABSTRACT);
  78. }
  79. public function isFinal() {
  80. return (bool) ($this->type & Class_::MODIFIER_FINAL);
  81. }
  82. public function isStatic() {
  83. return (bool) ($this->type & Class_::MODIFIER_STATIC);
  84. }
  85. }