This repository was archived by the owner on Jul 8, 2023. It is now read-only.
File tree 3 files changed +58
-1
lines changed
3 files changed +58
-1
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,33 @@ final protected static function initializeMembers()
24
24
$ reflector = new ReflectionClass (get_called_class ());
25
25
26
26
foreach ($ reflector ->getConstants () as $ key => $ value ) {
27
- new static ($ key , $ value );
27
+ if (self ::isPublicConstant ($ reflector , $ key )) {
28
+ new static ($ key , $ value );
29
+ }
28
30
}
29
31
}
32
+
33
+ /**
34
+ * Gets a value indicating whether the specified constant name is publicly accessible, according to the specified
35
+ * class reflector.
36
+ *
37
+ * This feature is not supported by all versions of PHP, so a check is performed to see if accessibility detection
38
+ * is available. This result of this check is statically cached so the check only occurs once per script lifetime.
39
+ *
40
+ * @param ReflectionClass $reflector Class reflector.
41
+ * @param string $name Constant name.
42
+ *
43
+ * @return bool True if the constant is public, otherwise false.
44
+ */
45
+ private static function isPublicConstant (ReflectionClass $ reflector , $ name )
46
+ {
47
+ static $ methodExists ;
48
+ $ methodExists === null && $ methodExists = method_exists ($ reflector , 'getReflectionConstants ' );
49
+
50
+ if (!$ methodExists ) {
51
+ return true ;
52
+ }
53
+
54
+ return $ reflector ->getReflectionConstant ($ name )->isPublic ();
55
+ }
30
56
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Eloquent \Enumeration \AbstractEnumeration ;
4
+
5
+ /**
6
+ * An enumeration whose members have different accessibility levels.
7
+ *
8
+ * @method static IMPLICIT_PUBLIC()
9
+ * @method static EXPLICIT_PUBLIC()
10
+ */
11
+ final class MixedAccessibilityEnumeration extends AbstractEnumeration
12
+ {
13
+ const IMPLICIT_PUBLIC = 'IMPLICIT_PUBLIC ' ;
14
+ public const EXPLICIT_PUBLIC = 'EXPLICIT_PUBLIC ' ;
15
+ protected const PROTECTED = 'PROTECTED ' ;
16
+ private const PRIVATE = 'PRIVATE ' ;
17
+ }
Original file line number Diff line number Diff line change @@ -120,4 +120,18 @@ public function testPlanetExampleScriptOutput()
120
120
121
121
$ this ->assertSame ($ expected , $ actual );
122
122
}
123
+
124
+ /**
125
+ * Tests that only public constants are included as enumeration members.
126
+ *
127
+ * @requires PHP 7.1
128
+ */
129
+ public function testMixedAccessibility ()
130
+ {
131
+ $ members = MixedAccessibilityEnumeration::members ();
132
+
133
+ self ::assertCount (2 , $ members );
134
+ self ::assertArrayHasKey (MixedAccessibilityEnumeration::IMPLICIT_PUBLIC , $ members );
135
+ self ::assertArrayHasKey (MixedAccessibilityEnumeration::EXPLICIT_PUBLIC , $ members );
136
+ }
123
137
}
You can’t perform that action at this time.
0 commit comments