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 |
||
| 20 | class ClubController extends Controller |
||
| 21 | { |
||
| 22 | // Only Super Admin and Club President can manage Clubs |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Display a listing of the resource. |
||
| 26 | * |
||
| 27 | * @return Collection|View |
||
| 28 | */ |
||
| 29 | public function index() |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Show the form for creating a new resource. |
||
| 48 | * @return View |
||
| 49 | * @throws AuthorizationException |
||
| 50 | */ |
||
| 51 | public function create() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Store a newly created resource in storage. |
||
| 72 | * |
||
| 73 | * @param ClubRequest $request |
||
| 74 | * @return JsonResponse|RedirectResponse |
||
| 75 | */ |
||
| 76 | public function store(ClubRequest $request) |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Display the specified resource. |
||
| 106 | * |
||
| 107 | * @param int $id |
||
| 108 | * @return View |
||
| 109 | */ |
||
| 110 | public function show($id) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Show the form for editing the specified resource. |
||
| 118 | * |
||
| 119 | * @param int $id |
||
| 120 | * @return View |
||
| 121 | * @throws AuthorizationException |
||
| 122 | */ |
||
| 123 | public function edit($id) |
||
| 124 | { |
||
| 125 | |||
| 126 | $defaultLng = Auth::user()->latitude ?? geoip()->lat; |
||
| 127 | $defaultLat = Auth::user()->longitude ?? geoip()->lon; |
||
| 128 | |||
| 129 | $club = Club::findOrFail($id); |
||
| 130 | $this->authorize('edit', [$club, Auth::user()]); |
||
| 131 | |||
| 132 | $federations = Federation::fillSelect(); |
||
| 133 | $associations = Association::forUser(auth()->user())->pluck('name', 'id')->prepend('-', 0); |
||
| 134 | $users = Auth::user()->fillSelect(); |
||
| 135 | |||
| 136 | return view('clubs.form', compact('club', 'users', 'associations', 'federations', 'defaultLng', 'defaultLat')); // |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Update the specified resource in storage. |
||
| 141 | * |
||
| 142 | * @param ClubRequest|Request $request |
||
| 143 | * @param int $id |
||
| 144 | * @return \Illuminate\Http\Response |
||
| 145 | * @throws AuthorizationException |
||
| 146 | */ |
||
| 147 | public function update(ClubRequest $request, $id) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Remove the specified resource from storage. |
||
| 171 | * |
||
| 172 | * @param $clubId |
||
| 173 | * @return JsonResponse |
||
| 174 | * @internal param Club $club |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function destroy($clubId) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param $id |
||
| 188 | * @return \Illuminate\Http\JsonResponse |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function restore($id) |
|
| 191 | |||
| 192 | { |
||
| 193 | $club = Club::withTrashed()->find($id); |
||
| 194 | if ($club->restore()) { |
||
| 195 | return Response::json(['msg' => trans('msg.club_restored_successful', ['name' => $club->name]), 'status' => 'success']); |
||
| 196 | } else { |
||
| 197 | return Response::json(['msg' => trans('msg.club_restored_error', ['name' => $club->name]), 'status' => 'error']); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: