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 |
||
| 34 | class AlertsController extends Controller |
||
| 35 | { |
||
| 36 | |||
| 37 | use Helpers; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Display a listing of all alerts |
||
| 41 | * |
||
| 42 | * @param Request $request |
||
| 43 | * @return \Illuminate\Http\Response |
||
| 44 | */ |
||
| 45 | public function index(Request $request) |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * Show the form for creating a new resource. |
||
| 54 | * |
||
| 55 | * @return \Illuminate\Http\Response|null |
||
| 56 | */ |
||
| 57 | public function create() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Store a newly created resource in storage. |
||
| 64 | * |
||
| 65 | * @param \Illuminate\Http\Request $request |
||
| 66 | * @return \Illuminate\Http\Response|null |
||
| 67 | */ |
||
| 68 | public function store(Request $request) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Display the specified resource. |
||
| 75 | * |
||
| 76 | * @param int $id |
||
| 77 | * @return \Illuminate\Http\Response|null |
||
| 78 | */ |
||
| 79 | public function show(Request $request, $id) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Show the form for editing the specified resource. |
||
| 86 | * |
||
| 87 | * @param int $id |
||
| 88 | * @return \Illuminate\Http\Response|null |
||
| 89 | */ |
||
| 90 | public function edit($id) |
||
| 94 | |||
| 95 | public function update(Request $request, $id) |
||
| 96 | { |
||
| 97 | $alert = Alert::find($id); |
||
| 98 | $alert->state = $request->input('state'); |
||
| 99 | View Code Duplication | if ($alert->save()) |
|
| 100 | { |
||
| 101 | return $this->response->array(array('statusText' => 'OK')); |
||
| 102 | } |
||
| 103 | else { |
||
| 104 | return $this->response->errorInternal(); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Remove the specified resource from storage. |
||
| 110 | * |
||
| 111 | * @param int $id |
||
| 112 | * @return \Illuminate\Http\Response|null |
||
| 113 | */ |
||
| 114 | public function destroy($id) |
||
| 118 | |||
| 119 | } |
||
| 120 |
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: