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 |
||
26 | class Collection extends BaseController { |
||
27 | |||
28 | /** |
||
29 | * The anime collection model |
||
30 | * @var AnimeCollectionModel $anime_collection_model |
||
31 | */ |
||
32 | private $anime_collection_model; |
||
33 | |||
34 | /** |
||
35 | * The anime API model |
||
36 | * @var AnimeModel $anime_model |
||
37 | */ |
||
38 | private $anime_model; |
||
39 | |||
40 | /** |
||
41 | * Data to ve sent to all routes in this controller |
||
42 | * @var array $base_data |
||
43 | */ |
||
44 | protected $base_data; |
||
45 | |||
46 | /** |
||
47 | * Url Generator class |
||
48 | * @var UrlGenerator |
||
49 | */ |
||
50 | protected $urlGenerator; |
||
51 | |||
52 | /** |
||
53 | * Constructor |
||
54 | * |
||
55 | * @param ContainerInterface $container |
||
56 | */ |
||
57 | public function __construct(ContainerInterface $container) |
||
71 | |||
72 | /** |
||
73 | * Search for anime |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function search() |
||
82 | |||
83 | /** |
||
84 | * Show the anime collection page |
||
85 | * |
||
86 | * @param string $view |
||
87 | * @return void |
||
88 | */ |
||
89 | public function index($view) |
||
104 | |||
105 | /** |
||
106 | * Show the anime collection add/edit form |
||
107 | * |
||
108 | * @param integer|null $id |
||
109 | * @return void |
||
110 | */ |
||
111 | public function form($id = NULL) |
||
125 | |||
126 | /** |
||
127 | * Update a collection item |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | View Code Duplication | public function edit() |
|
146 | |||
147 | /** |
||
148 | * Add a collection item |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | View Code Duplication | public function add() |
|
167 | |||
168 | /** |
||
169 | * Remove a collection item |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | View Code Duplication | public function delete() |
|
185 | } |
||
186 | // End of CollectionController.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.