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 |
||
| 11 | class DashboardController extends Controller |
||
| 12 | { |
||
| 13 | |||
| 14 | use Helpers; |
||
| 15 | |||
| 16 | public function __construct() |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Display a listing of all authorized devices |
||
| 22 | * |
||
| 23 | * @return \Illuminate\Http\Response |
||
| 24 | */ |
||
| 25 | public function index(Request $request) |
||
| 26 | { |
||
| 27 | $dashboards = Dashboard::allAvailable($request->user())->get(); |
||
|
|
|||
| 28 | return $dashboards; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Show the form for creating a new resource. |
||
| 33 | * |
||
| 34 | * @return \Illuminate\Http\Response|null |
||
| 35 | */ |
||
| 36 | public function create(Request $request) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Store a newly created resource in storage. |
||
| 43 | * |
||
| 44 | * @param \Illuminate\Http\Request $request |
||
| 45 | * @return \Illuminate\Http\Response |
||
| 46 | */ |
||
| 47 | public function store(Request $request) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Display the specified resource. |
||
| 81 | * |
||
| 82 | * @param int $id |
||
| 83 | * @return \Illuminate\Http\Response |
||
| 84 | */ |
||
| 85 | public function show(Request $request, $id) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Show the form for editing the specified resource. |
||
| 95 | * |
||
| 96 | * @param int $id |
||
| 97 | * @return \Illuminate\Http\Response |
||
| 98 | */ |
||
| 99 | public function edit($id) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Update the specified resource in storage. |
||
| 106 | * |
||
| 107 | * @param \Illuminate\Http\Request $request |
||
| 108 | * @param int $id |
||
| 109 | * @return \Illuminate\Http\Response |
||
| 110 | */ |
||
| 111 | public function update(Request $request, $id) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Remove the specified resource from storage. |
||
| 134 | * |
||
| 135 | * @param int $id |
||
| 136 | * @return \Illuminate\Http\Response |
||
| 137 | */ |
||
| 138 | public function destroy(Request $request, $id) |
||
| 150 | |||
| 151 | public function clear($id) |
||
| 159 | } |
||
| 160 |
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: