Modifiers.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. /**
  4. * Modifiers used (as a bit mask) by various flags subnodes, for example on classes, functions,
  5. * properties and constants.
  6. */
  7. final class Modifiers {
  8. public const PUBLIC = 1;
  9. public const PROTECTED = 2;
  10. public const PRIVATE = 4;
  11. public const STATIC = 8;
  12. public const ABSTRACT = 16;
  13. public const FINAL = 32;
  14. public const READONLY = 64;
  15. public const VISIBILITY_MASK = 1 | 2 | 4;
  16. /**
  17. * @internal
  18. */
  19. public static function verifyClassModifier(int $a, int $b): void {
  20. if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
  21. throw new Error('Multiple abstract modifiers are not allowed');
  22. }
  23. if ($a & Modifiers::FINAL && $b & Modifiers::FINAL) {
  24. throw new Error('Multiple final modifiers are not allowed');
  25. }
  26. if ($a & Modifiers::READONLY && $b & Modifiers::READONLY) {
  27. throw new Error('Multiple readonly modifiers are not allowed');
  28. }
  29. if ($a & 48 && $b & 48) {
  30. throw new Error('Cannot use the final modifier on an abstract class');
  31. }
  32. }
  33. /**
  34. * @internal
  35. */
  36. public static function verifyModifier(int $a, int $b): void {
  37. if ($a & Modifiers::VISIBILITY_MASK && $b & Modifiers::VISIBILITY_MASK) {
  38. throw new Error('Multiple access type modifiers are not allowed');
  39. }
  40. if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
  41. throw new Error('Multiple abstract modifiers are not allowed');
  42. }
  43. if ($a & Modifiers::STATIC && $b & Modifiers::STATIC) {
  44. throw new Error('Multiple static modifiers are not allowed');
  45. }
  46. if ($a & Modifiers::FINAL && $b & Modifiers::FINAL) {
  47. throw new Error('Multiple final modifiers are not allowed');
  48. }
  49. if ($a & Modifiers::READONLY && $b & Modifiers::READONLY) {
  50. throw new Error('Multiple readonly modifiers are not allowed');
  51. }
  52. if ($a & 48 && $b & 48) {
  53. throw new Error('Cannot use the final modifier on an abstract class member');
  54. }
  55. }
  56. }