Class_.php 3.5 KB

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