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 |
||
18 | class Comment2Controller implements |
||
19 | ConfigureInterface, |
||
20 | InjectionAwareInterface |
||
21 | { |
||
22 | use ConfigureTrait, |
||
23 | InjectionAwareTrait; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * @var $data description |
||
29 | */ |
||
30 | //private $data; |
||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * Show all items. |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | 1 | public function getIndex() |
|
40 | { |
||
41 | 1 | $title = "View | Comments"; |
|
42 | 1 | $view = $this->di->get("view"); |
|
43 | 1 | $pageRender = $this->di->get("pageRender"); |
|
44 | 1 | $comment2 = new Comment(); |
|
45 | 1 | $comment2->setDb($this->di->get("db")); |
|
46 | |||
47 | $data = [ |
||
48 | 1 | "items" => $comment2->findAll(), |
|
49 | 1 | ]; |
|
50 | |||
51 | 1 | $view->add("incl/header", ["title" => ["Book", "css/style.css"]]); |
|
52 | 1 | $view->add("incl/side-bar1"); |
|
53 | 1 | $view->add("comment/viewAll", $data); |
|
54 | 1 | $view->add("incl/side-bar2"); |
|
55 | 1 | $view->add("incl/footer"); |
|
56 | |||
57 | 1 | $pageRender->renderPage(["title" => $title]); |
|
58 | 1 | } |
|
59 | |||
60 | 1 | View Code Duplication | public function comment2Create() |
89 | |||
90 | 1 | View Code Duplication | public function comment2Update($id) |
91 | { |
||
92 | 1 | $session = $this->di->get("session"); |
|
93 | 1 | $userId = $session->get("user_id"); |
|
94 | |||
95 | 1 | if (!$userId) { |
|
96 | 1 | $this->di->get("response")->redirect(""); |
|
97 | 1 | } |
|
98 | |||
99 | 1 | $title = "Create | Comment"; |
|
100 | 1 | $view = $this->di->get("view"); |
|
101 | 1 | $pageRender = $this->di->get("pageRender"); |
|
102 | 1 | $comment2 = new Com2Update($this->di, $id); |
|
103 | |||
104 | 1 | $comment2->check(); |
|
105 | |||
106 | 1 | $data = [ |
|
107 | 1 | "form" => $comment2->getHTML(), |
|
108 | 1 | ]; |
|
109 | |||
110 | |||
111 | 1 | $view->add("incl/header", ["title" => ["Book", "../../css/style.css"]]); |
|
112 | 1 | $view->add("incl/side-bar1"); |
|
113 | 1 | $view->add("comment/create", $data); |
|
114 | 1 | $view->add("incl/side-bar2"); |
|
115 | 1 | $view->add("incl/footer"); |
|
116 | |||
117 | 1 | $pageRender->renderPage(["title" => $title]); |
|
118 | 1 | } |
|
119 | |||
120 | 1 | View Code Duplication | public function comment2Delete($id) |
121 | { |
||
122 | 1 | $session = $this->di->get("session"); |
|
123 | 1 | $userId = $session->get("user_id"); |
|
124 | |||
125 | 1 | if (!$userId) { |
|
126 | 1 | $this->di->get("response")->redirect(""); |
|
127 | 1 | } |
|
128 | |||
129 | 1 | $title = "Delete | Comment"; |
|
130 | 1 | $view = $this->di->get("view"); |
|
131 | 1 | $pageRender = $this->di->get("pageRender"); |
|
132 | 1 | $comment2 = new Com2Delete($this->di, $id); |
|
133 | |||
134 | 1 | $comment2->check(); |
|
135 | |||
136 | $data = [ |
||
137 | 1 | "form" => $comment2->getHTML(), |
|
138 | 1 | ]; |
|
139 | |||
140 | |||
141 | 1 | $view->add("incl/header", ["title" => ["Book", "../../css/style.css"]]); |
|
142 | 1 | $view->add("incl/side-bar1"); |
|
143 | 1 | $view->add("comment/create", $data); |
|
144 | 1 | $view->add("incl/side-bar2"); |
|
145 | 1 | $view->add("incl/footer"); |
|
146 | |||
147 | 1 | $pageRender->renderPage(["title" => $title]); |
|
148 | 1 | } |
|
149 | |||
150 | 1 | public function setUser() |
|
171 | } |
||
172 |
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.