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 |
||
| 9 | class CategoriesController extends Controller |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Controller specific permission ability map. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $customPermissionMap = [ |
||
| 17 | 'publish' => 'update', |
||
| 18 | ]; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * CategoriesController constructor. |
||
| 22 | */ |
||
| 23 | public function __construct() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Display a listing of the resource. |
||
| 30 | * |
||
| 31 | * @param \Yajra\CMS\DataTables\CategoriesDataTable $dataTable |
||
| 32 | * @return \Illuminate\Http\Response |
||
| 33 | */ |
||
| 34 | public function index(CategoriesDataTable $dataTable) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Show the form for creating a new resource. |
||
| 41 | * |
||
| 42 | * @param \Yajra\CMS\Entities\Category $category |
||
| 43 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 44 | */ |
||
| 45 | public function create(Category $category) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Store a newly created resource in storage. |
||
| 54 | * |
||
| 55 | * @param \Yajra\CMS\Http\Requests\CategoriesFormRequest $request |
||
| 56 | * @param \Yajra\CMS\Entities\Category $category |
||
| 57 | * @return \Illuminate\Http\Response |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function store(CategoriesFormRequest $request, Category $category) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Show the form for editing the specified resource. |
||
| 72 | * |
||
| 73 | * @param \Yajra\CMS\Entities\Category $category |
||
| 74 | * @return \Illuminate\Http\Response |
||
| 75 | */ |
||
| 76 | public function edit(Category $category) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Update the specified resource in storage. |
||
| 87 | * |
||
| 88 | * @param CategoriesFormRequest $request |
||
| 89 | * @param \Yajra\CMS\Entities\Category $category |
||
| 90 | * @return \Illuminate\Http\Response |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function update(CategoriesFormRequest $request, Category $category) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Remove the specified resource from storage. |
||
| 105 | * |
||
| 106 | * @param \Yajra\CMS\Entities\Category $category |
||
| 107 | * @return \Illuminate\Http\JsonResponse |
||
| 108 | */ |
||
| 109 | public function destroy(Category $category) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Button Response from DataTable request to publish status. |
||
| 126 | * |
||
| 127 | * @param \Yajra\CMS\Entities\Category $category |
||
| 128 | * @return \Illuminate\Http\JsonResponse |
||
| 129 | */ |
||
| 130 | public function publish(Category $category) |
||
| 148 | } |
||
| 149 |
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.