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 SiteController extends Controller |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Create a new controller instance. |
||
| 13 | */ |
||
| 14 | public function __construct() |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Display a listing of the resource. |
||
| 21 | * |
||
| 22 | * @param SiteDataTable $dataTable |
||
| 23 | * @return Response |
||
| 24 | */ |
||
| 25 | public function index(SiteDataTable $dataTable) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Show the form for creating a new resource. |
||
| 32 | * |
||
| 33 | * @return \Illuminate\Http\Response |
||
| 34 | */ |
||
| 35 | public function create() |
||
| 36 | { |
||
| 37 | return view('site.create'); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Store a newly created resource in storage. |
||
| 42 | * |
||
| 43 | * @param \Illuminate\Http\Request $request |
||
| 44 | * @return \Illuminate\Http\Response |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function store(Request $request) |
|
| 47 | { |
||
| 48 | $request->validate([ |
||
| 49 | 'name' => 'required|min:2|max:75|regex:#(^[a-zA-Z0-9])([\w ]*)(\w$)#|unique:sites,name', |
||
| 50 | ]); |
||
| 51 | |||
| 52 | $site = Site::create(['name' => $request->name]); |
||
| 53 | |||
| 54 | return redirect()->route('site.show', $site->id) |
||
| 55 | ->with('success', 'Site created successfully'); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Display the specified resource. |
||
| 60 | * |
||
| 61 | * @param int $id |
||
| 62 | * @return \Illuminate\Http\Response |
||
| 63 | */ |
||
| 64 | public function show($id) |
||
| 65 | { |
||
| 66 | $site = Site::findOrFail($id); |
||
| 67 | $locations = $site->locations()->orderBy('name', 'ASC')->paginate(15); |
||
| 68 | |||
| 69 | return view('site.show', [ 'site' => $site, 'locations' => $locations ]); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Show the form for editing the specified resource. |
||
| 74 | * |
||
| 75 | * @param int $id |
||
| 76 | * @return \Illuminate\Http\Response |
||
| 77 | */ |
||
| 78 | public function edit($id) |
||
| 79 | { |
||
| 80 | $site = Site::findOrFail($id); |
||
| 81 | |||
| 82 | return view('site.edit', [ 'site' => $site ]); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Update the specified resource in storage. |
||
| 87 | * |
||
| 88 | * @param \Illuminate\Http\Request $request |
||
| 89 | * @param Site $site |
||
| 90 | * @return \Illuminate\Http\Response |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function update(Request $request, Site $site) |
|
| 93 | { |
||
| 94 | $request->validate([ |
||
| 95 | 'name' => 'required|min:2|max:75|regex:#(^[a-zA-Z0-9])([\w ]*)(\w$)#|unique:sites,name,'.$site->id, |
||
| 96 | ]); |
||
| 97 | |||
| 98 | $site->update(['name' => $request->name]); |
||
| 99 | |||
| 100 | return redirect()->route('site.show', $site->id) |
||
| 101 | ->with('success', 'Site updated successfully'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Remove the specified resource from storage. |
||
| 106 | * |
||
| 107 | * @param int $id |
||
| 108 | * @return \Illuminate\Http\Response |
||
| 109 | */ |
||
| 110 | public function destroy($id) |
||
| 111 | { |
||
| 112 | Site::findOrFail($id)->delete(); |
||
| 113 | return redirect()->route('site.index') |
||
| 114 | ->with('success','Site deleted successfully'); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the locations belonging to the given site |
||
| 119 | * |
||
| 120 | * @param Site $site |
||
| 121 | * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
||
| 122 | */ |
||
| 123 | public function locations(Site $site) |
||
| 129 | } |
||
| 130 |