菜谱项目

Class_.php 3.2KB

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