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 AdminCategory { |
||
11 | |||
12 | |||
13 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
14 | public function __construct() { |
||
17 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
18 | |||
19 | |||
20 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
21 | /** |
||
22 | * @param $name |
||
23 | * @param null $id |
||
24 | * @return bool |
||
25 | * test if a category exist |
||
26 | */ |
||
27 | private function getTestCategoryExist($name, $id=null) { |
||
47 | |||
48 | /** |
||
49 | * function t get name of a category with it url |
||
50 | */ |
||
51 | public function getNameCategoryUrl() { |
||
52 | $dbc = App::getDb(); |
||
53 | |||
54 | $query = $dbc->select()->from("_blog_category")->where("url_category", "=", Blog::$router_parameter)->get(); |
||
55 | |||
56 | if (count($query) == 1) { |
||
57 | foreach ($query as $obj) { |
||
58 | Blog::setValues([ |
||
59 | "category_name" => $obj->category, |
||
60 | "id_category" => $obj->ID_category |
||
61 | ]); |
||
62 | |||
63 | return $obj->category; |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
68 | |||
69 | |||
70 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
71 | /** |
||
72 | * @param $name |
||
73 | * @return bool |
||
74 | * function to add a category |
||
75 | */ |
||
76 | View Code Duplication | public function setAddCategory($name) { |
|
88 | |||
89 | /** |
||
90 | * @param $name |
||
91 | * @param $id |
||
92 | * @return bool |
||
93 | * function to edit the name of a category |
||
94 | */ |
||
95 | View Code Duplication | public function setEditCategory($name, $id) { |
|
109 | |||
110 | /** |
||
111 | * @param $categories |
||
112 | * @param $id_article |
||
113 | * add list of categories to an article |
||
114 | */ |
||
115 | public function setCategoriesArticle($categories, $id_article) { |
||
127 | |||
128 | /** |
||
129 | * @param $categories |
||
130 | * @param $id_article |
||
131 | * function to update catgories, in first step delete all categories and reinsert it after |
||
132 | */ |
||
133 | public function setUpdateCategoriesArticle($categories, $id_article) { |
||
140 | |||
141 | /** |
||
142 | * @param $id_category |
||
143 | * function that can delete an category and all article related to it |
||
144 | */ |
||
145 | public function setDeleteCategory($id_category) { |
||
151 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
152 | } |