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 |
||
8 | class CacheNotificationDecorator extends BaseCacheDecorator implements NotificationRepository |
||
9 | { |
||
10 | public function __construct(NotificationRepository $notification) |
||
16 | |||
17 | /** |
||
18 | * @param int $userId |
||
19 | * @return \Illuminate\Database\Eloquent\Collection |
||
20 | */ |
||
21 | View Code Duplication | public function latestForUser($userId) |
|
33 | |||
34 | /** |
||
35 | * Mark the given notification id as "read" |
||
36 | * @param int $notificationId |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function markNotificationAsRead($notificationId) |
||
45 | |||
46 | /** |
||
47 | * Get all the notifications for the given user id |
||
48 | * @param int $userId |
||
49 | * @return \Illuminate\Database\Eloquent\Collection |
||
50 | */ |
||
51 | View Code Duplication | public function allForUser($userId) |
|
63 | |||
64 | /** |
||
65 | * Get all the read notifications for the given user id |
||
66 | * @param int $userId |
||
67 | * @return \Illuminate\Database\Eloquent\Collection |
||
68 | */ |
||
69 | View Code Duplication | public function allReadForUser($userId) |
|
81 | |||
82 | /** |
||
83 | * Get all the unread notifications for the given user id |
||
84 | * @param int $userId |
||
85 | * @return \Illuminate\Database\Eloquent\Collection |
||
86 | */ |
||
87 | View Code Duplication | public function allUnreadForUser($userId) |
|
99 | |||
100 | /** |
||
101 | * Delete all the notifications for the given user |
||
102 | * @param int $userId |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function deleteAllForUser($userId) |
||
111 | |||
112 | /** |
||
113 | * Mark all the notifications for the given user as read |
||
114 | * @param int $userId |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function markAllAsReadForUser($userId) |
||
123 | } |
||
124 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.