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. 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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Config extends Object |
||
26 | { |
||
27 | /** |
||
28 | * @var string full path to a base directory to look for configs. |
||
29 | * You may use a path alias here. |
||
30 | */ |
||
31 | public $configDir; |
||
32 | |||
33 | /** |
||
34 | * @var bool whether to enable caching. |
||
35 | * The complete configuration will be analyzed and converted |
||
36 | * to a single PHP file which will be cared by a OPcode cacher so it will load almost immediately. |
||
37 | * @since 2.0 |
||
38 | */ |
||
39 | public $enableCache = false; |
||
40 | |||
41 | /** |
||
42 | * @var int number of seconds that a cached config can remain valid in a cache. |
||
43 | * Use `0` to never expire. |
||
44 | * @since 2.0 |
||
45 | */ |
||
46 | public $cacheDuration = 3600; |
||
47 | |||
48 | /** |
||
49 | * @var Cache|string|array the [[Cache]] object or the application component ID of the [[Cache]] object. |
||
50 | * It can also be an array that is used to create a [[Cache]] instance. |
||
51 | * @since 2.0 |
||
52 | */ |
||
53 | public $cache = [ |
||
54 | 'class' => 'sergeymakinen\yii\phpfilecache\Cache', |
||
55 | 'cachePath' => '@yii/../../../runtime/cache', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * @var bool|null whether to inject the cache config/instance into the main config. |
||
60 | * By default the cache config will only be included if there are `id` & `basePath` keys |
||
61 | * in the main config and no `cacheConfig` key in the `components` array. |
||
62 | * @see $cache |
||
63 | * @since 2.0 |
||
64 | */ |
||
65 | public $includeCacheConfig; |
||
66 | |||
67 | /** |
||
68 | * @var string|null tier name (e. g. `console`, `web`, `backend`, `frontend`). |
||
69 | */ |
||
70 | public $tier; |
||
71 | |||
72 | /** |
||
73 | * @var string|null environment name (e. g. `dev`, `test`, `prod`). |
||
74 | */ |
||
75 | public $env = YII_ENV; |
||
76 | |||
77 | /** |
||
78 | * @var string[] array of pathes relative to [[configDir]]. |
||
79 | * [[Config]] will look for configs in each directory in the order they are defined. |
||
80 | * You can use the following substitutions: |
||
81 | * |
||
82 | * | Name | Description |
||
83 | * | --- | --- |
||
84 | * | `{env}` | Config environment name ([[env]]). |
||
85 | * | `{tier}` | Config tier name ([[tier]]). |
||
86 | */ |
||
87 | public $dirs = ['']; |
||
88 | |||
89 | /** |
||
90 | * @var array|string[]|Loader[] config file configurations. |
||
91 | * Array of: |
||
92 | * |
||
93 | * - [[Loader]] instances |
||
94 | * - array that is used to create [[Loader]] instances |
||
95 | * - shortcuts |
||
96 | */ |
||
97 | public $files = []; |
||
98 | |||
99 | /** |
||
100 | * @var string[] registered config file loaders per extension. |
||
101 | */ |
||
102 | public $loaders = []; |
||
103 | |||
104 | /** |
||
105 | * @var Storage the internal config instance. |
||
106 | */ |
||
107 | protected $storage; |
||
108 | |||
109 | /** |
||
110 | * Loads and returns the [[Config]] instance from the configuration file. |
||
111 | * @param string $path the configuration file path. |
||
112 | * You may use a path alias here. Also the file may have any extension which is loadable by [[Config]] by default. |
||
113 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
114 | * @return static [[Config]] instance. |
||
115 | * @throws InvalidConfigException |
||
116 | */ |
||
117 | 3 | public static function fromFile($path, array $config = []) |
|
132 | |||
133 | /** |
||
134 | * @inheritDoc |
||
135 | * @throws InvalidConfigException |
||
136 | */ |
||
137 | 66 | public function init() |
|
150 | |||
151 | /** |
||
152 | * Returns whether the config is cached. |
||
153 | * @return bool whether the config is cached. |
||
154 | */ |
||
155 | 3 | public function getIsCached() |
|
159 | |||
160 | /** |
||
161 | * Compiles the config and writes it to the cache. |
||
162 | * @return bool whether caching was successful. |
||
163 | */ |
||
164 | 19 | public function cache() |
|
177 | |||
178 | /** |
||
179 | * Removes the cached config. |
||
180 | * @return bool whether flushing was successful. |
||
181 | */ |
||
182 | 10 | public function flushCache() |
|
186 | |||
187 | /** |
||
188 | * Loads the config from/ignoring the cache and returns it. |
||
189 | * @return array the config. |
||
190 | */ |
||
191 | 36 | public function load() |
|
204 | |||
205 | /** |
||
206 | * Loads the config from the cache. |
||
207 | * @return array|false the config or `false` if loading failed. |
||
208 | */ |
||
209 | 3 | protected function loadCached() |
|
229 | |||
230 | /** |
||
231 | * Loads the config ignoring the cache. |
||
232 | * @return array the config. |
||
233 | */ |
||
234 | 36 | protected function loadFresh() |
|
245 | |||
246 | /** |
||
247 | * Resolves file configurations into [[Loader]] instances. |
||
248 | * @return Loader[] [[Loader]] instances. |
||
249 | * @throws InvalidConfigException |
||
250 | */ |
||
251 | 64 | protected function resolveLoaders() |
|
285 | |||
286 | /** |
||
287 | * Maps a shortcut to an array for [[resolveLoaders()]]. |
||
288 | * @param mixed $key the array entry key. |
||
289 | * @param string $value the array entry value. |
||
290 | * @return array a configuration array. |
||
291 | */ |
||
292 | 12 | protected function resolveShortcut($key, $value) |
|
316 | |||
317 | /** |
||
318 | * Compiles all specified files according to their configurations. |
||
319 | */ |
||
320 | 19 | protected function compile() |
|
330 | |||
331 | /** |
||
332 | * Returns an array of default loaders available. |
||
333 | * @return string[] default loaders. |
||
334 | * @since 2.0 |
||
335 | */ |
||
336 | 64 | protected function defaultLoaders() |
|
346 | |||
347 | /** |
||
348 | * Calculates and returns a key based on some [[Config]] parameters. |
||
349 | * @return array the cache key. |
||
350 | * @since 2.0 |
||
351 | */ |
||
352 | 11 | protected function calculateCacheKey() |
|
360 | |||
361 | /** |
||
362 | * Merges the config with the cache config and returns it. |
||
363 | * @param array $config |
||
364 | * @return array |
||
365 | */ |
||
366 | 31 | private function includeCacheConfig(array $config) |
|
380 | } |
||
381 |
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.