No Description

Class_.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node;
  4. use PhpParser\Error;
  5. /**
  6. * @property int $type Type
  7. * @property string $name Name
  8. * @property null|Node\Name $extends Name of extended class
  9. * @property Node\Name[] $implements Names of implemented interfaces
  10. * @property Node[] $stmts Statements
  11. */
  12. class Class_ extends Node\Stmt
  13. {
  14. const MODIFIER_PUBLIC = 1;
  15. const MODIFIER_PROTECTED = 2;
  16. const MODIFIER_PRIVATE = 4;
  17. const MODIFIER_STATIC = 8;
  18. const MODIFIER_ABSTRACT = 16;
  19. const MODIFIER_FINAL = 32;
  20. const VISIBILITY_MODIFER_MASK = 7; // 1 | 2 | 4
  21. protected static $specialNames = array(
  22. 'self' => true,
  23. 'parent' => true,
  24. 'static' => true,
  25. );
  26. /**
  27. * Constructs a class node.
  28. *
  29. * @param string $name Name
  30. * @param array $subNodes Array of the following optional subnodes:
  31. * 'type' => 0 : Type
  32. * 'extends' => null : Name of extended class
  33. * 'implements' => array(): Names of implemented interfaces
  34. * 'stmts' => array(): Statements
  35. * @param array $attributes Additional attributes
  36. */
  37. public function __construct($name, array $subNodes = array(), array $attributes = array()) {
  38. parent::__construct(
  39. array(
  40. 'type' => isset($subNodes['type']) ? $subNodes['type'] : 0,
  41. 'name' => $name,
  42. 'extends' => isset($subNodes['extends']) ? $subNodes['extends'] : null,
  43. 'implements' => isset($subNodes['implements']) ? $subNodes['implements'] : array(),
  44. 'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
  45. ),
  46. $attributes
  47. );
  48. if (isset(self::$specialNames[(string) $this->name])) {
  49. throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name));
  50. }
  51. if (isset(self::$specialNames[(string) $this->extends])) {
  52. throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends));
  53. }
  54. foreach ($this->implements as $interface) {
  55. if (isset(self::$specialNames[(string) $interface])) {
  56. throw new Error(sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface));
  57. }
  58. }
  59. }
  60. public function isAbstract() {
  61. return (bool) ($this->type & self::MODIFIER_ABSTRACT);
  62. }
  63. public function isFinal() {
  64. return (bool) ($this->type & self::MODIFIER_FINAL);
  65. }
  66. public function getMethods() {
  67. $methods = array();
  68. foreach ($this->stmts as $stmt) {
  69. if ($stmt instanceof ClassMethod) {
  70. $methods[] = $stmt;
  71. }
  72. }
  73. return $methods;
  74. }
  75. /**
  76. * @internal
  77. */
  78. public static function verifyModifier($a, $b) {
  79. if ($a & self::VISIBILITY_MODIFER_MASK && $b & self::VISIBILITY_MODIFER_MASK) {
  80. throw new Error('Multiple access type modifiers are not allowed');
  81. }
  82. if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
  83. throw new Error('Multiple abstract modifiers are not allowed');
  84. }
  85. if ($a & self::MODIFIER_STATIC && $b & self::MODIFIER_STATIC) {
  86. throw new Error('Multiple static modifiers are not allowed');
  87. }
  88. if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
  89. throw new Error('Multiple final modifiers are not allowed');
  90. }
  91. if ($a & 48 && $b & 48) {
  92. throw new Error('Cannot use the final modifier on an abstract class member');
  93. }
  94. }
  95. }