Complex classes like SiteInfo 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 SiteInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class SiteInfo |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * If TRUE then setting will be saved and getted for each locale |
||
13 | * @var boolean |
||
14 | */ |
||
15 | public $useLocales = TRUE; |
||
16 | |||
17 | /** |
||
18 | * For items witch are same for all locales |
||
19 | * (only if $useLocales is TRUE) |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $nonLocaleKeys = [ |
||
23 | 'siteinfo_logo', |
||
24 | 'siteinfo_favicon', |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * Current locale |
||
29 | * @var string |
||
30 | */ |
||
31 | public $locale; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | public $locales = []; |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $siteinfo; |
||
44 | |||
45 | /** |
||
46 | * Path to folder where images will be uploaded |
||
47 | * ATTENTION! serves as url too!!! |
||
48 | * @var string |
||
49 | */ |
||
50 | public $imagesPath = '/uploads/images/'; |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public static $siteInfoObject; |
||
57 | |||
58 | /** |
||
59 | * Setting class variables |
||
60 | * @param string $locale locale to intiate class with |
||
61 | */ |
||
62 | public function __construct($locale = NULL) { |
||
84 | |||
85 | /** |
||
86 | * Sets all params in one array (mostly on saving) |
||
87 | * @param array $siteInfo |
||
88 | */ |
||
89 | public function setSiteInfoData(array $siteInfo) { |
||
114 | |||
115 | /** |
||
116 | * @param string $key |
||
117 | * @param string $lang |
||
118 | */ |
||
119 | private function contactsKeys($key, $lang) { |
||
146 | |||
147 | /** |
||
148 | * Saving data in DB |
||
149 | */ |
||
150 | public function save() { |
||
156 | |||
157 | /** |
||
158 | * Setting one value of site informations |
||
159 | * @param string $key |
||
160 | * @param string $value |
||
161 | * @param boolean $contacts (optional, default false) true if value need to be setted in contacts |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function setSiteInfoValue($key, $value, $contacts = FALSE) { |
||
192 | |||
193 | /** |
||
194 | * @param string $key |
||
195 | * @param bool|FALSE $contacts |
||
196 | */ |
||
197 | public function deleteSiteInfoValue($key, $contacts = FALSE) { |
||
215 | |||
216 | /** |
||
217 | * Returns all params in one array (for serialize) |
||
218 | * @param boolean $byLocale if true then will be returned data by locale, else all dataS |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getSiteInfoData($byLocale = FALSE) { |
||
249 | |||
250 | /** |
||
251 | * Returns siteinfo item by param |
||
252 | * @param string $name name of param |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getSiteInfo($name = NULL) { |
||
298 | |||
299 | /** |
||
300 | * Changing array structure relatively to that locales are use or not |
||
301 | */ |
||
302 | public function normalizeData() { |
||
327 | |||
328 | } |
This checks looks for cases where a variable has been assigned to itself.
This assignement can be removed without consequences.