Total Complexity | 40 |
Total Lines | 256 |
Duplicated Lines | 0 % |
Changes | 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 |
||
45 | { |
||
46 | $config = new Config($builder, $this->name); |
||
47 | $config->sources = $this->sources; |
||
48 | $config->values = $this->values; |
||
49 | |||
50 | return $config; |
||
51 | } |
||
52 | |||
53 | public function getValues(): array |
||
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 |
||
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 |
||
119 | } |
||
120 | |||
121 | public function write(): self |
||
126 | } |
||
127 | |||
128 | protected function calcValues(array $sources): array |
||
129 | { |
||
130 | $values = call_user_func_array([ConfigMergeHelper::class, 'mergeConfig'], $sources); |
||
131 | |||
132 | return $this->substituteOutputDirs($values); |
||
133 | } |
||
134 | |||
135 | protected function writeFile(string $path, array $data): void |
||
136 | { |
||
137 | $depth = $this->findDepth(); |
||
138 | $baseDir = $depth > 0 ? "dirname(__DIR__, $depth)" : '__DIR__'; |
||
139 | |||
140 | $params = $this->paramsRequired() ? "(array) require __DIR__ . '/params.php'" : '[]'; |
||
141 | $constants = $this->constantsRequired() ? $this->builder->getConfig('constants')->buildRequires() : ''; |
||
142 | $envs = $this->envsRequired() ? "\$_ENV = array_merge((array) require __DIR__ . '/envs.php', \$_ENV);" : ''; |
||
143 | $variables = Helper::exportVar($data); |
||
144 | |||
145 | $content = <<<PHP |
||
146 | <?php |
||
147 | \$baseDir = {$baseDir}; |
||
148 | \$params = {$params}; |
||
149 | {$constants} |
||
150 | {$envs} |
||
151 | return {$variables}; |
||
152 | PHP; |
||
153 | |||
154 | $this->contentWriter->write($path, $this->replaceMarkers($content) . "\n"); |
||
155 | } |
||
156 | |||
157 | public function envsRequired(): bool |
||
158 | { |
||
159 | return true; |
||
160 | } |
||
161 | |||
162 | public function constantsRequired(): bool |
||
163 | { |
||
164 | return true; |
||
165 | } |
||
166 | |||
167 | public function paramsRequired(): bool |
||
168 | { |
||
169 | return true; |
||
170 | } |
||
171 | |||
172 | private function findDepth(): int |
||
173 | { |
||
174 | $outDir = realpath(dirname($this->normalizePath($this->getOutputPath()))); |
||
175 | $diff = substr($outDir, strlen(realpath($this->getBaseDir()))); |
||
176 | |||
177 | return substr_count($diff, '/'); |
||
178 | } |
||
179 | |||
180 | private function replaceMarkers(string $content): string |
||
181 | { |
||
182 | return str_replace( |
||
183 | ["'" . self::BASE_DIR_MARKER, "'?" . self::BASE_DIR_MARKER], |
||
184 | ["\$baseDir . '", "'?' . \$baseDir . '"], |
||
185 | $content |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Substitute output paths in given data array recursively with marker. |
||
191 | * |
||
192 | * @param array $data |
||
193 | * @return array |
||
194 | */ |
||
195 | protected function substituteOutputDirs(array $data): array |
||
196 | { |
||
197 | $dir = $this->normalizePath($this->getBaseDir()); |
||
198 | |||
199 | return $this->substitutePaths($data, $dir, self::BASE_DIR_MARKER); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Normalizes given path with given directory separator. |
||
204 | * Default forced to Unix directory separator for substitutePaths to work properly in Windows. |
||
205 | * |
||
206 | * @param string $path path to be normalized |
||
207 | * @param string $ds directory separator |
||
208 | * @return string |
||
209 | */ |
||
210 | private function normalizePath($path): string |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Substitute all paths in given array recursively with alias if applicable. |
||
217 | * |
||
218 | * @param array $data |
||
219 | * @param string $dir |
||
220 | * @param string $alias |
||
221 | * @return array |
||
222 | */ |
||
223 | private function substitutePaths($data, $dir, $alias): array |
||
224 | { |
||
225 | foreach ($data as &$value) { |
||
226 | if (is_string($value)) { |
||
227 | $value = $this->substitutePath($value, $dir, $alias); |
||
228 | } elseif (is_array($value)) { |
||
229 | $value = $this->substitutePaths($value, $dir, $alias); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $data; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Substitute path with alias if applicable. |
||
238 | * |
||
239 | * @param string $path |
||
240 | * @param string $dir |
||
241 | * @param string $alias |
||
242 | * @return string |
||
243 | */ |
||
244 | private function substitutePath($path, $dir, $alias): string |
||
260 | } |
||
261 | |||
262 | private function getBaseDir(): string |
||
263 | { |
||
264 | return $this->builder->getBaseDir(); |
||
265 | } |
||
266 | |||
267 | protected function getOutputPath(string $name = null): string |
||
270 | } |
||
271 | } |
||
272 |