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 |
||
3 | class FlipsideCAPTCHA implements JsonSerializable |
||
4 | { |
||
5 | public $random_id; |
||
6 | private $validIDs; |
||
7 | |||
8 | public static function get_valid_captcha_ids() |
||
20 | |||
21 | public static function get_all() |
||
34 | |||
35 | public static function save_new_captcha($question, $hint, $answer) |
||
36 | { |
||
37 | $dataset = DataSetFactory::getDataSetByName('profiles'); |
||
38 | $datatable = $dataset['captcha']; |
||
39 | return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer)); |
||
40 | } |
||
41 | |||
42 | public function __construct() |
||
48 | |||
49 | View Code Duplication | public function get_question() |
|
60 | |||
61 | View Code Duplication | public function get_hint() |
|
62 | { |
||
63 | $dataset = DataSetFactory::getDataSetByName('profiles'); |
||
64 | $datatable = $dataset['captcha']; |
||
65 | $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('hint')); |
||
66 | if($data === false) |
||
67 | { |
||
68 | return false; |
||
69 | } |
||
70 | return $data[0]['hint']; |
||
71 | } |
||
72 | |||
73 | View Code Duplication | private function get_answer() |
|
74 | { |
||
75 | $dataset = DataSetFactory::getDataSetByName('profiles'); |
||
76 | $datatable = $dataset['captcha']; |
||
77 | $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('answer')); |
||
78 | if($data === false) |
||
79 | { |
||
80 | return false; |
||
81 | } |
||
82 | return $data[0]['answer']; |
||
83 | } |
||
84 | |||
85 | public function is_answer_right($answer) |
||
86 | { |
||
87 | return strcasecmp($this->get_answer(), $answer) === 0; |
||
88 | } |
||
89 | |||
90 | public function draw_captcha($explination = true, $return = false, $ownForm = false) |
||
91 | { |
||
92 | $string = ''; |
||
93 | |||
94 | if($ownForm) |
||
95 | { |
||
96 | $string .= '<form id="flipcaptcha" name="flipcaptcha">'; |
||
97 | } |
||
98 | |||
99 | $string .= '<label for="captcha" class="col-sm-2 control-label">'.$this->get_question().'</label><div class="col-sm-10"><input class="form-control" type="text" id="captcha" name="captcha" placeholder="'.$this->get_hint().'" required/></div>'; |
||
100 | if($ownForm) |
||
101 | { |
||
102 | $string .= '</form>'; |
||
103 | } |
||
104 | if($explination) |
||
105 | { |
||
106 | $string .= '<div class="col-sm-10">The answer to this question may be found in the Burning Flipside Survival Guide. It may be found <a href="http://www.burningflipside.com/sg">here</a>.</div>'; |
||
107 | } |
||
108 | |||
109 | if(!$return) |
||
110 | { |
||
111 | echo $string; |
||
112 | } |
||
113 | return $string; |
||
114 | } |
||
115 | |||
116 | public function self_json_encode() |
||
117 | { |
||
118 | return json_encode($this->jsonSerialize()); |
||
119 | } |
||
120 | |||
121 | public function jsonSerialize() |
||
130 | } |
||
131 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
132 |
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.