Class_.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @param array $attributes Additional attributes
  30. */
  31. public function __construct($name, array $subNodes = [], array $attributes = []) {
  32. parent::__construct($attributes);
  33. $this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
  34. $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
  35. $this->extends = $subNodes['extends'] ?? null;
  36. $this->implements = $subNodes['implements'] ?? [];
  37. $this->stmts = $subNodes['stmts'] ?? [];
  38. }
  39. public function getSubNodeNames() : array {
  40. return ['flags', 'name', 'extends', 'implements', 'stmts'];
  41. }
  42. /**
  43. * Whether the class is explicitly abstract.
  44. *
  45. * @return bool
  46. */
  47. public function isAbstract() : bool {
  48. return (bool) ($this->flags & self::MODIFIER_ABSTRACT);
  49. }
  50. /**
  51. * Whether the class is final.
  52. *
  53. * @return bool
  54. */
  55. public function isFinal() : bool {
  56. return (bool) ($this->flags & self::MODIFIER_FINAL);
  57. }
  58. /**
  59. * Whether the class is anonymous.
  60. *
  61. * @return bool
  62. */
  63. public function isAnonymous() : bool {
  64. return null === $this->name;
  65. }
  66. /**
  67. * @internal
  68. */
  69. public static function verifyModifier($a, $b) {
  70. if ($a & self::VISIBILITY_MODIFIER_MASK && $b & self::VISIBILITY_MODIFIER_MASK) {
  71. throw new Error('Multiple access type modifiers are not allowed');
  72. }
  73. if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
  74. throw new Error('Multiple abstract modifiers are not allowed');
  75. }
  76. if ($a & self::MODIFIER_STATIC && $b & self::MODIFIER_STATIC) {
  77. throw new Error('Multiple static modifiers are not allowed');
  78. }
  79. if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
  80. throw new Error('Multiple final modifiers are not allowed');
  81. }
  82. if ($a & 48 && $b & 48) {
  83. throw new Error('Cannot use the final modifier on an abstract class member');
  84. }
  85. }
  86. public function getType() : string {
  87. return 'Stmt_Class';
  88. }
  89. }