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 | public function __construct(NotificationService $notificationService) |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Display a listing of the resource. |
||
| 20 | * |
||
| 21 | * @return \Illuminate\Http\Response |
||
| 22 | */ |
||
| 23 | public function index(Request $request) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Display a listing of the resource searched. |
||
| 31 | * |
||
| 32 | * @return \Illuminate\Http\Response |
||
| 33 | */ |
||
| 34 | public function search(Request $request) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Show the form for creating a new resource. |
||
| 42 | * |
||
| 43 | * @return \Illuminate\Http\Response |
||
| 44 | */ |
||
| 45 | public function create() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Store a newly created resource in storage. |
||
| 52 | * |
||
| 53 | * @param \Illuminate\Http\NotificationCreateRequest $request |
||
| 54 | * @return \Illuminate\Http\Response |
||
| 55 | */ |
||
| 56 | public function store(NotificationCreateRequest $request) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Show the form for editing the specified resource. |
||
| 71 | * |
||
| 72 | * @param int $id |
||
| 73 | * @return \Illuminate\Http\Response |
||
| 74 | */ |
||
| 75 | public function edit(Request $request, $id) |
||
| 76 | { |
||
| 77 | $notification = $this->service->find($id); |
||
| 78 | return view('transmissor::admin.notifications.edit')->with('notification', $notification); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Update the specified resource in storage. |
||
| 83 | * |
||
| 84 | * @param \Illuminate\Http\NotificationUpdateRequest $request |
||
| 85 | * @param int $id |
||
| 86 | * @return \Illuminate\Http\Response |
||
| 87 | */ |
||
| 88 | public function update(NotificationUpdateRequest $request, $id) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Remove the specified resource from storage. |
||
| 101 | * |
||
| 102 | * @param int $id |
||
| 103 | * @return \Illuminate\Http\Response |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function destroy(Request $request, $id) |
|
| 115 | } |
||
| 116 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: