Class_.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Modifiers;
  4. use PhpParser\Node;
  5. class Class_ extends ClassLike {
  6. /** @deprecated Use Modifiers::PUBLIC instead */
  7. public const MODIFIER_PUBLIC = 1;
  8. /** @deprecated Use Modifiers::PROTECTED instead */
  9. public const MODIFIER_PROTECTED = 2;
  10. /** @deprecated Use Modifiers::PRIVATE instead */
  11. public const MODIFIER_PRIVATE = 4;
  12. /** @deprecated Use Modifiers::STATIC instead */
  13. public const MODIFIER_STATIC = 8;
  14. /** @deprecated Use Modifiers::ABSTRACT instead */
  15. public const MODIFIER_ABSTRACT = 16;
  16. /** @deprecated Use Modifiers::FINAL instead */
  17. public const MODIFIER_FINAL = 32;
  18. /** @deprecated Use Modifiers::READONLY instead */
  19. public const MODIFIER_READONLY = 64;
  20. /** @deprecated Use Modifiers::VISIBILITY_MASK instead */
  21. public const VISIBILITY_MODIFIER_MASK = 7; // 1 | 2 | 4
  22. /** @var int Modifiers */
  23. public int $flags;
  24. /** @var null|Node\Name Name of extended class */
  25. public ?Node\Name $extends;
  26. /** @var Node\Name[] Names of implemented interfaces */
  27. public array $implements;
  28. /**
  29. * Constructs a class node.
  30. *
  31. * @param string|Node\Identifier|null $name Name
  32. * @param array{
  33. * flags?: int,
  34. * extends?: Node\Name|null,
  35. * implements?: Node\Name[],
  36. * stmts?: Node\Stmt[],
  37. * attrGroups?: Node\AttributeGroup[],
  38. * } $subNodes Array of the following optional subnodes:
  39. * 'flags' => 0 : Flags
  40. * 'extends' => null : Name of extended class
  41. * 'implements' => array(): Names of implemented interfaces
  42. * 'stmts' => array(): Statements
  43. * 'attrGroups' => array(): PHP attribute groups
  44. * @param array<string, mixed> $attributes Additional attributes
  45. */
  46. public function __construct($name, array $subNodes = [], array $attributes = []) {
  47. $this->attributes = $attributes;
  48. $this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
  49. $this->name = \is_string($name) ? new Node\Identifier($name) : $name;
  50. $this->extends = $subNodes['extends'] ?? null;
  51. $this->implements = $subNodes['implements'] ?? [];
  52. $this->stmts = $subNodes['stmts'] ?? [];
  53. $this->attrGroups = $subNodes['attrGroups'] ?? [];
  54. }
  55. public function getSubNodeNames(): array {
  56. return ['attrGroups', 'flags', 'name', 'extends', 'implements', 'stmts'];
  57. }
  58. /**
  59. * Whether the class is explicitly abstract.
  60. */
  61. public function isAbstract(): bool {
  62. return (bool) ($this->flags & Modifiers::ABSTRACT);
  63. }
  64. /**
  65. * Whether the class is final.
  66. */
  67. public function isFinal(): bool {
  68. return (bool) ($this->flags & Modifiers::FINAL);
  69. }
  70. public function isReadonly(): bool {
  71. return (bool) ($this->flags & Modifiers::READONLY);
  72. }
  73. /**
  74. * Whether the class is anonymous.
  75. */
  76. public function isAnonymous(): bool {
  77. return null === $this->name;
  78. }
  79. public function getType(): string {
  80. return 'Stmt_Class';
  81. }
  82. }