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 |
||
25 | class Anime extends BaseController { |
||
26 | |||
27 | use \Aviat\Ion\StringWrapper; |
||
28 | |||
29 | /** |
||
30 | * The anime list model |
||
31 | * @var object $model |
||
32 | */ |
||
33 | protected $model; |
||
34 | |||
35 | /** |
||
36 | * Data to ve sent to all routes in this controller |
||
37 | * @var array $base_data |
||
38 | */ |
||
39 | protected $base_data; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param ContainerInterface $container |
||
45 | */ |
||
46 | public function __construct(ContainerInterface $container) |
||
59 | |||
60 | /** |
||
61 | * Show a portion, or all of the anime list |
||
62 | * |
||
63 | * @param string $type - The section of the list |
||
64 | * @param string $view - List or cover view |
||
65 | * @return void |
||
66 | */ |
||
67 | public function index($type = "watching", $view = '') |
||
111 | |||
112 | /** |
||
113 | * Form to add an anime |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | View Code Duplication | public function add_form() |
|
|
|||
118 | { |
||
119 | $raw_status_list = AnimeWatchingStatus::getConstList(); |
||
120 | |||
121 | $statuses = []; |
||
122 | |||
123 | foreach ($raw_status_list as $status_item) |
||
124 | { |
||
125 | $statuses[$status_item] = (string)$this->string($status_item) |
||
126 | ->underscored() |
||
127 | ->humanize() |
||
128 | ->titleize(); |
||
129 | } |
||
130 | |||
131 | $this->set_session_redirect(); |
||
132 | $this->outputHTML('anime/add', [ |
||
133 | 'title' => $this->config->get('whose_list') . |
||
134 | "'s Anime List · Add", |
||
135 | 'action_url' => $this->urlGenerator->url('anime/add'), |
||
136 | 'status_list' => $statuses |
||
137 | ]); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Add an anime to the list |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function add() |
||
166 | |||
167 | /** |
||
168 | * Form to edit details about a series |
||
169 | * |
||
170 | * @param int $id |
||
171 | * @param string $status |
||
172 | * @return void |
||
173 | */ |
||
174 | public function edit($id, $status = "all") |
||
200 | |||
201 | /** |
||
202 | * Search for anime |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | public function search() |
||
211 | |||
212 | /** |
||
213 | * Update an anime item via a form submission |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | public function form_update() |
||
244 | |||
245 | /** |
||
246 | * Update an anime item |
||
247 | */ |
||
248 | public function update() |
||
253 | |||
254 | /** |
||
255 | * Remove an anime from the list |
||
256 | */ |
||
257 | public function delete() |
||
262 | |||
263 | /** |
||
264 | * View details of an anime |
||
265 | * |
||
266 | * @param string anime_id |
||
267 | * @return void |
||
268 | */ |
||
269 | public function details($anime_id) |
||
278 | } |
||
279 | // End of AnimeController.php |
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.