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 Manga extends Controller { |
||
26 | |||
27 | use \Aviat\Ion\StringWrapper; |
||
28 | |||
29 | /** |
||
30 | * The manga 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 | View Code Duplication | public function __construct(ContainerInterface $container) |
|
|
|||
47 | { |
||
48 | parent::__construct($container); |
||
49 | |||
50 | $this->model = $container->get('manga-model'); |
||
51 | $this->base_data = array_merge($this->base_data, [ |
||
52 | 'menu_name' => 'manga_list', |
||
53 | 'config' => $this->config, |
||
54 | 'url_type' => 'manga', |
||
55 | 'other_type' => 'anime' |
||
56 | ]); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get a section of the manga list |
||
61 | * |
||
62 | * @param string $status |
||
63 | * @param string $view |
||
64 | * @return void |
||
65 | */ |
||
66 | public function index($status = "all", $view = "") |
||
93 | |||
94 | /** |
||
95 | * Form to add an manga |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | View Code Duplication | public function add_form() |
|
121 | |||
122 | /** |
||
123 | * Add an manga to the list |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | public function add() |
||
148 | |||
149 | /** |
||
150 | * Show the manga edit form |
||
151 | * |
||
152 | * @param string $id |
||
153 | * @param string $status |
||
154 | * @return void |
||
155 | */ |
||
156 | public function edit($id, $status = "All") |
||
170 | |||
171 | /** |
||
172 | * Search for a manga to add to the list |
||
173 | * |
||
174 | * @param string $query |
||
175 | * @return void |
||
176 | */ |
||
177 | public function search() |
||
182 | |||
183 | /** |
||
184 | * Update an anime item via a form submission |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | public function form_update() |
||
216 | |||
217 | /** |
||
218 | * Update an anime item |
||
219 | * |
||
220 | * @return boolean|null |
||
221 | */ |
||
222 | public function update() |
||
227 | } |
||
228 | // End of MangaController.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.