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 | trait NotifableBasic |
||
11 | { |
||
12 | /** |
||
13 | * Get the notifications Relationship without any eager loading. |
||
14 | * |
||
15 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
||
16 | */ |
||
17 | abstract public function getLazyNotificationRelation(); |
||
18 | |||
19 | /** |
||
20 | * Get the notifications Relationship. |
||
21 | * |
||
22 | * @param array|bool $eagerLoad |
||
23 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
||
24 | */ |
||
25 | public function getNotificationRelation($eagerLoad = null) |
||
40 | |||
41 | /** |
||
42 | * Get a new NotifynderManager instance with the given category. |
||
43 | * |
||
44 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
||
45 | * @return \Fenos\Notifynder\Managers\NotifynderManager |
||
46 | */ |
||
47 | public function notifynder($category) |
||
51 | |||
52 | /** |
||
53 | * Get a new NotifynderManager instance with the given category and $this as the sender. |
||
54 | * |
||
55 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
||
56 | * @return \Fenos\Notifynder\Managers\NotifynderManager |
||
57 | */ |
||
58 | public function sendNotificationFrom($category) |
||
62 | |||
63 | /** |
||
64 | * Get a new NotifynderManager instance with the given category and $this as the receiver. |
||
65 | * |
||
66 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
||
67 | * @return \Fenos\Notifynder\Managers\NotifynderManager |
||
68 | */ |
||
69 | public function sendNotificationTo($category) |
||
73 | |||
74 | /** |
||
75 | * Read a single Notification. |
||
76 | * |
||
77 | * @param int $notification |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function readNotification($notification) |
||
84 | |||
85 | /** |
||
86 | * Unread a single Notification. |
||
87 | * |
||
88 | * @param int $notification |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function unreadNotification($notification) |
||
95 | |||
96 | /** |
||
97 | * @param int $notification |
||
98 | * @param int $value |
||
99 | * @return bool |
||
100 | */ |
||
101 | protected function updateSingleReadStatus($notification, $value) |
||
113 | |||
114 | /** |
||
115 | * Read all Notifications. |
||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function readAllNotifications() |
||
123 | |||
124 | /** |
||
125 | * Unread all Notifications. |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | public function unreadAllNotifications() |
||
133 | |||
134 | /** |
||
135 | * Count unread notifications. |
||
136 | * |
||
137 | * @return int |
||
138 | */ |
||
139 | public function countUnreadNotifications() |
||
143 | |||
144 | /** |
||
145 | * Get all Notifications ordered by creation and optional limit. |
||
146 | * |
||
147 | * @param null|int $limit |
||
148 | * @param string $order |
||
149 | * @return \Illuminate\Database\Eloquent\Collection |
||
150 | */ |
||
151 | View Code Duplication | public function getNotifications($limit = null, $order = 'desc') |
|
160 | |||
161 | /** |
||
162 | * Get all unread Notifications. |
||
163 | * |
||
164 | * @param null|int $limit |
||
165 | * @param string $order |
||
166 | * @return \Illuminate\Database\Eloquent\Collection |
||
167 | */ |
||
168 | View Code Duplication | public function getNotificationsNotRead($limit = null, $order = 'desc') |
|
177 | } |
||
178 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: