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 | class Inbox extends Provider |
||
12 | { |
||
13 | use SendsMessages; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $loginRequiredFor = [ |
||
19 | 'news', |
||
20 | 'sendEmail', |
||
21 | 'sendMessage', |
||
22 | 'notifications', |
||
23 | 'conversations', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @param int $limit |
||
28 | * @return Pagination |
||
29 | */ |
||
30 | public function news($limit = Pagination::DEFAULT_LIMIT) |
||
36 | |||
37 | /** |
||
38 | * @param int $limit |
||
39 | * @return Pagination |
||
40 | */ |
||
41 | public function notifications($limit = Pagination::DEFAULT_LIMIT) |
||
45 | |||
46 | /** |
||
47 | * Get last user conversations. |
||
48 | * |
||
49 | * @return array|bool |
||
50 | */ |
||
51 | public function conversations() |
||
55 | |||
56 | /** |
||
57 | * Send message to a user. |
||
58 | * |
||
59 | * @param array|string $userIds |
||
60 | * @param string $text |
||
61 | * @param int|null $pinId |
||
62 | * @throws InvalidRequest |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function sendMessage($userIds, $text, $pinId = null) |
||
69 | |||
70 | /** |
||
71 | * Send email. |
||
72 | * |
||
73 | * @param array|string $emails |
||
74 | * @param string $text |
||
75 | * @param int|null $pinId |
||
76 | * @throws InvalidRequest |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function sendEmail($emails, $text, $pinId = null) |
||
83 | |||
84 | public function contactRequests() |
||
90 | |||
91 | /** |
||
92 | * @param string $requestId |
||
93 | * @return bool |
||
94 | */ |
||
95 | View Code Duplication | public function acceptContactRequest($requestId) |
|
106 | |||
107 | /** |
||
108 | * @param string $requestId |
||
109 | * @return bool |
||
110 | */ |
||
111 | View Code Duplication | public function ignoreContactRequests($requestId) |
|
122 | } |
||
123 |
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.