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 |
||
| 12 | class EventCategoryController extends Controller |
||
| 13 | { |
||
| 14 | /* Restrict the access to this resource just to logged in users except show view */ |
||
| 15 | public function __construct() |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Display a listing of the resource. |
||
| 22 | * |
||
| 23 | * @return \Illuminate\Http\Response |
||
| 24 | */ |
||
| 25 | public function index() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Show the form for creating a new resource. |
||
| 39 | * |
||
| 40 | * @return \Illuminate\Http\Response |
||
| 41 | */ |
||
| 42 | public function create() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Store a newly created resource in storage. |
||
| 49 | * |
||
| 50 | * @param \Illuminate\Http\Request $request |
||
| 51 | * @return \Illuminate\Http\Response |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function store(Request $request) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Display the specified resource. |
||
| 74 | * |
||
| 75 | * @param \App\EventCategory $eventCategory |
||
| 76 | * @return \Illuminate\Http\Response |
||
| 77 | */ |
||
| 78 | public function show(EventCategory $eventCategory) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Show the form for editing the specified resource. |
||
| 85 | * |
||
| 86 | * @param \App\EventCategory $eventCategory |
||
| 87 | * @return \Illuminate\Http\Response |
||
| 88 | */ |
||
| 89 | public function edit(EventCategory $eventCategory) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Update the specified resource in storage. |
||
| 96 | * |
||
| 97 | * @param \Illuminate\Http\Request $request |
||
| 98 | * @param \App\EventCategory $eventCategory |
||
| 99 | * @return \Illuminate\Http\Response |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function update(Request $request, EventCategory $eventCategory) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Remove the specified resource from storage. |
||
| 115 | * |
||
| 116 | * @param \App\EventCategory $eventCategory |
||
| 117 | * @return \Illuminate\Http\Response |
||
| 118 | */ |
||
| 119 | public function destroy(EventCategory $eventCategory) |
||
| 126 | |||
| 127 | // ********************************************************************** |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Return the single event category datas by cat id. |
||
| 131 | * |
||
| 132 | * @param int $cat_id |
||
| 133 | * @return \App\EventCategory |
||
| 134 | */ |
||
| 135 | public function eventcategorydata($cat_id) |
||
| 142 | |||
| 143 | // ********************************************************************** |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Save/Update the record on DB. |
||
| 147 | * |
||
| 148 | * @param \Illuminate\Http\Request $request |
||
| 149 | * @param \App\EventCategory $eventCategory |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function saveOnDb($request, $eventCategory) |
||
| 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: