| Total Complexity | 41 |
| Total Lines | 262 |
| 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 |
||
| 14 | class Config |
||
| 15 | { |
||
| 16 | private const BASE_DIR_MARKER = '<<<base-dir>>>'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string config name |
||
| 20 | */ |
||
| 21 | private string $name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array sources - paths to config source files |
||
| 25 | */ |
||
| 26 | private array $sources = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array config value |
||
| 30 | */ |
||
| 31 | protected array $values = []; |
||
| 32 | |||
| 33 | protected Builder $builder; |
||
| 34 | |||
| 35 | protected ContentWriter $contentWriter; |
||
| 36 | |||
| 37 | public function __construct(Builder $builder, string $name) |
||
| 38 | { |
||
| 39 | $this->builder = $builder; |
||
| 40 | $this->name = $name; |
||
| 41 | $this->contentWriter = new ContentWriter(); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function clone(Builder $builder): self |
||
| 51 | } |
||
| 52 | |||
| 53 | public function getValues(): array |
||
| 54 | { |
||
| 55 | return $this->values; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function load(array $paths = []): self |
||
| 59 | { |
||
| 60 | $this->sources = $this->loadFiles($paths); |
||
| 61 | |||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | private function loadFiles(array $paths): array |
||
| 66 | { |
||
| 67 | switch (count($paths)) { |
||
| 68 | case 0: |
||
| 69 | return []; |
||
| 70 | case 1: |
||
| 71 | return [$this->loadFile(reset($paths))]; |
||
| 72 | } |
||
| 73 | |||
| 74 | $configs = []; |
||
| 75 | foreach ($paths as $path) { |
||
| 76 | $cs = $this->loadFiles($this->glob($path)); |
||
| 77 | foreach ($cs as $config) { |
||
| 78 | if (!empty($config)) { |
||
| 79 | $configs[] = $config; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | return $configs; |
||
| 85 | } |
||
| 86 | |||
| 87 | private function glob(string $path): array |
||
| 88 | { |
||
| 89 | if (strpos($path, '*') === false) { |
||
| 90 | return [$path]; |
||
| 91 | } |
||
| 92 | |||
| 93 | return glob($path); |
||
|
|
|||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Reads config file. |
||
| 98 | * |
||
| 99 | * @param string $path |
||
| 100 | * @return array configuration read from file |
||
| 101 | */ |
||
| 102 | protected function loadFile($path): array |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Merges given configs and writes at given name. |
||
| 111 | * |
||
| 112 | * @return Config |
||
| 113 | */ |
||
| 114 | public function build(): self |
||
| 115 | { |
||
| 116 | $this->values = $this->calcValues($this->sources); |
||
| 117 | |||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function write(): self |
||
| 122 | { |
||
| 123 | $this->writeFile($this->getOutputPath(), $this->values); |
||
| 124 | |||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function calcValues(array $sources): array |
||
| 134 | } |
||
| 135 | |||
| 136 | protected function writeFile(string $path, array $data): void |
||
| 137 | { |
||
| 138 | $depth = $this->findDepth(); |
||
| 139 | $baseDir = $depth > 0 ? "dirname(__DIR__, $depth)" : '__DIR__'; |
||
| 140 | |||
| 141 | $content = $this->replaceMarkers(implode("\n\n", array_filter([ |
||
| 142 | 'header' => '<?php', |
||
| 143 | 'baseDir' => "\$baseDir = $baseDir;", |
||
| 144 | 'dotenv' => $this->hasEnv() |
||
| 145 | ? "\$_ENV = array_merge((array) require __DIR__ . '/dotenv.php', (array) \$_ENV);" : '', |
||
| 146 | 'defines' => $this->hasConstants() ? $this->builder->getConfig('defines')->buildRequires() : '', |
||
| 147 | 'params' => $this->hasParams() ? "\$params = require __DIR__ . '/params.php';" : '', |
||
| 148 | 'content' => $this->renderVars($data), |
||
| 149 | ]))); |
||
| 150 | $this->contentWriter->write($path, $content . "\n"); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function hasEnv(): bool |
||
| 154 | { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function hasConstants(): bool |
||
| 159 | { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function hasParams(): bool |
||
| 164 | { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | private function findDepth(): int |
||
| 169 | { |
||
| 170 | $outDir = dirname($this->normalizePath($this->getOutputPath())); |
||
| 171 | $diff = substr($outDir, strlen($this->getBaseDir())); |
||
| 172 | |||
| 173 | return substr_count($diff, '/'); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param array $vars array to be exported |
||
| 178 | * @return string |
||
| 179 | * @throws ReflectionException |
||
| 180 | */ |
||
| 181 | private function renderVars(array $vars): string |
||
| 182 | { |
||
| 183 | return 'return ' . Helper::exportVar($vars) . ';'; |
||
| 184 | } |
||
| 185 | |||
| 186 | private function replaceMarkers(string $content): string |
||
| 187 | { |
||
| 188 | return str_replace( |
||
| 189 | ["'" . self::BASE_DIR_MARKER, "'?" . self::BASE_DIR_MARKER], |
||
| 190 | ["\$baseDir . '", "'?' . \$baseDir . '"], |
||
| 191 | $content |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Substitute output paths in given data array recursively with marker. |
||
| 197 | * |
||
| 198 | * @param array $data |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | protected function substituteOutputDirs(array $data): array |
||
| 202 | { |
||
| 203 | $dir = $this->normalizePath($this->getBaseDir()); |
||
| 204 | |||
| 205 | return $this->substitutePaths($data, $dir, self::BASE_DIR_MARKER); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Normalizes given path with given directory separator. |
||
| 210 | * Default forced to Unix directory separator for substitutePaths to work properly in Windows. |
||
| 211 | * |
||
| 212 | * @param string $path path to be normalized |
||
| 213 | * @param string $ds directory separator |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | private function normalizePath($path): string |
||
| 217 | { |
||
| 218 | return rtrim(strtr($path, '/\\', '//'), '/'); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Substitute all paths in given array recursively with alias if applicable. |
||
| 223 | * |
||
| 224 | * @param array $data |
||
| 225 | * @param string $dir |
||
| 226 | * @param string $alias |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | private function substitutePaths($data, $dir, $alias): array |
||
| 230 | { |
||
| 231 | foreach ($data as &$value) { |
||
| 232 | if (is_string($value)) { |
||
| 233 | $value = $this->substitutePath($value, $dir, $alias); |
||
| 234 | } elseif (is_array($value)) { |
||
| 235 | $value = $this->substitutePaths($value, $dir, $alias); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | return $data; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Substitute path with alias if applicable. |
||
| 244 | * |
||
| 245 | * @param string $path |
||
| 246 | * @param string $dir |
||
| 247 | * @param string $alias |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | private function substitutePath($path, $dir, $alias): string |
||
| 266 | } |
||
| 267 | |||
| 268 | private function getBaseDir(): string |
||
| 269 | { |
||
| 270 | return dirname(__DIR__, 5); |
||
| 271 | } |
||
| 272 | |||
| 273 | protected function getOutputPath(string $name = null): string |
||
| 276 | } |
||
| 277 | } |
||
| 278 |