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 NotificationController extends Controller |
||
| 12 | { |
||
| 13 | |||
| 14 | use Helpers; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Display a listing of all notifications |
||
| 18 | * |
||
| 19 | * @param Request $request |
||
| 20 | * @param null $type archive or null (all) |
||
| 21 | * @return \Illuminate\Http\Response |
||
| 22 | */ |
||
| 23 | public function index(Request $request, $type = null) |
||
| 34 | |||
| 35 | public function update(Request $request, $id, $action) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Create a new notification |
||
| 60 | * |
||
| 61 | * @return \Illuminate\Http\Response |
||
| 62 | */ |
||
| 63 | public function create(Request $request) |
||
| 77 | |||
| 78 | } |
||
| 79 |
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: