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 |
||
15 | class UtilitiesController extends Controller |
||
16 | { |
||
17 | /** |
||
18 | * @var \Illuminate\Contracts\Console\Kernel |
||
19 | */ |
||
20 | protected $command; |
||
21 | |||
22 | /** |
||
23 | * @var \Collective\Html\HtmlBuilder |
||
24 | */ |
||
25 | protected $html; |
||
26 | |||
27 | /** |
||
28 | * @var \Illuminate\Contracts\Logging\Log |
||
29 | */ |
||
30 | protected $log; |
||
31 | |||
32 | /** |
||
33 | * Controller specific permission ability map. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $customPermissionMap = [ |
||
38 | 'backup' => 'view', |
||
39 | 'config' => 'view', |
||
40 | 'cache' => 'view', |
||
41 | 'logs' => 'view', |
||
42 | 'views' => 'view', |
||
43 | 'index' => 'view', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * UtilitiesController constructor. |
||
48 | * |
||
49 | * @param \Illuminate\Contracts\Logging\Log $log |
||
50 | * @param \Collective\Html\HtmlBuilder $html |
||
51 | */ |
||
52 | public function __construct(Log $log, HtmlBuilder $html) |
||
53 | { |
||
54 | $this->html = $html; |
||
55 | $this->log = $log; |
||
56 | |||
57 | $this->authorizePermissionResource('utilities'); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Display list of utilities. |
||
62 | * |
||
63 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
64 | */ |
||
65 | public function index() |
||
66 | { |
||
67 | return view('administrator.utilities.index'); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Execute back up manually. |
||
72 | * |
||
73 | * @param string $task |
||
74 | * @return \Illuminate\Http\JsonResponse |
||
75 | */ |
||
76 | public function backup($task = 'run') |
||
77 | { |
||
78 | if (! in_array($task, ['backup', 'clean'])) { |
||
79 | $message = trans('cms::utilities.backup.not_allowed', |
||
80 | ['task' => $task]) . trans('cms::utilities.field.executed_by', |
||
81 | ['name' => $this->getCurrentUserName()]); |
||
82 | $this->log->info($message); |
||
83 | |||
84 | return $this->notifyError($message); |
||
85 | } |
||
86 | |||
87 | Artisan::call('backup:' . $task); |
||
88 | $message = $task == 'clean' ? trans('cms::utilities.backup.cleanup_complete') : trans('cms::utilities.backup.complete'); |
||
89 | $this->log->info($message . trans('cms::utilities.field.executed_by', ['name' => $this->getCurrentUserName()])); |
||
90 | |||
91 | return $this->notifySuccess($message); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get name of the current user. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | protected function getCurrentUserName() |
||
103 | |||
104 | /** |
||
105 | * Clear cache manually. |
||
106 | * |
||
107 | * @return \Illuminate\Http\JsonResponse |
||
108 | */ |
||
109 | 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 | public function config($task) |
||
144 | |||
145 | /** |
||
146 | * Clear views manually. |
||
147 | * |
||
148 | * @return \Illuminate\Http\JsonResponse |
||
149 | */ |
||
150 | View Code Duplication | public function views() |
|
160 | |||
161 | /** |
||
162 | * Log viewer. |
||
163 | * |
||
164 | * @param \Illuminate\Http\Request $request |
||
165 | * @param \Yajra\Datatables\Datatables $datatables |
||
166 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View|\Symfony\Component\HttpFoundation\BinaryFileResponse |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | public function logs(Request $request, Datatables $datatables) |
||
221 | |||
222 | /** |
||
223 | * Rebuild menu entity nested set tree. |
||
224 | */ |
||
225 | public function rebuildMenu() |
||
231 | |||
232 | /** |
||
233 | * Rebuild category entity nested set tree. |
||
234 | */ |
||
235 | public function rebuildCategory() |
||
241 | } |
||
242 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: