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 |
||
| 10 | class NotificationController extends Controller |
||
| 11 | { |
||
| 12 | public function __construct(NotificationService $notificationService) |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Display a listing of the resource. |
||
| 19 | * |
||
| 20 | * @return \Illuminate\Http\Response |
||
| 21 | */ |
||
| 22 | public function index(Request $request) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Display a listing of the resource searched. |
||
| 32 | * |
||
| 33 | * @return \Illuminate\Http\Response |
||
| 34 | */ |
||
| 35 | public function search(Request $request) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Display the specified resource. |
||
| 43 | * |
||
| 44 | * @param int $id |
||
| 45 | * @return \Illuminate\Http\Response |
||
| 46 | */ |
||
| 47 | public function read($uuid) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Remove the specified resource from storage. |
||
| 57 | * |
||
| 58 | * @param int $id |
||
| 59 | * @return \Illuminate\Http\Response |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function delete($id) |
|
| 71 | } |
||
| 72 |
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: