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 Captcha 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 Captcha, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Captcha |
||
41 | { |
||
42 | /** |
||
43 | * @var Configuration |
||
44 | */ |
||
45 | private $_config = null; |
||
46 | |||
47 | /** |
||
48 | * The phpMyFAQ session id. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $sids; |
||
53 | |||
54 | /** |
||
55 | * Array of fonts. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $fonts = []; |
||
60 | |||
61 | /** |
||
62 | * The captcha code. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $code = ''; |
||
67 | |||
68 | /** |
||
69 | * Array of characters. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private $letters = array( |
||
74 | '1', '2', '3', '4', '5', '6', '7', '8', '9', |
||
75 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', |
||
76 | 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
||
77 | 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
||
78 | ); |
||
79 | |||
80 | /** |
||
81 | * Length of the captcha code. |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | public $caplength = 6; |
||
86 | |||
87 | /** |
||
88 | * Width of the image. |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | private $width = 165; |
||
93 | |||
94 | /** |
||
95 | * Height of the image. |
||
96 | * |
||
97 | * @var int |
||
98 | */ |
||
99 | private $height = 40; |
||
100 | |||
101 | /** |
||
102 | * JPEG quality in percents. |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | private $quality = 60; |
||
107 | |||
108 | /** |
||
109 | * Random background color RGB components. |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | private $_backgroundColor; |
||
114 | |||
115 | /** |
||
116 | * Generated image. |
||
117 | * |
||
118 | * @var resource |
||
119 | */ |
||
120 | private $img; |
||
121 | |||
122 | /** |
||
123 | * The user agent string. |
||
124 | * |
||
125 | * @var string |
||
126 | */ |
||
127 | private $userAgent; |
||
128 | |||
129 | /** |
||
130 | * Timestamp. |
||
131 | * |
||
132 | * @var int |
||
133 | */ |
||
134 | private $timestamp; |
||
135 | |||
136 | /** |
||
137 | * Constructor. |
||
138 | * |
||
139 | * @param Configuration $config |
||
140 | */ |
||
141 | public function __construct(Configuration $config) |
||
149 | |||
150 | // |
||
151 | // public functions |
||
152 | // |
||
153 | |||
154 | /** |
||
155 | * Setter for session id. |
||
156 | * |
||
157 | * @param int $sid session id |
||
158 | */ |
||
159 | public function setSessionId($sid) |
||
163 | |||
164 | /** |
||
165 | * Setter for the captcha code length. |
||
166 | * |
||
167 | * @param int $length Length of captch code |
||
168 | */ |
||
169 | public function setCodeLength($length = 6) |
||
173 | |||
174 | /** |
||
175 | * Gives the HTML output code for the Captcha. |
||
176 | * |
||
177 | * @param string $action The action parameter |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function printCaptcha($action) |
||
197 | |||
198 | /** |
||
199 | * Draw the Captcha. |
||
200 | */ |
||
201 | public function showCaptchaImg() |
||
216 | |||
217 | /** |
||
218 | * Gets the Captcha from the DB. |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getCaptchaCode() |
||
232 | |||
233 | /** |
||
234 | * Validate the Captcha. |
||
235 | * |
||
236 | * @param string $captchaCode Captcha code |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function validateCaptchaCode($captchaCode) |
||
282 | |||
283 | /** |
||
284 | * This function checks the provided captcha code |
||
285 | * if the captcha code spam protection has been activated from the general PMF configuration. |
||
286 | * |
||
287 | * @param string $code Captcha Code |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function checkCaptchaCode($code) |
||
299 | |||
300 | // |
||
301 | // private functions |
||
302 | // |
||
303 | |||
304 | /** |
||
305 | * Draw random lines. |
||
306 | * |
||
307 | * @return resource |
||
308 | */ |
||
309 | private function drawlines() |
||
338 | |||
339 | /** |
||
340 | * Draw the Text. |
||
341 | * |
||
342 | * @return resource |
||
343 | */ |
||
344 | private function drawText() |
||
400 | |||
401 | /** |
||
402 | * Create the background. |
||
403 | * |
||
404 | * @return resource |
||
405 | */ |
||
406 | private function createBackground() |
||
424 | |||
425 | /** |
||
426 | * Generate a Captcha Code. |
||
427 | * |
||
428 | * @param int $caplength Length of captch code |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | private function generateCaptchaCode($caplength) |
||
464 | |||
465 | /** |
||
466 | * Save the Captcha. |
||
467 | * |
||
468 | * @return bool |
||
469 | */ |
||
470 | private function saveCaptcha() |
||
512 | |||
513 | /** |
||
514 | * Remove the Captcha. |
||
515 | * |
||
516 | * @param string $captchaCode Captch code |
||
517 | */ |
||
518 | private function removeCaptcha($captchaCode = null) |
||
526 | |||
527 | /** |
||
528 | * Delete old captcha records. |
||
529 | * |
||
530 | * During normal use the <b>faqcaptcha</b> table would be empty, on average: |
||
531 | * each record is created when a captcha image is showed to the user |
||
532 | * and deleted upon a successful matching, so, on average, a record |
||
533 | * in this table is probably related to a spam attack. |
||
534 | * |
||
535 | * @param int $time The time (sec) to define a captcha code old and ready |
||
536 | * to be deleted (default: 1 week) |
||
537 | */ |
||
538 | private function garbageCollector($time = 604800) |
||
564 | |||
565 | /** |
||
566 | * Get Fonts. |
||
567 | * |
||
568 | * @return array |
||
569 | */ |
||
570 | private function getFonts() |
||
574 | } |
||
575 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.