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 | class Pinners extends Provider |
||
17 | { |
||
18 | use Searchable, Followable, HasFollowers; |
||
19 | |||
20 | protected $loginRequired = [ |
||
21 | 'follow', |
||
22 | 'unFollow', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * Get user info. |
||
27 | * If username param is not specified, will |
||
28 | * return info for logged user. |
||
29 | * |
||
30 | * @param string $username |
||
31 | * |
||
32 | * @return null|array |
||
33 | */ |
||
34 | public function info($username) |
||
35 | { |
||
36 | $res = $this->paginate($username, UrlHelper::RESOURCE_USER_INFO, "/$username/", 1); |
||
37 | $res = iterator_to_array($res); |
||
38 | |||
39 | return !empty($res) ? $res[0] : null; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Get pinner followers. |
||
44 | * |
||
45 | * @param string $username |
||
46 | * @param int $batchesLimit |
||
47 | * |
||
48 | * @return Iterator |
||
49 | */ |
||
50 | public function followers($username, $batchesLimit = 0) |
||
56 | |||
57 | /** |
||
58 | * Get pinner following other pinners. |
||
59 | * |
||
60 | * @param string $username |
||
61 | * @param int $batchesLimit |
||
62 | * |
||
63 | * @return Iterator |
||
64 | */ |
||
65 | public function following($username, $batchesLimit = 0) |
||
71 | |||
72 | /** |
||
73 | * Get pinner pins. |
||
74 | * |
||
75 | * @param string $username |
||
76 | * @param int $batchesLimit |
||
77 | * |
||
78 | * @return Iterator |
||
79 | */ |
||
80 | public function pins($username, $batchesLimit = 0) |
||
86 | |||
87 | /** |
||
88 | * Login as pinner. |
||
89 | * |
||
90 | * @param string $username |
||
91 | * @param string $password |
||
92 | * |
||
93 | * @throws AuthException |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function login($username, $password) |
||
117 | |||
118 | /** |
||
119 | * Validates password and login. |
||
120 | * |
||
121 | * @param string $username |
||
122 | * @param string $password |
||
123 | */ |
||
124 | protected function checkCredentials($username, $password) |
||
130 | |||
131 | /** |
||
132 | * Search scope. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | protected function getScope() |
||
140 | |||
141 | protected function getEntityIdName() |
||
145 | |||
146 | /** |
||
147 | * Follow resource. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | protected function getFollowUrl() |
||
155 | |||
156 | /** |
||
157 | * UnFollow resource. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | protected function getUnfFollowUrl() |
||
165 | |||
166 | /** |
||
167 | * @param string $username |
||
168 | * @param string $url |
||
169 | * @param string $sourceUrl |
||
170 | * @param int $batchesLimit |
||
171 | * |
||
172 | * @return Iterator |
||
173 | */ |
||
174 | View Code Duplication | public function paginate($username, $url, $sourceUrl, $batchesLimit) |
|
185 | } |
||
186 |
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.