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 |
||
17 | View Code Duplication | class PackageController extends BaseController { |
|
|
|||
18 | /** |
||
19 | * @param \Illuminate\Http\Request $request |
||
20 | * @param \App\Satis\ConfigManager $configManager |
||
21 | * @param string|null $packageId |
||
22 | * |
||
23 | * @throws \HttpInvalidParamException |
||
24 | */ |
||
25 | protected function addOrUpdatePackage(Request $request, ConfigManager $configManager, $packageId = null) { |
||
58 | |||
59 | /** |
||
60 | * @param \App\Satis\ConfigManager $configManager |
||
61 | * @param string|null $packageId |
||
62 | */ |
||
63 | public function get(ConfigManager $configManager, $packageId = null) { |
||
79 | |||
80 | /** |
||
81 | * @param \Illuminate\Http\Request $request |
||
82 | * @param \App\Satis\ConfigManager $configManager |
||
83 | * |
||
84 | * @throws \HttpInvalidParamException |
||
85 | */ |
||
86 | public function add(Request $request, ConfigManager $configManager) { |
||
89 | |||
90 | /** |
||
91 | * @param \Illuminate\Http\Request $request |
||
92 | * @param \App\Satis\ConfigManager $configManager |
||
93 | * @param string $packageId |
||
94 | * |
||
95 | * @throws \HttpInvalidParamException |
||
96 | */ |
||
97 | public function update(Request $request, ConfigManager $configManager, $packageId) { |
||
100 | |||
101 | /** |
||
102 | * @param \App\Satis\ConfigManager $configManager |
||
103 | * @param string $packageId |
||
104 | */ |
||
105 | public function delete(ConfigManager $configManager, $packageId) { |
||
121 | } |
||
122 |
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.