| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the "elao/enum" package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (C) Elao | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Elao <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Elao\Enum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Elao\Enum\Exception\LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Auto-discover enumerated values from public constants. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | trait AutoDiscoveredValuesTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @internal */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private static $guessedValues = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @internal */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private static $guessedReadables = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @see EnumInterface::values() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return int[]|string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public static function values(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return self::autodiscoveredValues(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @see ChoiceEnumTrait::choices() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected static function choices(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (!in_array(ChoiceEnumTrait::class, class_uses(self::class, false), true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             throw new LogicException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'Method "%s" is only meant to be used when using the "%s" trait which is not used in "%s"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 ChoiceEnumTrait::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 static::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return self::autodiscoveredReadables(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private static function autodiscoveredValues(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $enumType = static::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!isset(self::$guessedValues[$enumType])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $r = new \ReflectionClass($enumType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $values = $r->getConstants(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |             if (PHP_VERSION_ID >= 70100) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $values = array_filter($values, function (string $k) use ($r) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     return $r->getReflectionConstant($k)->isPublic(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 }, ARRAY_FILTER_USE_KEY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (is_a($enumType, FlaggedEnum::class, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $values = array_filter($values, function ($v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     return is_int($v) && 0 === ($v & $v - 1) && $v > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $values = array_filter($values, function ($v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     return is_int($v) || is_string($v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             self::$guessedValues[$enumType] = array_values($values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return self::$guessedValues[$enumType]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @internal | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 89 |  | View Code Duplication |     private static function autodiscoveredReadables(): array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         $enumType = static::class; | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         if (!isset(self::$guessedReadables[$enumType])) { | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |             $constants = (new \ReflectionClass($enumType))->getConstants(); | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             foreach (self::autodiscoveredValues() as $value) { | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |                 $constantName = array_search($value, $constants, true); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |                 self::$guessedReadables[$enumType][$value] = ucfirst(strtolower(str_replace('_', ' ', $constantName))); | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         return self::$guessedReadables[$enumType]; | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.