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 |
||
10 | class NavigationController extends Controller |
||
11 | { |
||
12 | /** |
||
13 | * Controller specific permission ability map. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $customPermissionMap = [ |
||
18 | 'publish' => 'update', |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * NavigationController constructor. |
||
23 | */ |
||
24 | public function __construct() |
||
28 | |||
29 | /** |
||
30 | * Display list of navigation. |
||
31 | * |
||
32 | * @param \Yajra\CMS\DataTables\NavigationDataTable $dataTable |
||
33 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
34 | */ |
||
35 | public function index(NavigationDataTable $dataTable) |
||
39 | |||
40 | /** |
||
41 | * Show navigation form. |
||
42 | * |
||
43 | * @param \Yajra\CMS\Entities\Navigation $navigation |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function create(Navigation $navigation) |
||
50 | |||
51 | /** |
||
52 | * Store a newly created navigation. |
||
53 | * |
||
54 | * @param \Illuminate\Http\Request $request |
||
55 | * @return mixed |
||
56 | */ |
||
57 | View Code Duplication | public function store(Request $request) |
|
72 | |||
73 | /** |
||
74 | * Show and edit selected navigation. |
||
75 | * |
||
76 | * @param \Yajra\CMS\Entities\Navigation $navigation |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function edit(Navigation $navigation) |
||
83 | |||
84 | /** |
||
85 | * Update selected navigation. |
||
86 | * |
||
87 | * @param \Yajra\CMS\Entities\Navigation $navigation |
||
88 | * @param \Illuminate\Http\Request $request |
||
89 | * @return mixed |
||
90 | */ |
||
91 | View Code Duplication | public function update(Navigation $navigation, Request $request) |
|
105 | |||
106 | /** |
||
107 | * Remove selected navigation. |
||
108 | * |
||
109 | * @param \Yajra\CMS\Entities\Navigation $navigation |
||
110 | * @return string |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | public function destroy(Navigation $navigation) |
||
119 | |||
120 | /** |
||
121 | * Publish/Unpublish a navigation. |
||
122 | * |
||
123 | * @param \Yajra\CMS\Entities\Navigation $navigation |
||
124 | * @return \Illuminate\Http\JsonResponse |
||
125 | */ |
||
126 | public function publish(Navigation $navigation) |
||
135 | } |
||
136 |
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.