No Description

ClassMethod.php 2.8KB

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