Complex classes like SystemParamsService 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 SystemParamsService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class SystemParamsService |
||
21 | { |
||
22 | const ARRAY_KEYS_SEPARATOR = '.'; |
||
23 | const PARAM_IS_NOT_SET = 'CONFIG_VALUE_IS_NOT_SET'; |
||
24 | const CACHE_KEY = 'YII_SYSTEM_PARAM'; |
||
25 | |||
26 | /** |
||
27 | * Cache duration |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | public $cacheDuration = 0; |
||
32 | |||
33 | /** |
||
34 | * Params array |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $params = []; |
||
39 | |||
40 | /** |
||
41 | * Get system param |
||
42 | * |
||
43 | * @param string $param |
||
44 | * |
||
45 | * @return mixed |
||
46 | */ |
||
47 | public function getParam(string $param) |
||
55 | |||
56 | /** |
||
57 | * Prepare params |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | protected function prepareParams() |
||
82 | |||
83 | /** |
||
84 | * Load params from db. |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | protected function loadParamsFromCache() |
||
98 | |||
99 | /** |
||
100 | * Load params from db. |
||
101 | * |
||
102 | * @return bolean |
||
103 | */ |
||
104 | protected function loadParamsFromDb() |
||
119 | |||
120 | /** |
||
121 | * Merge params |
||
122 | * |
||
123 | * @param $params |
||
124 | * @param bool $autoUpdate |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function mergeParams(array $params, bool $autoUpdate = false) |
||
191 | |||
192 | /** |
||
193 | * Flush system params cache |
||
194 | */ |
||
195 | public static function flushCache() |
||
201 | |||
202 | /** |
||
203 | * Generate array of keys recursively in string. |
||
204 | * |
||
205 | * @param $dataArray |
||
206 | * |
||
207 | * @return array |
||
208 | * |
||
209 | * @author Virchenko Maksim <[email protected]> |
||
210 | */ |
||
211 | protected function getArrayKeysRecursivelyInString(array $dataArray): array |
||
227 | |||
228 | /** |
||
229 | * Return param value or false if param not exist. |
||
230 | * |
||
231 | * @param $pk |
||
232 | * @param $array |
||
233 | * |
||
234 | * @return bool |
||
235 | * |
||
236 | * @author Chaykovskiy Roman |
||
237 | */ |
||
238 | protected function getParamValueByKey($pk, $array) |
||
251 | |||
252 | /** |
||
253 | * Generate multidimensional array for pk with value. |
||
254 | * |
||
255 | * @param $pk can be string like 'a|b...|n' |
||
256 | * @param $value |
||
257 | * |
||
258 | * @return array |
||
259 | * |
||
260 | * @author Chaykovskiy Roman |
||
261 | */ |
||
262 | protected function generateArrayForParamValue($pk, $value) |
||
281 | } |
||
282 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..