Class_.php 3.3 KB

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