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 namespace Anomaly\AddonsModule\Http\Controller\Admin; |
||
21 | class AddonsController extends AdminController |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Return an index of existing entries. |
||
26 | * |
||
27 | * @param AddonTableBuilder $builder |
||
28 | * @param string $type |
||
29 | * @return \Symfony\Component\HttpFoundation\Response |
||
30 | */ |
||
31 | public function index(AddonTableBuilder $builder, $type = 'modules') |
||
37 | |||
38 | /** |
||
39 | * Return the details for an addon. |
||
40 | * |
||
41 | * @param AddonCollection $addons |
||
42 | * @param $addon |
||
43 | * @return mixed|null|string |
||
44 | */ |
||
45 | public function details(AddonCollection $addons, $addon) |
||
54 | |||
55 | /** |
||
56 | * Return the modal form for the seed |
||
57 | * option when installing modules. |
||
58 | * |
||
59 | * @param AddonCollection $addons |
||
60 | * @param $namespace |
||
61 | * @return |
||
62 | */ |
||
63 | public function options(AddonCollection $addons, $namespace) |
||
70 | |||
71 | /** |
||
72 | * Install an addon. |
||
73 | * |
||
74 | * @param Request $request |
||
75 | * @param ModuleManager $modules |
||
76 | * @param AddonCollection $addons |
||
77 | * @param ExtensionManager $extensions |
||
78 | * @param $addon |
||
79 | * @return \Illuminate\Http\RedirectResponse |
||
80 | */ |
||
81 | View Code Duplication | public function install( |
|
101 | |||
102 | /** |
||
103 | * Migrate an addon. |
||
104 | * |
||
105 | * @param Request $request |
||
106 | * @param ModuleManager $modules |
||
107 | * @param AddonCollection $addons |
||
108 | * @param ExtensionManager $extensions |
||
109 | * @param $addon |
||
110 | * @return \Illuminate\Http\RedirectResponse |
||
111 | */ |
||
112 | View Code Duplication | public function migrate( |
|
132 | |||
133 | /** |
||
134 | * Uninstall an addon. |
||
135 | * |
||
136 | * @param AddonCollection $addons |
||
137 | * @param ModuleManager $modules |
||
138 | * @param ExtensionManager $extensions |
||
139 | * @param $addon |
||
140 | * @return \Illuminate\Http\RedirectResponse |
||
141 | */ |
||
142 | public function uninstall(AddonCollection $addons, ModuleManager $modules, ExtensionManager $extensions, $addon) |
||
157 | } |
||
158 |
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.