22
33namespace MabeEnum ;
44
5+ use AssertionError ;
56use ReflectionClass ;
67use InvalidArgumentException ;
78use LogicException ;
@@ -337,7 +338,7 @@ final public static function has($value)
337338 *
338339 * @param string $class
339340 * @return array
340- * @throws LogicException On ambiguous constant values
341+ * @throws AssertionError On ambiguous constant values
341342 */
342343 private static function detectConstants ($ class )
343344 {
@@ -347,7 +348,7 @@ private static function detectConstants($class)
347348
348349 do {
349350 $ scopeConstants = [];
350- if (PHP_VERSION_ID >= 70100 ) {
351+ if (\ PHP_VERSION_ID >= 70100 ) {
351352 // Since PHP-7.1 visibility modifiers are allowed for class constants
352353 // for enumerations we are only interested in public once.
353354 foreach ($ reflection ->getReflectionConstants () as $ reflConstant ) {
@@ -363,30 +364,34 @@ private static function detectConstants($class)
363364 $ constants = $ scopeConstants + $ constants ;
364365 } while (($ reflection = $ reflection ->getParentClass ()) && $ reflection ->name !== __CLASS__ );
365366
366- // Detect ambiguous values and report names
367- $ ambiguous = [];
368- foreach ($ constants as $ value ) {
369- $ names = \array_keys ($ constants , $ value , true );
370- if (\count ($ names ) > 1 ) {
371- $ ambiguous [\var_export ($ value , true )] = $ names ;
372- }
373- }
374- if (!empty ($ ambiguous )) {
375- throw new LogicException (
376- 'All possible values needs to be unique. The following are ambiguous: '
377- . \implode (', ' , \array_map (function ($ names ) use ($ constants ) {
378- return \implode ('/ ' , $ names ) . '= ' . \var_export ($ constants [$ names [0 ]], true );
379- }, $ ambiguous ))
380- );
381- }
382-
383367 self ::$ constants [$ class ] = $ constants ;
384368 self ::$ names [$ class ] = \array_keys ($ constants );
369+
370+ assert ('self::assertNonAmbiguous($class) ' );
385371 }
386372
387373 return self ::$ constants [$ class ];
388374 }
389375
376+ /**
377+ * Assert that the given enumeration class doesn't define ambiguous enumerator values
378+ * @param string $class
379+ * @return bool
380+ */
381+ private static function assertNonAmbiguous ($ class )
382+ {
383+ $ constants = self ::$ constants [$ class ];
384+ $ ambiguous = [];
385+ foreach ($ constants as $ value ) {
386+ $ names = \array_keys ($ constants , $ value , true );
387+ if (\count ($ names ) > 1 ) {
388+ $ ambiguous [\var_export ($ value , true )] = $ names ;
389+ }
390+ }
391+
392+ return empty ($ ambiguous );
393+ }
394+
390395 /**
391396 * Get an enumerator instance by the given name.
392397 *
0 commit comments