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 |
||
16 | class UtilitiesController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * @var \Illuminate\Contracts\Console\Kernel |
||
20 | */ |
||
21 | protected $command; |
||
22 | |||
23 | /** |
||
24 | * @var \Collective\Html\HtmlBuilder |
||
25 | */ |
||
26 | protected $html; |
||
27 | |||
28 | /** |
||
29 | * @var \Illuminate\Contracts\Logging\Log |
||
30 | */ |
||
31 | protected $log; |
||
32 | |||
33 | /** |
||
34 | * Controller specific permission ability map. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $customPermissionMap = [ |
||
39 | 'backup' => 'view', |
||
40 | 'config' => 'view', |
||
41 | 'cache' => 'view', |
||
42 | 'logs' => 'view', |
||
43 | 'views' => 'view', |
||
44 | 'index' => 'view', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * UtilitiesController constructor. |
||
49 | * |
||
50 | * @param \Illuminate\Contracts\Console\Kernel $command |
||
51 | * @param \Illuminate\Contracts\Logging\Log $log |
||
52 | * @param \Collective\Html\HtmlBuilder $html |
||
53 | */ |
||
54 | public function __construct(Kernel $command, Log $log, HtmlBuilder $html) |
||
62 | |||
63 | /** |
||
64 | * Display list of utilities. |
||
65 | * |
||
66 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
67 | */ |
||
68 | public function index() |
||
72 | |||
73 | /** |
||
74 | * Execute back up manually. |
||
75 | * |
||
76 | * @param string $task |
||
77 | * @return \Illuminate\Http\JsonResponse |
||
78 | */ |
||
79 | View Code Duplication | public function backup($task = 'run') |
|
94 | |||
95 | /** |
||
96 | * Get name of the current user. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | protected function getCurrentUserName() |
||
104 | |||
105 | /** |
||
106 | * Clear cache manually. |
||
107 | * |
||
108 | * @return \Illuminate\Http\JsonResponse |
||
109 | */ |
||
110 | View Code Duplication | public function cache() |
|
117 | |||
118 | /** |
||
119 | * Run config artisan command manually. |
||
120 | * |
||
121 | * @param string $task |
||
122 | * @return \Illuminate\Http\JsonResponse |
||
123 | */ |
||
124 | View Code Duplication | public function config($task) |
|
139 | |||
140 | /** |
||
141 | * Clear views manually. |
||
142 | * |
||
143 | * @return \Illuminate\Http\JsonResponse |
||
144 | */ |
||
145 | View Code Duplication | public function views() |
|
152 | |||
153 | /** |
||
154 | * Log viewer. |
||
155 | * |
||
156 | * @param \Illuminate\Http\Request $request |
||
157 | * @param \Yajra\Datatables\Datatables $datatables |
||
158 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View|\Symfony\Component\HttpFoundation\BinaryFileResponse |
||
159 | * @throws \Exception |
||
160 | */ |
||
161 | public function logs(Request $request, Datatables $datatables) |
||
212 | |||
213 | /** |
||
214 | * Rebuild menu entity nested set tree. |
||
215 | */ |
||
216 | public function rebuildMenu() |
||
222 | |||
223 | /** |
||
224 | * Rebuild category entity nested set tree. |
||
225 | */ |
||
226 | public function rebuildCategory() |
||
232 | } |
||
233 |
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.