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 | trait Notifable |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Get the notifications Relationship. |
||
| 15 | * |
||
| 16 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 17 | */ |
||
| 18 | public function notifications() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get a new NotifynderManager instance with the given category. |
||
| 30 | * |
||
| 31 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
||
| 32 | * @return \Fenos\Notifynder\Managers\NotifynderManager |
||
| 33 | */ |
||
| 34 | public function notifynder($category) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get a new NotifynderManager instance with the given category and $this as the sender. |
||
| 41 | * |
||
| 42 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
||
| 43 | * @return \Fenos\Notifynder\Managers\NotifynderManager |
||
| 44 | */ |
||
| 45 | public function sendNotificationFrom($category) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get a new NotifynderManager instance with the given category and $this as the receiver. |
||
| 52 | * |
||
| 53 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
||
| 54 | * @return \Fenos\Notifynder\Managers\NotifynderManager |
||
| 55 | */ |
||
| 56 | public function sendNotificationTo($category) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Read a single Notification. |
||
| 63 | * |
||
| 64 | * @param int $notification |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function readNotification($notification) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Read all Notifications. |
||
| 82 | * |
||
| 83 | * @return mixed |
||
| 84 | */ |
||
| 85 | public function readAllNotifications() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Unread a single Notification. |
||
| 92 | * |
||
| 93 | * @param int $notification |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function unreadNotification($notification) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Unread all Notifications. |
||
| 111 | * |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public function unreadAllNotifications() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Count unread notifications. |
||
| 121 | * |
||
| 122 | * @return int |
||
| 123 | */ |
||
| 124 | public function countUnreadNotifications() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get all Notifications ordered by creation and optional limit. |
||
| 131 | * |
||
| 132 | * @param null|int $limit |
||
| 133 | * @param string $order |
||
| 134 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 135 | */ |
||
| 136 | public function getNotifications($limit = null, $order = 'desc') |
||
| 145 | } |
||
| 146 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.