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 |
||
28 | class XoopsCaptchaImage extends XoopsCaptchaMethod |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * XoopsCaptchaImage::isActive() |
||
33 | * |
||
34 | * @return bool |
||
35 | */ |
||
36 | 1 | public function isActive() |
|
37 | { |
||
38 | 1 | if (!extension_loaded('gd')) { |
|
39 | trigger_error('GD library is not loaded', E_USER_WARNING); |
||
40 | return false; |
||
41 | View Code Duplication | } else { |
|
42 | $required_functions = array( |
||
43 | 1 | 'imagecreatetruecolor' , |
|
44 | 'imagecolorallocate' , |
||
45 | 'imagefilledrectangle' , |
||
46 | 'imagejpeg' , |
||
47 | 'imagedestroy' , |
||
48 | 'imageftbbox'); |
||
49 | 1 | foreach ($required_functions as $func) { |
|
50 | 1 | if (!function_exists($func)) { |
|
51 | trigger_error('Function ' . $func . ' is not defined', E_USER_WARNING); |
||
52 | return false; |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | 1 | return true; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * XoopsCaptchaImage::render() |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 1 | public function render() |
|
65 | { |
||
66 | 1 | $xoops_url = \XoopsBaseConfig::get('url'); |
|
67 | $js = "<script type='text/javascript'> |
||
68 | function xoops_captcha_refresh(imgId) |
||
69 | { |
||
70 | 1 | xoopsGetElementById(imgId).src = '" . $xoops_url . "/class/captcha/image/scripts/image.php?refresh='+Math.random(); |
|
71 | } |
||
72 | </script>"; |
||
73 | 1 | $image = $this->loadImage(); |
|
74 | 1 | $image .= "<br /><a href=\"javascript: xoops_captcha_refresh('" . ($this->config['name']) . "')\">" . XoopsLocale::CLICK_TO_REFRESH_IMAGE_IF_NOT_CLEAR . "</a>"; |
|
75 | 1 | $input = '<input type="text" name="' . $this->config['name'] . '" id="' . $this->config['name'] . '" size="' . $this->config['num_chars'] . '" maxlength="' . $this->config['num_chars'] . '" value="" required>'; |
|
76 | 1 | $rule = XoopsLocale::INPUT_LETTERS_IN_THE_IMAGE; |
|
77 | 1 | $rule .= '<br />' . (empty($this->config['casesensitive']) ? XoopsLocale::CODE_IS_CASE_INSENSITIVE : XoopsLocale::CODE_IS_CASE_SENSITIVE); |
|
78 | 1 | View Code Duplication | if (!empty($this->config['maxattempts'])) { |
79 | $rule .= '<br />' . sprintf(XoopsLocale::F_MAXIMUM_ATTEMPTS, $this->config['maxattempts']); |
||
80 | } |
||
81 | 1 | return $js . $image . '<br /><br />' . $input . '<br />' . $rule; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * XoopsCaptchaImage::loadImage() |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 2 | public function loadImage() |
|
94 | } |
||
95 |