Complex classes like ConfigurationLoader 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ConfigurationLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ConfigurationLoader |
||
27 | { |
||
28 | /** |
||
29 | * Config passed in the constructor |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $_initialConfig; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $_configArray = null; |
||
39 | |||
40 | /** |
||
41 | * Cache |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $_distConfig; |
||
46 | |||
47 | /** |
||
48 | * Cache |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $_pluginConfig; |
||
53 | |||
54 | /** |
||
55 | * Cache |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $_systemConfig; |
||
60 | |||
61 | /** |
||
62 | * Cache |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $_userConfig; |
||
67 | |||
68 | /** |
||
69 | * Cache |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $_projectConfig; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $_customConfigFilename = 'n98-magerun.yaml'; |
||
79 | |||
80 | /** |
||
81 | * @var bool |
||
82 | */ |
||
83 | protected $_isPharMode = true; |
||
84 | |||
85 | /** |
||
86 | * @var OutputInterface |
||
87 | */ |
||
88 | protected $_output; |
||
89 | |||
90 | /** |
||
91 | * Load config |
||
92 | * If $magentoRootFolder is null, only non-project config is loaded |
||
93 | * |
||
94 | * @param array $config |
||
95 | * @param bool $isPharMode |
||
96 | * @param OutputInterface $output |
||
97 | */ |
||
98 | public function __construct(array $config, $isPharMode, OutputInterface $output) |
||
104 | |||
105 | /** |
||
106 | * @param bool $loadExternalConfig |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getPartialConfig($loadExternalConfig = true) |
||
120 | |||
121 | /** |
||
122 | * @param string $magentoRootFolder |
||
123 | * @param bool $loadExternalConfig |
||
124 | * @param string $magerunStopFileFolder |
||
125 | */ |
||
126 | public function loadStageTwo($magentoRootFolder, $loadExternalConfig = true, $magerunStopFileFolder = '') |
||
138 | |||
139 | /** |
||
140 | * @throws \ErrorException |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function toArray() |
||
152 | |||
153 | /** |
||
154 | * @param array $initConfig |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | protected function loadDistConfig(array $initConfig) |
||
170 | |||
171 | /** |
||
172 | * Check if there is a global config file in /etc folder |
||
173 | * |
||
174 | * @param array $config |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function loadSystemConfig(array $config) |
||
199 | |||
200 | /** |
||
201 | * Load config from all installed bundles |
||
202 | * |
||
203 | * @param array $config |
||
204 | * @param string $magentoRootFolder |
||
205 | * |
||
206 | * @return array |
||
207 | */ |
||
208 | public function loadPluginConfig(array $config, $magentoRootFolder) |
||
271 | |||
272 | /** |
||
273 | * Check if there is a user config file. ~/.n98-magerun.yaml |
||
274 | * |
||
275 | * @param array $config |
||
276 | * @param string $magentoRootFolder [optional] |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | public function loadUserConfig(array $config, $magentoRootFolder = null) |
||
294 | |||
295 | /** |
||
296 | * MAGENTO_ROOT/app/etc/n98-magerun.yaml |
||
297 | * |
||
298 | * @param string $magentoRootFolder |
||
299 | * @param string $magerunStopFileFolder |
||
300 | * @param array $config |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | public function loadProjectConfig($magentoRootFolder, $magerunStopFileFolder, array $config) |
||
324 | |||
325 | /** |
||
326 | * Loads a plugin config file and merges it to plugin config |
||
327 | * |
||
328 | * @param string $magentoRootFolder |
||
329 | * @param SplFileInfo $file |
||
330 | */ |
||
331 | protected function registerPluginConfigFile($magentoRootFolder, $file) |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getVendorDir() |
||
374 | |||
375 | /** |
||
376 | * @return string |
||
377 | */ |
||
378 | public function getConfigurationLoaderDir() |
||
382 | |||
383 | /** |
||
384 | * @param string $message |
||
385 | */ |
||
386 | private function logDebug($message) |
||
392 | |||
393 | /** |
||
394 | * @param string $message |
||
395 | */ |
||
396 | private function log($message) |
||
400 | } |
||
401 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.