@@ -14,8 +14,13 @@ final class Modifiers {
14
14
public const ABSTRACT = 16 ;
15
15
public const FINAL = 32 ;
16
16
public const READONLY = 64 ;
17
+ public const PUBLIC_SET = 128 ;
18
+ public const PROTECTED_SET = 256 ;
19
+ public const PRIVATE_SET = 512 ;
17
20
18
- public const VISIBILITY_MASK = 1 | 2 | 4 ;
21
+ public const VISIBILITY_MASK = self ::PUBLIC | self ::PROTECTED | self ::PRIVATE ;
22
+
23
+ public const VISIBILITY_SET_MASK = self ::PUBLIC_SET | self ::PROTECTED_SET | self ::PRIVATE_SET ;
19
24
20
25
private const TO_STRING_MAP = [
21
26
self ::PUBLIC => 'public ' ,
@@ -25,6 +30,9 @@ final class Modifiers {
25
30
self ::ABSTRACT => 'abstract ' ,
26
31
self ::FINAL => 'final ' ,
27
32
self ::READONLY => 'readonly ' ,
33
+ self ::PUBLIC_SET => 'public(set) ' ,
34
+ self ::PROTECTED_SET => 'protected(set) ' ,
35
+ self ::PRIVATE_SET => 'private(set) ' ,
28
36
];
29
37
30
38
public static function toString (int $ modifier ): string {
@@ -34,15 +42,19 @@ public static function toString(int $modifier): string {
34
42
return self ::TO_STRING_MAP [$ modifier ];
35
43
}
36
44
45
+ private static function isValidModifier (int $ modifier ): bool {
46
+ $ isPow2 = ($ modifier & ($ modifier - 1 )) == 0 && $ modifier != 0 ;
47
+ return $ isPow2 && $ modifier <= self ::PRIVATE_SET ;
48
+ }
49
+
37
50
/**
38
51
* @internal
39
52
*/
40
53
public static function verifyClassModifier (int $ a , int $ b ): void {
41
- foreach ([Modifiers::ABSTRACT , Modifiers::FINAL , Modifiers::READONLY ] as $ modifier ) {
42
- if ($ a & $ modifier && $ b & $ modifier ) {
43
- throw new Error (
44
- 'Multiple ' . self ::toString ($ modifier ) . ' modifiers are not allowed ' );
45
- }
54
+ assert (self ::isValidModifier ($ b ));
55
+ if (($ a & $ b ) != 0 ) {
56
+ throw new Error (
57
+ 'Multiple ' . self ::toString ($ b ) . ' modifiers are not allowed ' );
46
58
}
47
59
48
60
if ($ a & 48 && $ b & 48 ) {
@@ -54,15 +66,16 @@ public static function verifyClassModifier(int $a, int $b): void {
54
66
* @internal
55
67
*/
56
68
public static function verifyModifier (int $ a , int $ b ): void {
57
- if ($ a & Modifiers::VISIBILITY_MASK && $ b & Modifiers::VISIBILITY_MASK ) {
69
+ assert (self ::isValidModifier ($ b ));
70
+ if (($ a & Modifiers::VISIBILITY_MASK && $ b & Modifiers::VISIBILITY_MASK ) ||
71
+ ($ a & Modifiers::VISIBILITY_SET_MASK && $ b & Modifiers::VISIBILITY_SET_MASK )
72
+ ) {
58
73
throw new Error ('Multiple access type modifiers are not allowed ' );
59
74
}
60
75
61
- foreach ([Modifiers::ABSTRACT , Modifiers::STATIC , Modifiers::FINAL , Modifiers::READONLY ] as $ modifier ) {
62
- if ($ a & $ modifier && $ b & $ modifier ) {
63
- throw new Error (
64
- 'Multiple ' . self ::toString ($ modifier ) . ' modifiers are not allowed ' );
65
- }
76
+ if (($ a & $ b ) != 0 ) {
77
+ throw new Error (
78
+ 'Multiple ' . self ::toString ($ b ) . ' modifiers are not allowed ' );
66
79
}
67
80
68
81
if ($ a & 48 && $ b & 48 ) {
0 commit comments