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 |
||
10 | class Pinners extends Provider |
||
11 | { |
||
12 | use SearchHelper; |
||
13 | |||
14 | /** |
||
15 | * Follow user by user_id |
||
16 | * |
||
17 | * @param integer $userId |
||
18 | * @return bool |
||
19 | */ |
||
20 | View Code Duplication | public function follow($userId) |
|
27 | |||
28 | /** |
||
29 | * Unfollow user by user_id |
||
30 | * |
||
31 | * @param integer $userId |
||
32 | * @return bool |
||
33 | */ |
||
34 | View Code Duplication | public function unFollow($userId) |
|
41 | |||
42 | /** |
||
43 | * Get different user data, for example, followers, following, pins. |
||
44 | * Collects data while paginating with bookmarks through pinterest results. |
||
45 | * Return array. Key data - for results and key bookmarks - for pagination. |
||
46 | * |
||
47 | * @param string $username |
||
48 | * @param string $url |
||
49 | * @param string $sourceUrl |
||
50 | * @param array $bookmarks |
||
51 | * @return array |
||
52 | */ |
||
53 | public function getUserData($username, $url, $sourceUrl, $bookmarks = []) |
||
60 | |||
61 | /** |
||
62 | * @param string $username |
||
63 | * @param string $resourceUrl |
||
64 | * @param string $sourceUrl |
||
65 | * @param int $batchesLimit |
||
66 | * @return \Iterator |
||
67 | */ |
||
68 | public function getPaginatedUserData($username, $resourceUrl, $sourceUrl, $batchesLimit = 0) |
||
80 | |||
81 | /** |
||
82 | * Get the logged-in account username |
||
83 | * |
||
84 | * @return array|null |
||
85 | */ |
||
86 | public function myAccountName() |
||
93 | |||
94 | /** |
||
95 | * Get user info |
||
96 | * If username param is not specified, will |
||
97 | * return info for logged user |
||
98 | * |
||
99 | * @param string $username |
||
100 | * @return null|array |
||
101 | */ |
||
102 | public function info($username) |
||
107 | |||
108 | /** |
||
109 | * Get pinner followers |
||
110 | * |
||
111 | * @param string $username |
||
112 | * @param int $batchesLimit |
||
113 | * @return \Iterator |
||
114 | */ |
||
115 | public function followers($username, $batchesLimit = 0) |
||
121 | |||
122 | /** |
||
123 | * Get pinner following other pinners |
||
124 | * |
||
125 | * @param string $username |
||
126 | * @param int $batchesLimit |
||
127 | * @return \Iterator |
||
128 | */ |
||
129 | public function following($username, $batchesLimit = 0) |
||
135 | |||
136 | /** |
||
137 | * Get pinner pins |
||
138 | * |
||
139 | * @param string $username |
||
140 | * @param int $batchesLimit |
||
141 | * @return \Iterator |
||
142 | */ |
||
143 | public function pins($username, $batchesLimit = 0) |
||
149 | |||
150 | /** |
||
151 | * Login as pinner |
||
152 | * |
||
153 | * @param $username |
||
154 | * @param $password |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function login($username, $password) |
||
173 | |||
174 | protected function getScope() |
||
178 | } |
||
179 |
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.