Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit e44bec1

Browse files
committed
Added support for non-public enumeration constants.
Previously, non-public constants were being included in enumerations. However, non-public constants cannot be accessed externally so they should not be part of the enumeration. This patch adds only public constants, whether declared explicitly or implicitly, to the enumeration.
1 parent 9a5bb77 commit e44bec1

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/AbstractEnumeration.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,33 @@ final protected static function initializeMembers()
2424
$reflector = new ReflectionClass(get_called_class());
2525

2626
foreach ($reflector->getConstants() as $key => $value) {
27-
new static($key, $value);
27+
if (self::isPublicConstant($reflector, $key)) {
28+
new static($key, $value);
29+
}
2830
}
2931
}
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+
}
3056
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

test/suite/FunctionalTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,18 @@ public function testPlanetExampleScriptOutput()
120120

121121
$this->assertSame($expected, $actual);
122122
}
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+
}
123137
}

0 commit comments

Comments
 (0)