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; |
||
22 | class AddonsController extends AdminController |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Return an index of existing entries. |
||
27 | * |
||
28 | * @param AddonTableBuilder $builder |
||
29 | * @param string $type |
||
30 | * @return \Symfony\Component\HttpFoundation\Response |
||
31 | */ |
||
32 | public function index(AddonTableBuilder $builder, $type = 'modules') |
||
38 | |||
39 | /** |
||
40 | * Return the details for an addon. |
||
41 | * |
||
42 | * @param AddonCollection $addons |
||
43 | * @param $addon |
||
44 | * @return mixed|null|string |
||
45 | */ |
||
46 | View Code Duplication | public function details(AddonCollection $addons, $addon) |
|
55 | |||
56 | /** |
||
57 | * Ask the user for any options when installing the addon. |
||
58 | * |
||
59 | * @param AddonCollection $addons |
||
60 | * @param $addon |
||
61 | * @return mixed|null|string |
||
62 | */ |
||
63 | View Code Duplication | public function installOptions(AddonCollection $addons, $namespace) |
|
72 | |||
73 | /** |
||
74 | * Install an addon. |
||
75 | * |
||
76 | * @param AddonCollection $addons |
||
77 | * @param ModuleManager $modules |
||
78 | * @param ExtensionManager $extensions |
||
79 | * @param $addon |
||
80 | * @return \Illuminate\Http\RedirectResponse |
||
81 | */ |
||
82 | public function install(Request $request, AddonCollection $addons, ModuleManager $modules, ExtensionManager $extensions, $addon) |
||
99 | |||
100 | /** |
||
101 | * Uninstall an addon. |
||
102 | * |
||
103 | * @param AddonCollection $addons |
||
104 | * @param ModuleManager $modules |
||
105 | * @param ExtensionManager $extensions |
||
106 | * @param $addon |
||
107 | * @return \Illuminate\Http\RedirectResponse |
||
108 | */ |
||
109 | public function uninstall(AddonCollection $addons, ModuleManager $modules, ExtensionManager $extensions, $addon) |
||
124 | } |
||
125 |
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.