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 XoopsCaptcha 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 XoopsCaptcha, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class XoopsCaptcha |
||
15 | { |
||
16 | public $active = true; |
||
17 | public $mode = 'text'; // potential values: image, text |
||
18 | public $config = []; |
||
19 | |||
20 | public $message = []; // Logging error messages |
||
21 | |||
22 | /** |
||
23 | * XoopsCaptcha constructor. |
||
24 | */ |
||
25 | public function __construct() |
||
32 | |||
33 | /** |
||
34 | * @return XoopsCaptcha |
||
35 | */ |
||
36 | public static function getInstance() |
||
45 | |||
46 | /** |
||
47 | * @param $name |
||
48 | * @param $val |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function setConfig($name, $val) |
||
63 | |||
64 | /** |
||
65 | * Set CAPTCHA mode |
||
66 | * |
||
67 | * For future possible modes, right now force to use text or image |
||
68 | * |
||
69 | * @param string $mode if no mode is set, just verify current mode |
||
70 | */ |
||
71 | public function setMode($mode = null) |
||
101 | |||
102 | /** |
||
103 | * Initializing the CAPTCHA class |
||
104 | * @param string $name |
||
105 | * @param null $skipmember |
||
106 | * @param null $num_chars |
||
107 | * @param null $fontsize_min |
||
108 | * @param null $fontsize_max |
||
109 | * @param null $background_type |
||
110 | * @param null $background_num |
||
111 | */ |
||
112 | public function init( |
||
134 | |||
135 | /** |
||
136 | * Verify user submission |
||
137 | * @param null $skipMember |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function verify($skipMember = null) |
||
177 | |||
178 | /** |
||
179 | * @return mixed|string |
||
180 | */ |
||
181 | public function getCaption() |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getMessage() |
||
193 | |||
194 | /** |
||
195 | * Destory historical stuff |
||
196 | * @param bool $clearSession |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function destroyGarbage($clearSession = false) |
||
218 | |||
219 | /** |
||
220 | * @return mixed|string |
||
221 | */ |
||
222 | public function render() |
||
250 | |||
251 | /** |
||
252 | * @return mixed |
||
253 | */ |
||
254 | public function loadForm() |
||
265 | } |
||
266 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.