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 | /**  | 
            ||
| 13 | * Constructor  | 
            ||
| 14 | */  | 
            ||
| 15 | public function __construct()  | 
            ||
| 21 | |||
| 22 | /**  | 
            ||
| 23 | * Show the notifications.  | 
            ||
| 24 | *  | 
            ||
| 25 | * @return \Illuminate\View\View  | 
            ||
| 26 | */  | 
            ||
| 27 | public function index(): View  | 
            ||
| 28 |     { | 
            ||
| 29 | $user = User::find(Auth::user()->id);  | 
            ||
| 30 | |||
| 31 |         $this->breadcrumbs->setCssClasses('breadcrumb'); | 
            ||
| 32 | |||
| 33 | $notifications = $user->notifications()  | 
            ||
| 34 |             ->paginate(config('xetaravel.pagination.notification.notification_per_page')); | 
            ||
| 35 | $hasUnreadNotifications = $user->unreadNotifications->isNotEmpty();  | 
            ||
| 36 | |||
| 37 | return view(  | 
            ||
| 38 | 'notification.index',  | 
            ||
| 39 | [  | 
            ||
| 40 | 'user' => $user,  | 
            ||
| 41 | 'breadcrumbs' => $this->breadcrumbs,  | 
            ||
| 42 | 'notifications' => $notifications,  | 
            ||
| 43 | 'hasUnreadNotifications' => $hasUnreadNotifications  | 
            ||
| 44 | ]  | 
            ||
| 45 | );  | 
            ||
| 46 | }  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * Delete a notification by its id.  | 
            ||
| 50 | *  | 
            ||
| 51 | * @param \Illuminate\Http\Request $request The current request.  | 
            ||
| 52 | *  | 
            ||
| 53 | * @return \Illuminate\Http\JsonResponse  | 
            ||
| 54 | */  | 
            ||
| 55 | View Code Duplication | public function delete(Request $request): JsonResponse  | 
            |
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Mark a notification as read.  | 
            ||
| 73 | *  | 
            ||
| 74 | * @param \Illuminate\Http\Request $request The current request.  | 
            ||
| 75 | *  | 
            ||
| 76 | * @return \Illuminate\Http\JsonResponse  | 
            ||
| 77 | */  | 
            ||
| 78 | View Code Duplication | public function markAsRead(Request $request): JsonResponse  | 
            |
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * Mark all notifications as read.  | 
            ||
| 96 | *  | 
            ||
| 97 | * @return \Illuminate\Http\JsonResponse  | 
            ||
| 98 | */  | 
            ||
| 99 | public function markAllAsRead(): JsonResponse  | 
            ||
| 108 | }  | 
            ||
| 109 | 
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: