Skip to content

Commit 71d7b81

Browse files
committed
handle class constant visibility added in PHP-7.1
1 parent 7d99150 commit 71d7b81

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

src/Enum.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,27 @@ private static function detectConstants($class)
296296
{
297297
if (!isset(self::$constants[$class])) {
298298
$reflection = new ReflectionClass($class);
299-
$constants = $reflection->getConstants();
299+
$constants = array();
300+
301+
do {
302+
$scopeConstants = array();
303+
if (PHP_VERSION_ID >= 70100) {
304+
// Since PHP-7.1 visibility modifiers are allowed for class constants
305+
// for enumerations we are only interested in public once.
306+
foreach ($reflection->getReflectionConstants() as $reflConstant) {
307+
if ($reflConstant->isPublic()) {
308+
$scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue();
309+
}
310+
}
311+
} else {
312+
// In PHP < 7.1 all class constants were public by definition
313+
$scopeConstants = $reflection->getConstants();
314+
}
315+
316+
$constants = $scopeConstants + $constants;
317+
} while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__);
300318

301-
// values needs to be unique
319+
// Detect ambiguous values and report names
302320
$ambiguous = array();
303321
foreach ($constants as $value) {
304322
$names = array_keys($constants, $value, true);
@@ -315,11 +333,6 @@ private static function detectConstants($class)
315333
);
316334
}
317335

318-
// This is required to make sure that constants of base classes will be the first
319-
while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__) {
320-
$constants = $reflection->getConstants() + $constants;
321-
}
322-
323336
self::$constants[$class] = $constants;
324337
}
325338

tests/MabeEnumTest/EnumTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use MabeEnumTest\TestAsset\EnumInheritance;
77
use MabeEnumTest\TestAsset\EnumAmbiguous;
88
use MabeEnumTest\TestAsset\EnumExtendedAmbiguous;
9+
use MabeEnumTest\TestAsset\ConstVisibilityEnum;
10+
use MabeEnumTest\TestAsset\ConstVisibilityEnumExtended;
911
use PHPUnit_Framework_TestCase as TestCase;
1012
use ReflectionClass;
1113

@@ -255,4 +257,32 @@ public function testHas()
255257
$this->assertTrue($enum->has(EnumBasic::ONE()));
256258
$this->assertTrue($enum->has(EnumBasic::ONE));
257259
}
260+
261+
public function testConstVisibility()
262+
{
263+
if (PHP_VERSION_ID < 70100) {
264+
$this->markTestSkipped('This test is for PHP-7.1 and upper only');
265+
}
266+
267+
$constants = ConstVisibilityEnum::getConstants();
268+
$this->assertSame(array(
269+
'IPUB' => ConstVisibilityEnum::IPUB,
270+
'PUB' => ConstVisibilityEnum::PUB,
271+
), $constants);
272+
}
273+
274+
public function testConstVisibilityExtended()
275+
{
276+
if (PHP_VERSION_ID < 70100) {
277+
$this->markTestSkipped('This test is for PHP-7.1 and upper only');
278+
}
279+
280+
$constants = ConstVisibilityEnumExtended::getConstants();
281+
$this->assertSame(array(
282+
'IPUB' => ConstVisibilityEnumExtended::IPUB,
283+
'PUB' => ConstVisibilityEnumExtended::PUB,
284+
'IPUB2' => ConstVisibilityEnumExtended::IPUB2,
285+
'PUB2' => ConstVisibilityEnumExtended::PUB2,
286+
), $constants);
287+
}
258288
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
7+
/**
8+
* Unit tests for the class MabeEnum\Enum
9+
*
10+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
11+
* @copyright Copyright (c) 2013 Marc Bennewitz
12+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
13+
*/
14+
class ConstVisibilityEnum extends Enum
15+
{
16+
const IPUB = 'indirect public';
17+
public const PUB = 'public';
18+
protected const PRO = 'protected';
19+
private const PRI = 'private';
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
7+
/**
8+
* Unit tests for the class MabeEnum\Enum
9+
*
10+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
11+
* @copyright Copyright (c) 2013 Marc Bennewitz
12+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
13+
*/
14+
class ConstVisibilityEnumExtended extends ConstVisibilityEnum
15+
{
16+
const IPUB2 = 'indirect public extended';
17+
public const PUB2 = 'public extended';
18+
protected const PRO2 = 'protected extended';
19+
private const PRI2 = 'private extended';
20+
}

0 commit comments

Comments
 (0)