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 |
||
11 | class WidgetsController extends Controller |
||
12 | { |
||
13 | /** |
||
14 | * Controller specific permission ability map. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $customPermissionMap = [ |
||
19 | 'publish' => 'update', |
||
20 | ]; |
||
21 | |||
22 | /** |
||
23 | * @var \Yajra\CMS\Repositories\Extension\Repository |
||
24 | */ |
||
25 | protected $repository; |
||
26 | |||
27 | /** |
||
28 | * WidgetsController constructor. |
||
29 | * |
||
30 | * @param \Yajra\CMS\Repositories\Extension\Repository $repository |
||
31 | */ |
||
32 | public function __construct(Repository $repository) |
||
33 | { |
||
34 | $this->authorizePermissionResource('widget'); |
||
35 | $this->repository = $repository; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Display list of widgets. |
||
40 | * |
||
41 | * @param \Yajra\CMS\DataTables\WidgetsDataTable $dataTable |
||
42 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
43 | */ |
||
44 | public function index(WidgetsDataTable $dataTable) |
||
48 | |||
49 | /** |
||
50 | * Show widget form. |
||
51 | * |
||
52 | * @param \Yajra\CMS\Entities\Widget $widget |
||
53 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
54 | */ |
||
55 | public function create(Widget $widget) |
||
56 | { |
||
57 | $widget->extension_id = old('extension_id', Extension::WIDGET_WYSIWYG); |
||
58 | $widget->template = old('template', 'widgets.wysiwyg.raw'); |
||
59 | $widget->published = old('published', true); |
||
60 | |||
61 | return view('administrator.widgets.create', compact('widget')); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Store a newly created widget. |
||
66 | * |
||
67 | * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request |
||
68 | * @return \Illuminate\Http\RedirectResponse |
||
69 | */ |
||
70 | View Code Duplication | public function store(WidgetFormRequest $request) |
|
86 | |||
87 | /** |
||
88 | * Show and edit selected widget. |
||
89 | * |
||
90 | * @param \Yajra\CMS\Entities\Widget $widget |
||
91 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
92 | */ |
||
93 | public function edit(Widget $widget) |
||
100 | |||
101 | /** |
||
102 | * Update selected widget. |
||
103 | * |
||
104 | * @param \Yajra\CMS\Entities\Widget $widget |
||
105 | * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request |
||
106 | * @return \Illuminate\Http\RedirectResponse |
||
107 | */ |
||
108 | View Code Duplication | public function update(Widget $widget, WidgetFormRequest $request) |
|
123 | |||
124 | /** |
||
125 | * Remove selected widget. |
||
126 | * |
||
127 | * @param \Yajra\CMS\Entities\Widget $widget |
||
128 | * @return \Illuminate\Http\JsonResponse |
||
129 | * @throws \Exception |
||
130 | */ |
||
131 | public function destroy(Widget $widget) |
||
137 | |||
138 | /** |
||
139 | * Publish/Unpublish a widget. |
||
140 | * |
||
141 | * @param \Yajra\CMS\Entities\Widget $widget |
||
142 | * @return \Illuminate\Http\JsonResponse |
||
143 | */ |
||
144 | public function publish(Widget $widget) |
||
154 | |||
155 | /** |
||
156 | * Get all widget types. |
||
157 | * |
||
158 | * @param string $type |
||
159 | * @return string |
||
160 | */ |
||
161 | public function templates($type) |
||
175 | |||
176 | /** |
||
177 | * Get widget custom parameter form if any. |
||
178 | * |
||
179 | * @param int $id |
||
180 | * @param int $widget |
||
181 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
182 | */ |
||
183 | public function parameters($id, $widget) |
||
195 | } |
||
196 |
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.