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 Boards extends Provider |
||
12 | { |
||
13 | use SearchTrait, FollowTrait; |
||
14 | |||
15 | /** |
||
16 | * Get boards for user by username |
||
17 | * @param string $username |
||
18 | * @return array|bool |
||
19 | */ |
||
20 | public function forUser($username) |
||
26 | |||
27 | /** |
||
28 | * Get info about user's board |
||
29 | * @param string $username |
||
30 | * @param string $board |
||
31 | * @return array|bool |
||
32 | */ |
||
33 | public function info($username, $board) |
||
47 | |||
48 | /** |
||
49 | * Get pins form board by boardId |
||
50 | * @param int $boardId |
||
51 | * @param int $batchesLimit |
||
52 | * @return \Iterator |
||
53 | */ |
||
54 | public function pins($boardId, $batchesLimit = 0) |
||
63 | |||
64 | /** |
||
65 | * @param int $boardId |
||
66 | * @param array $bookmarks |
||
67 | * @return array|bool |
||
68 | */ |
||
69 | protected function getPinsFromBoard($boardId, $bookmarks = []) |
||
77 | |||
78 | /** |
||
79 | * Run GET api request to boards resource |
||
80 | * @param array $query |
||
81 | * @param string $url |
||
82 | * @param bool $pagination |
||
83 | * @return array|bool |
||
84 | */ |
||
85 | protected function boardsGetCall($query, $url, $pagination = false) |
||
95 | |||
96 | /** |
||
97 | * Delete your board by ID |
||
98 | * |
||
99 | * @param int $boardId |
||
100 | * @return array|bool |
||
101 | */ |
||
102 | public function delete($boardId) |
||
106 | |||
107 | /** |
||
108 | * Create a new board |
||
109 | * |
||
110 | * @param string $name |
||
111 | * @param string $description |
||
112 | * @param string $privacy Can be 'public' or 'secret'. 'public by default. |
||
113 | * @return array|bool |
||
114 | */ |
||
115 | public function create($name, $description, $privacy = 'public') |
||
125 | |||
126 | /** |
||
127 | * @param $boardId |
||
128 | * @param string $url |
||
129 | * @param string $sourceUrl |
||
130 | * @param array $bookmarks |
||
131 | * @return array |
||
132 | * @internal param string $username |
||
133 | */ |
||
134 | View Code Duplication | public function getBoardData($boardId, $url, $sourceUrl, $bookmarks = []) |
|
142 | |||
143 | /** |
||
144 | * @param $boardId |
||
145 | * @param string $resourceUrl |
||
146 | * @param string $sourceUrl |
||
147 | * @param int $batchesLimit |
||
148 | * @return Iterator |
||
149 | */ |
||
150 | public function getPaginatedUserData($boardId, $resourceUrl, $sourceUrl, $batchesLimit = 0) |
||
160 | |||
161 | /** |
||
162 | * Get board followers |
||
163 | * |
||
164 | * @param $boardId |
||
165 | * @param int $batchesLimit |
||
166 | * @return Iterator |
||
167 | */ |
||
168 | public function followers($boardId, $batchesLimit = 0) |
||
174 | |||
175 | /** |
||
176 | * Search scope |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function getScope() |
||
184 | |||
185 | protected function getEntityIdName() |
||
189 | |||
190 | /** |
||
191 | * Follow resource |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | protected function getFollowUrl() |
||
199 | |||
200 | /** |
||
201 | * UnFollow resource |
||
202 | * @return string |
||
203 | */ |
||
204 | protected function getUnfFollowUrl() |
||
208 | } |
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.