| Total Complexity | 55 |
| Total Lines | 312 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Config |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Fichier de configuration déjà chargé |
||
| 30 | */ |
||
| 31 | private static array $loaded = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Different registrars decouverts. |
||
| 35 | * |
||
| 36 | * Les registrars sont des mecanismes permettant aux packages externe de definir un elements de configuration |
||
| 37 | */ |
||
| 38 | private static array $registrars = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Drapeau permettant de savoir si la config a deja ete initialiser |
||
| 42 | */ |
||
| 43 | private static bool $initialized = false; |
||
| 44 | |||
| 45 | private Configurator $configurator; |
||
| 46 | |||
| 47 | public function __construct() |
||
| 48 | { |
||
| 49 | $this->configurator = new Configurator(); |
||
| 50 | $this->initialize(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Détermine si une clé de configuration existe. |
||
| 55 | */ |
||
| 56 | public function exists(string $key): bool |
||
| 57 | { |
||
| 58 | if (! $this->configurator->exists($key)) { |
||
| 59 | $config = explode('.', $key); |
||
| 60 | $this->load($config[0]); |
||
| 61 | |||
| 62 | return $this->configurator->exists(implode('.', $config)); |
||
| 63 | } |
||
| 64 | |||
| 65 | return true; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Détermine s'il y'a une clé de configuration. |
||
| 70 | */ |
||
| 71 | public function has(string $key): bool |
||
| 72 | { |
||
| 73 | return $this->exists($key); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Détermine s'il manque une clé de configuration. |
||
| 78 | */ |
||
| 79 | public function missing(string $key): bool |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Renvoyer une configuration de l'application |
||
| 86 | * |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | public function get(string $key, mixed $default = null) |
||
| 90 | { |
||
| 91 | if ($this->exists($key)) { |
||
| 92 | return $this->configurator->get($key); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (func_num_args() > 1) { |
||
| 96 | return $default; |
||
| 97 | } |
||
| 98 | |||
| 99 | $path = explode('.', $key); |
||
| 100 | |||
| 101 | throw ConfigException::notFound(implode(' » ', $path)); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Définir une configuration de l'application |
||
| 106 | * |
||
| 107 | * @param mixed $value |
||
| 108 | */ |
||
| 109 | public function set(string $key, $value) |
||
| 110 | { |
||
| 111 | $this->configurator->set($key, $value); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Charger la configuration spécifique dans le scoope |
||
| 116 | * |
||
| 117 | * @param string|string[] $config |
||
| 118 | */ |
||
| 119 | public function load($config, ?string $file = null, ?Schema $schema = null) |
||
| 120 | { |
||
| 121 | if (is_array($config)) { |
||
| 122 | foreach ($config as $key => $value) { |
||
| 123 | if (! is_string($value) || empty($value)) { |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | if (is_string($key)) { |
||
| 127 | $file = $value; |
||
| 128 | $conf = $key; |
||
| 129 | } else { |
||
| 130 | $file = null; |
||
| 131 | $conf = $value; |
||
| 132 | } |
||
| 133 | self::load($conf, $file); |
||
|
|
|||
| 134 | } |
||
| 135 | } elseif (is_string($config) && ! isset(self::$loaded[$config])) { |
||
| 136 | if (empty($file)) { |
||
| 137 | $file = self::path($config); |
||
| 138 | } |
||
| 139 | |||
| 140 | $configurations = []; |
||
| 141 | if (file_exists($file) && ! in_array($file, get_included_files(), true)) { |
||
| 142 | $configurations = (array) require $file; |
||
| 143 | } |
||
| 144 | |||
| 145 | $configurations = Arr::merge(self::$registrars[$config] ?? [], $configurations); |
||
| 146 | |||
| 147 | if (empty($schema)) { |
||
| 148 | $schema = self::schema($config); |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->configurator->addSchema($config, $schema, false); |
||
| 152 | $this->configurator->merge([$config => $configurations]); |
||
| 153 | |||
| 154 | self::$loaded[$config] = $file; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Affiche l'exception dû à la mauvaise definition d'une configuration |
||
| 160 | * |
||
| 161 | * @param array|string $accepts_values |
||
| 162 | * @param string $group (app, data, database, etc.) |
||
| 163 | */ |
||
| 164 | public static function exceptBadConfigValue(string $config_key, $accepts_values, string $group) |
||
| 165 | { |
||
| 166 | if (is_array($accepts_values)) { |
||
| 167 | $accepts_values = '(Accept values: ' . implode('/', $accepts_values) . ')'; |
||
| 168 | } elseif (! is_string($accepts_values)) { |
||
| 169 | throw new InvalidArgumentException('Misuse of the method ' . __METHOD__); |
||
| 170 | } |
||
| 171 | |||
| 172 | throw new ConfigException("The '{$group}.{$config_key} configuration is not set correctly. {$accepts_values} \n Please edit '{" . self::path($group) . "}' file to correct it"); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Renvoie le chemin du fichier d'un groupe de configuration donné |
||
| 177 | */ |
||
| 178 | public static function path(string $path): string |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Retrouve le schema de configuration d'un groupe |
||
| 197 | */ |
||
| 198 | public static function schema(string $key): Schema |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Initialiser la configuration du système avec les données des fichier de configuration |
||
| 219 | */ |
||
| 220 | private function initialize() |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Charges les registrars disponible pour l'application. |
||
| 241 | * Les registrars sont des mecanismes permettant aux packages externe de definir un elements de configuration |
||
| 242 | */ |
||
| 243 | private function loadRegistrar() |
||
| 244 | { |
||
| 245 | $autoloader = new Autoloader(['psr4' => [APP_NAMESPACE => APP_PATH]]); |
||
| 246 | $locator = new Locator($autoloader->initialize()); |
||
| 247 | |||
| 248 | $registrarsFiles = $locator->search('Config/Registrar.php'); |
||
| 249 | |||
| 250 | foreach ($registrarsFiles as $file) { |
||
| 251 | if (false === $classname = $locator->findQualifiedNameFromPath($file)) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | $class = new ReflectionClass($classname); |
||
| 256 | $methods = $class->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC); |
||
| 257 | |||
| 258 | foreach ($methods as $method) { |
||
| 259 | if (! ($method->isPublic() && $method->isStatic())) { |
||
| 260 | continue; |
||
| 261 | } |
||
| 262 | |||
| 263 | if (! is_array($result = $method->invoke(null))) { |
||
| 264 | continue; |
||
| 265 | } |
||
| 266 | |||
| 267 | $name = $method->getName(); |
||
| 268 | self::$registrars[$name] = Arr::merge(self::$registrars[$name] ?? [], $result); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Initialise l'URL |
||
| 275 | */ |
||
| 276 | private function initializeURL() |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Initialise l'environnement d'execution de l'application |
||
| 289 | */ |
||
| 290 | private function initializeEnvironment() |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Initialise les paramètres de la bar de debug |
||
| 327 | */ |
||
| 328 | private function initializeDebugbar() |
||
| 341 |