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 |
||
10 | class AdminArticle { |
||
11 | private $error_title; |
||
12 | private $error_article; |
||
13 | |||
14 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
15 | public function __construct() { |
||
18 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
19 | |||
20 | |||
21 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
22 | /** |
||
23 | * @param $title |
||
24 | * @return bool |
||
25 | * function that verify if title of the article is ok |
||
26 | */ |
||
27 | private function getTestTitle($title) { |
||
49 | |||
50 | /** |
||
51 | * @param $article |
||
52 | * @return bool |
||
53 | * function that verify if article is ok |
||
54 | */ |
||
55 | private function getTestArticle($article) { |
||
63 | |||
64 | /** |
||
65 | * this function get last articles |
||
66 | */ |
||
67 | View Code Duplication | public function getAllArticle() { |
|
94 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
95 | |||
96 | |||
97 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
98 | /** |
||
99 | * @param $title |
||
100 | * @param $categories |
||
101 | * @param $article |
||
102 | * @param $state |
||
103 | * @return bool |
||
104 | * function to add an article and his categories |
||
105 | */ |
||
106 | public function setAddArticle($title, $categories, $article, $state) { |
||
127 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
128 | } |
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.