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 |
||
7 | class WidgetJsonifier |
||
8 | { |
||
9 | /** |
||
10 | * @param $widget object|string |
||
11 | * @param array $args |
||
12 | * @return string |
||
13 | */ |
||
14 | View Code Duplication | public function jsonResponse($widget, ...$args) |
|
30 | |||
31 | /** |
||
32 | * @param $widget object |
||
33 | * @return \Illuminate\Foundation\Application|mixed |
||
34 | */ |
||
35 | private function makeWidgetObj($widget) |
||
41 | |||
42 | /** |
||
43 | * It tries to get the html from cache if possible, otherwise generates it. |
||
44 | * |
||
45 | * @param $widget object |
||
46 | * @param array ...$args |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | private function generateJson($widget, ...$args) |
||
67 | } |
||
68 |
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.