Class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @property int $type Type
  4. * @property string $name Name
  5. * @property null|PHPParser_Node_Name $extends Name of extended class
  6. * @property PHPParser_Node_Name[] $implements Names of implemented interfaces
  7. * @property PHPParser_Node[] $stmts Statements
  8. */
  9. class PHPParser_Node_Stmt_Class extends PHPParser_Node_Stmt
  10. {
  11. const MODIFIER_PUBLIC = 1;
  12. const MODIFIER_PROTECTED = 2;
  13. const MODIFIER_PRIVATE = 4;
  14. const MODIFIER_STATIC = 8;
  15. const MODIFIER_ABSTRACT = 16;
  16. const MODIFIER_FINAL = 32;
  17. protected static $specialNames = array(
  18. 'self' => true,
  19. 'parent' => true,
  20. 'static' => true,
  21. );
  22. /**
  23. * Constructs a class node.
  24. *
  25. * @param string $name Name
  26. * @param array $subNodes Array of the following optional subnodes:
  27. * 'type' => 0 : Type
  28. * 'extends' => null : Name of extended class
  29. * 'implements' => array(): Names of implemented interfaces
  30. * 'stmts' => array(): Statements
  31. * @param array $attributes Additional attributes
  32. */
  33. public function __construct($name, array $subNodes = array(), array $attributes = array()) {
  34. parent::__construct(
  35. $subNodes + array(
  36. 'type' => 0,
  37. 'extends' => null,
  38. 'implements' => array(),
  39. 'stmts' => array(),
  40. ),
  41. $attributes
  42. );
  43. $this->name = $name;
  44. if (isset(self::$specialNames[(string) $this->name])) {
  45. throw new PHPParser_Error(sprintf('Cannot use "%s" as class name as it is reserved', $this->name));
  46. }
  47. if (isset(self::$specialNames[(string) $this->extends])) {
  48. throw new PHPParser_Error(sprintf('Cannot use "%s" as class name as it is reserved', $this->extends));
  49. }
  50. foreach ($this->implements as $interface) {
  51. if (isset(self::$specialNames[(string) $interface])) {
  52. throw new PHPParser_Error(sprintf('Cannot use "%s" as interface name as it is reserved', $interface));
  53. }
  54. }
  55. }
  56. public function isAbstract() {
  57. return (bool) ($this->type & self::MODIFIER_ABSTRACT);
  58. }
  59. public function isFinal() {
  60. return (bool) ($this->type & self::MODIFIER_FINAL);
  61. }
  62. public function getMethods() {
  63. $methods = array();
  64. foreach ($this->stmts as $stmt) {
  65. if ($stmt instanceof PHPParser_Node_Stmt_ClassMethod) {
  66. $methods[] = $stmt;
  67. }
  68. }
  69. return $methods;
  70. }
  71. public static function verifyModifier($a, $b) {
  72. if ($a & 7 && $b & 7) {
  73. throw new PHPParser_Error('Multiple access type modifiers are not allowed');
  74. }
  75. if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
  76. throw new PHPParser_Error('Multiple abstract modifiers are not allowed');
  77. }
  78. if ($a & self::MODIFIER_STATIC && $b & self::MODIFIER_STATIC) {
  79. throw new PHPParser_Error('Multiple static modifiers are not allowed');
  80. }
  81. if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
  82. throw new PHPParser_Error('Multiple final modifiers are not allowed');
  83. }
  84. if ($a & 48 && $b & 48) {
  85. throw new PHPParser_Error('Cannot use the final and abstract modifier at the same time');
  86. }
  87. }
  88. }