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 |
||
7 | class PinHelper |
||
8 | { |
||
9 | /** |
||
10 | * Create Pinterest API request form commenting pin |
||
11 | * |
||
12 | * @param int $pinId |
||
13 | * @param string $text |
||
14 | * @return array |
||
15 | */ |
||
16 | View Code Duplication | public static function createCommentRequest($pinId, $text) |
|
23 | |||
24 | /** |
||
25 | * Create Pinterest API request form commenting pin |
||
26 | * |
||
27 | * @param int $pinId |
||
28 | * @param int $commentId |
||
29 | * @return array |
||
30 | */ |
||
31 | View Code Duplication | public static function createCommentDeleteRequest($pinId, $commentId) |
|
38 | |||
39 | /** |
||
40 | * Creates Pinterest API request for Pin creation |
||
41 | * |
||
42 | * @param string $description |
||
43 | * @param string $imageUrl |
||
44 | * @param int $boardId |
||
45 | * @return array |
||
46 | */ |
||
47 | public static function createPinCreationRequest($imageUrl, $boardId, $description = "") |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Creates Pinterest API request for Pin repin |
||
65 | * |
||
66 | * @param string $description |
||
67 | * @param int $repinId |
||
68 | * @param int $boardId |
||
69 | * @return array |
||
70 | */ |
||
71 | public static function createRepinRequest($repinId, $boardId, $description) |
||
86 | |||
87 | |||
88 | /** |
||
89 | * Creates Pinterest API request to get Pin info |
||
90 | * |
||
91 | * @param int $pinId |
||
92 | * @return array |
||
93 | */ |
||
94 | public static function createInfoRequest($pinId) |
||
107 | |||
108 | /** |
||
109 | * Creates common pin request data by PinId |
||
110 | * |
||
111 | * @param int $pinId |
||
112 | * @param string $template |
||
113 | * @return array |
||
114 | */ |
||
115 | public static function createPinRequest($pinId, $template = 'id') |
||
124 | |||
125 | /** |
||
126 | * Creates simple Pin request by PinId (used by delete and like requests) |
||
127 | * |
||
128 | * @param int $pinId |
||
129 | * @return array |
||
130 | */ |
||
131 | public static function createSimplePinRequest($pinId) |
||
136 | |||
137 | /** |
||
138 | * @param string|null $sourceUrl |
||
139 | * @param array $data |
||
140 | * @return array |
||
141 | */ |
||
142 | public static function createPinRequestData($data, $sourceUrl = null) |
||
151 | |||
152 | /** |
||
153 | * @param $pinId |
||
154 | * @return array |
||
155 | */ |
||
156 | public static function createPinIdRequest($pinId) |
||
160 | } |
||
161 |
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.