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 |
||
16 | trait MonitorTrait |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @param string $end string |
||
21 | * @return string |
||
22 | * @internal |
||
23 | */ |
||
24 | abstract protected function endpoint($end = null); |
||
25 | |||
26 | /** |
||
27 | * @return \Freshdesk\Api |
||
28 | * @internal |
||
29 | */ |
||
30 | abstract protected function api(); |
||
31 | |||
32 | /** |
||
33 | * Monitor a resource |
||
34 | * |
||
35 | * Monitor a resource for the given user |
||
36 | * |
||
37 | * @api |
||
38 | * @param $id The id of the resource |
||
39 | * @param $userId The id of the user |
||
40 | * @return array|null |
||
41 | * @throws \Freshdesk\Exceptions\AccessDeniedException |
||
42 | * @throws \Freshdesk\Exceptions\ApiException |
||
43 | * @throws \Freshdesk\Exceptions\AuthenticationException |
||
44 | * @throws \Freshdesk\Exceptions\ConflictingStateException |
||
45 | * @throws \Freshdesk\Exceptions\NotFoundException |
||
46 | * @throws \Freshdesk\Exceptions\RateLimitExceededException |
||
47 | * @throws \Freshdesk\Exceptions\UnsupportedContentTypeException |
||
48 | * @throws \Freshdesk\Exceptions\MethodNotAllowedException |
||
49 | * @throws \Freshdesk\Exceptions\UnsupportedAcceptHeaderException |
||
50 | * @throws \Freshdesk\Exceptions\ValidationException |
||
51 | */ |
||
52 | View Code Duplication | public function monitor($id, $userId) |
|
60 | |||
61 | /** |
||
62 | * Unmonitor a resource |
||
63 | * |
||
64 | * Unmonitor a resource for the given user |
||
65 | * |
||
66 | * @api |
||
67 | * @param $id The id of the resource |
||
68 | * @param $userId The id of the user |
||
69 | * @return array|null |
||
70 | * @throws \Freshdesk\Exceptions\AccessDeniedException |
||
71 | * @throws \Freshdesk\Exceptions\ApiException |
||
72 | * @throws \Freshdesk\Exceptions\AuthenticationException |
||
73 | * @throws \Freshdesk\Exceptions\ConflictingStateException |
||
74 | * @throws \Freshdesk\Exceptions\NotFoundException |
||
75 | * @throws \Freshdesk\Exceptions\RateLimitExceededException |
||
76 | * @throws \Freshdesk\Exceptions\UnsupportedContentTypeException |
||
77 | * @throws \Freshdesk\Exceptions\MethodNotAllowedException |
||
78 | * @throws \Freshdesk\Exceptions\UnsupportedAcceptHeaderException |
||
79 | * @throws \Freshdesk\Exceptions\ValidationException |
||
80 | */ |
||
81 | View Code Duplication | public function unmonitor($id, $userId) |
|
89 | |||
90 | /** |
||
91 | * Monitor status |
||
92 | * |
||
93 | * Get the monitoring status of the topic for the user |
||
94 | * |
||
95 | * @api |
||
96 | * @param $id The id of the resource |
||
97 | * @param $userId The id of the user |
||
98 | * @return array|null |
||
99 | * @throws \Freshdesk\Exceptions\AccessDeniedException |
||
100 | * @throws \Freshdesk\Exceptions\ApiException |
||
101 | * @throws \Freshdesk\Exceptions\AuthenticationException |
||
102 | * @throws \Freshdesk\Exceptions\ConflictingStateException |
||
103 | * @throws \Freshdesk\Exceptions\NotFoundException |
||
104 | * @throws \Freshdesk\Exceptions\RateLimitExceededException |
||
105 | * @throws \Freshdesk\Exceptions\UnsupportedContentTypeException |
||
106 | * @throws \Freshdesk\Exceptions\MethodNotAllowedException |
||
107 | * @throws \Freshdesk\Exceptions\UnsupportedAcceptHeaderException |
||
108 | * @throws \Freshdesk\Exceptions\ValidationException |
||
109 | */ |
||
110 | View Code Duplication | public function monitorStatus($id, $userId) |
|
118 | } |
||
119 |
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.