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 |
||
7 | class QuestionAddModel extends AbstractModel |
||
8 | { |
||
9 | public function add($examID, $post) |
||
20 | |||
21 | /** |
||
22 | * Add question for exam. |
||
23 | * |
||
24 | * @param int $examID |
||
25 | * @param array $question |
||
26 | * |
||
27 | * @return book |
||
28 | */ |
||
29 | View Code Duplication | private function addQuestion($examID, $question) |
|
39 | |||
40 | /** |
||
41 | * Add correct answer to question. |
||
42 | * |
||
43 | * @param int $questionID Question id |
||
44 | * @param int $correct Correct anwer id |
||
45 | */ |
||
46 | View Code Duplication | public function addCorrectAnswerToQuestion($questionID, $correct) |
|
55 | |||
56 | /** |
||
57 | * Add answers for exam. |
||
58 | * |
||
59 | * @param int $examID |
||
60 | * @param int $questionID |
||
61 | * @param int $correct |
||
62 | * @param array $answers |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | private function addAnswers($examID, $questionID, $correct, $answers) |
||
92 | } |
||
93 |
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.