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:
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 12 | class Captcha_Core |
||
| 13 | { |
||
| 14 | |||
| 15 | // Captcha singleton |
||
| 16 | protected static $instance; |
||
| 17 | |||
| 18 | // Style-dependent Captcha driver |
||
| 19 | protected $driver; |
||
| 20 | |||
| 21 | // Config values |
||
| 22 | public static $config = array( |
||
| 23 | 'style' => 'basic', |
||
| 24 | 'width' => 150, |
||
| 25 | 'height' => 50, |
||
| 26 | 'complexity' => 4, |
||
| 27 | 'background' => '', |
||
| 28 | 'fontpath' => '', |
||
| 29 | 'fonts' => array(), |
||
| 30 | 'promote' => false, |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Singleton instance of Captcha. |
||
| 35 | * |
||
| 36 | * @return object |
||
| 37 | */ |
||
| 38 | public static function instance() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructs and returns a new Captcha object. |
||
| 48 | * |
||
| 49 | * @param string config group name |
||
| 50 | * @return object |
||
|
|
|||
| 51 | */ |
||
| 52 | public static function factory($group = null) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructs a new Captcha object. |
||
| 59 | * |
||
| 60 | * @throws Kohana_Exception |
||
| 61 | * @param string config group name |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | public function __construct($group = null) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Validates a Captcha response and updates response counter. |
||
| 141 | * |
||
| 142 | * @param string captcha response |
||
| 143 | * @return boolean |
||
| 144 | */ |
||
| 145 | public static function valid($response) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Gets or sets the number of valid Captcha responses for this session. |
||
| 177 | * |
||
| 178 | * @param integer new counter value |
||
| 179 | * @param boolean trigger invalid counter (for internal use only) |
||
| 180 | * @return integer counter value |
||
| 181 | */ |
||
| 182 | public function valid_count($new_count = null, $invalid = false) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Gets or sets the number of invalid Captcha responses for this session. |
||
| 210 | * |
||
| 211 | * @param integer new counter value |
||
| 212 | * @return integer counter value |
||
| 213 | */ |
||
| 214 | public function invalid_count($new_count = null) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Resets the Captcha response counters and removes the count sessions. |
||
| 221 | * |
||
| 222 | * @return void |
||
| 223 | */ |
||
| 224 | public function reset_count() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Checks whether user has been promoted after having given enough valid responses. |
||
| 232 | * |
||
| 233 | * @param integer valid response count threshold |
||
| 234 | * @return boolean |
||
| 235 | */ |
||
| 236 | public function promoted($threshold = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns or outputs the Captcha challenge. |
||
| 254 | * |
||
| 255 | * @param boolean TRUE to output html, e.g. <img src="#" /> |
||
| 256 | * @return mixed html string or void |
||
| 257 | */ |
||
| 258 | public function render($html = true) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Magically outputs the Captcha challenge. |
||
| 265 | * |
||
| 266 | * @return mixed |
||
| 267 | */ |
||
| 268 | public function __toString() |
||
| 272 | } // End Captcha Class |
||
| 273 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.