Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Intraface_Setting 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 Intraface_Setting, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Intraface_Setting |
||
13 | { |
||
14 | /** |
||
15 | * @var object |
||
16 | */ |
||
17 | private $db; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $system; |
||
23 | |||
24 | /** |
||
25 | * @var integer |
||
26 | */ |
||
27 | private $user_id; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | private $intranet_id; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $settings = array(); |
||
38 | |||
39 | /** |
||
40 | * @var boolean |
||
41 | */ |
||
42 | protected $is_loaded = false; |
||
43 | |||
44 | /** |
||
45 | * Checks whether setting is in system |
||
46 | * |
||
47 | * @param integer $intranet_id |
||
48 | * @param integer $user_id |
||
49 | * |
||
50 | * @return void |
||
|
|||
51 | */ |
||
52 | 14 | function __construct($intranet_id, $user_id = 0) |
|
63 | |||
64 | /** |
||
65 | * Checks whether setting is in system |
||
66 | * |
||
67 | * @param string $setting to test |
||
68 | * |
||
69 | * @return boolean or throws exception |
||
70 | */ |
||
71 | 13 | private function checkSystem($setting) |
|
79 | |||
80 | /** |
||
81 | * Checks whether type is a valid type |
||
82 | * |
||
83 | * @param string $type to test |
||
84 | * |
||
85 | * @return boolean or throws exception |
||
86 | */ |
||
87 | 12 | private function checkType($type) |
|
95 | |||
96 | /** |
||
97 | * Checks whether the user is logged in |
||
98 | * |
||
99 | * @return boolean |
||
100 | */ |
||
101 | 12 | private function checkLogin() |
|
109 | |||
110 | /** |
||
111 | * Sets a certain setting |
||
112 | * |
||
113 | * @access protected, however to be tested we kept it public |
||
114 | * |
||
115 | * @param string $type Can be either system, intranet, user |
||
116 | * @param string $setting The actual setting |
||
117 | * @param integer $sub_id @todo What is this exactly |
||
118 | * |
||
119 | * @return boolean |
||
120 | */ |
||
121 | 12 | function set($type, $setting, $value, $sub_id = 0) |
|
154 | |||
155 | /** |
||
156 | * Checks whether a setting is set |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | 1 | public function getSettings() |
|
164 | |||
165 | /** |
||
166 | * Loads settings |
||
167 | * |
||
168 | * @return void |
||
169 | */ |
||
170 | 7 | private function loadSettings() |
|
179 | |||
180 | /** |
||
181 | * Returns whether the settings has already been loaded |
||
182 | * |
||
183 | * @return boolean |
||
184 | */ |
||
185 | 7 | private function isLoaded() |
|
189 | |||
190 | /** |
||
191 | * Gets a certain setting |
||
192 | * |
||
193 | * @access protected, however to be tested we kept it public |
||
194 | * |
||
195 | * @param string $type Can be either system, intranet, user |
||
196 | * @param string $setting The actual setting |
||
197 | * @param integer $sub_id @todo What is this exactly |
||
198 | * |
||
199 | * @return boolean |
||
200 | */ |
||
201 | 7 | public function get($type, $setting, $sub_id = 0) |
|
252 | |||
253 | /** |
||
254 | * Checks whether a setting is set |
||
255 | * |
||
256 | * @access protected, however to be tested we kept it public |
||
257 | * |
||
258 | * @param string $type Can be either system, intranet, user |
||
259 | * @param string $setting The actual setting |
||
260 | * @param integer $sub_id @todo What is this exactly |
||
261 | * |
||
262 | * @return boolean |
||
263 | */ |
||
264 | 3 | function isSettingSet($type, $setting, $sub_id = 0) |
|
291 | |||
292 | /** |
||
293 | * Deletes a setting |
||
294 | * |
||
295 | * @access protected, however to be tested we kept it public |
||
296 | * |
||
297 | * @param string $type Can be either system, intranet, user |
||
298 | * @param string $setting The actual setting |
||
299 | * @param integer $sub_id @todo What is this exactly |
||
300 | * |
||
301 | * @return boolean |
||
302 | */ |
||
303 | 1 | function delete($type, $setting, $sub_id = 0) |
|
334 | } |
||
335 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.