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 |
||
| 13 | class OrganizerController extends Controller |
||
| 14 | { |
||
| 15 | /* Restrict the access to this resource just to logged in users except show view */ |
||
| 16 | public function __construct() |
||
| 20 | |||
| 21 | /***************************************************************************/ |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Display a listing of the resource. |
||
| 25 | * |
||
| 26 | * @return \Illuminate\Http\Response |
||
| 27 | */ |
||
| 28 | public function index(Request $request) |
||
| 56 | |||
| 57 | /***************************************************************************/ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Show the form for creating a new resource. |
||
| 61 | * |
||
| 62 | * @return \Illuminate\Http\Response |
||
| 63 | */ |
||
| 64 | public function create() |
||
| 73 | |||
| 74 | /***************************************************************************/ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Store a newly created resource in storage. |
||
| 78 | * |
||
| 79 | * @param \Illuminate\Http\Request $request |
||
| 80 | * @return \Illuminate\Http\Response |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function store(Request $request) |
|
| 96 | |||
| 97 | /***************************************************************************/ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Display the specified resource. |
||
| 101 | * |
||
| 102 | * @param \App\Organizer $organizer |
||
| 103 | * @return \Illuminate\Http\Response |
||
| 104 | */ |
||
| 105 | public function show(Organizer $organizer) |
||
| 109 | |||
| 110 | /***************************************************************************/ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Show the form for editing the specified resource. |
||
| 114 | * |
||
| 115 | * @param \App\Organizer $organizer |
||
| 116 | * @return \Illuminate\Http\Response |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function edit(Organizer $organizer) |
|
| 131 | |||
| 132 | /***************************************************************************/ |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Update the specified resource in storage. |
||
| 136 | * |
||
| 137 | * @param \Illuminate\Http\Request $request |
||
| 138 | * @param \App\Organizer $organizer |
||
| 139 | * @return \Illuminate\Http\Response |
||
| 140 | */ |
||
| 141 | public function update(Request $request, Organizer $organizer) |
||
| 154 | |||
| 155 | /***************************************************************************/ |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Remove the specified resource from storage. |
||
| 159 | * |
||
| 160 | * @param \App\Organizer $organizer |
||
| 161 | * @return \Illuminate\Http\Response |
||
| 162 | */ |
||
| 163 | public function destroy(Organizer $organizer) |
||
| 170 | |||
| 171 | /***************************************************************************/ |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Save the record on DB. |
||
| 175 | * @param \Illuminate\Http\Request $request |
||
| 176 | * @param \App\Organizer $organizer |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function saveOnDb($request, $organizer) |
||
| 194 | |||
| 195 | /***************************************************************************/ |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Open a modal in the event view when create organizer is clicked. |
||
| 199 | * |
||
| 200 | * @return view |
||
| 201 | */ |
||
| 202 | public function modal() |
||
| 206 | |||
| 207 | /***************************************************************************/ |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Store a newly created organizer from the create event view modal in storage. |
||
| 211 | * |
||
| 212 | * @param \Illuminate\Http\Request $request |
||
| 213 | * @return \Illuminate\Http\Response |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function storeFromModal(Request $request) |
|
| 228 | |||
| 229 | /***************************************************************************/ |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return the organizer by SLUG. (eg. http://websitename.com/organizer/xxxxx). |
||
| 233 | * |
||
| 234 | * @param string $slug |
||
| 235 | * @return \Illuminate\Http\Response |
||
| 236 | */ |
||
| 237 | public function organizerBySlug($slug) |
||
| 245 | |||
| 246 | /***************************************************************************/ |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Return the validator with all the defined constraint. |
||
| 250 | * |
||
| 251 | * @param \Illuminate\Http\Request $request |
||
| 252 | * @return \Illuminate\Validation\Validator |
||
| 253 | */ |
||
| 254 | View Code Duplication | public function organizersValidator($request) |
|
| 269 | } |
||
| 270 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: