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 |
||
21 | class User |
||
22 | { |
||
23 | /** |
||
24 | * Query. |
||
25 | * |
||
26 | * @var Query |
||
27 | */ |
||
28 | protected $query; |
||
29 | |||
30 | /** |
||
31 | * LDAP Group object. |
||
32 | * |
||
33 | * @var Group |
||
34 | */ |
||
35 | protected $group; |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param Query $query |
||
41 | * @param Group $group |
||
42 | */ |
||
43 | public function __construct(Query $query, Group $group = null) |
||
48 | |||
49 | /** |
||
50 | * Get user profile. |
||
51 | * |
||
52 | * @static |
||
53 | * |
||
54 | * @param Client $client |
||
55 | * @param string $username |
||
56 | * |
||
57 | * @return LdapUserProvider |
||
58 | */ |
||
59 | public static function getUser(Client $client, $username) |
||
65 | |||
66 | /** |
||
67 | * Find user. |
||
68 | * |
||
69 | * @param string $query |
||
70 | * |
||
71 | * @return LdapUserProvider |
||
72 | */ |
||
73 | View Code Duplication | public function find($query) |
|
84 | |||
85 | /** |
||
86 | * Get user groupIds (DN). |
||
87 | * |
||
88 | * 1) If configured, use memberUid and posixGroup |
||
89 | * 2) Otherwise, use memberOf |
||
90 | * |
||
91 | * @param Entry $entry |
||
92 | * @param string $username |
||
93 | * |
||
94 | * @return string[] |
||
95 | */ |
||
96 | protected function getGroups(Entry $entry, $username) |
||
112 | |||
113 | /** |
||
114 | * Get role from LDAP groups. |
||
115 | * |
||
116 | * Note: Do not touch the current role if groups are not configured |
||
117 | * |
||
118 | * @param string[] $groupIds |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function getRole(array $groupIds) |
||
140 | |||
141 | /** |
||
142 | * Build user profile. |
||
143 | * |
||
144 | * @return LdapUserProvider |
||
145 | */ |
||
146 | protected function build() |
||
163 | |||
164 | /** |
||
165 | * Ge the list of attributes to fetch when reading the LDAP user entry. |
||
166 | * |
||
167 | * Must returns array with index that start at 0 otherwise ldap_search returns a warning "Array initialization wrong" |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getAttributes() |
||
182 | |||
183 | /** |
||
184 | * Get LDAP account id attribute. |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getAttributeUsername() |
||
196 | |||
197 | /** |
||
198 | * Get LDAP user name attribute. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getAttributeName() |
||
210 | |||
211 | /** |
||
212 | * Get LDAP account email attribute. |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getAttributeEmail() |
||
224 | |||
225 | /** |
||
226 | * Get LDAP account memberOf attribute. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getAttributeGroup() |
||
234 | |||
235 | /** |
||
236 | * Get LDAP profile photo attribute. |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getAttributePhoto() |
||
244 | |||
245 | /** |
||
246 | * Get LDAP language attribute. |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getAttributeLanguage() |
||
254 | |||
255 | /** |
||
256 | * Get LDAP Group User filter. |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getGroupUserFilter() |
||
264 | |||
265 | /** |
||
266 | * Return true if LDAP Group User filter is defined. |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | public function hasGroupUserFilter() |
||
274 | |||
275 | /** |
||
276 | * Return true if LDAP Group mapping are configured. |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | public function hasGroupsConfigured() |
||
284 | |||
285 | /** |
||
286 | * Get LDAP admin group DN. |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getGroupAdminDn() |
||
294 | |||
295 | /** |
||
296 | * Get LDAP application manager group DN. |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getGroupManagerDn() |
||
304 | |||
305 | /** |
||
306 | * Get LDAP user base DN. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getBasDn() |
||
318 | |||
319 | /** |
||
320 | * Get LDAP user pattern. |
||
321 | * |
||
322 | * @param string $username |
||
323 | * @param string $filter |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getLdapUserPattern($username, $filter = LDAP_USER_FILTER) |
||
335 | } |
||
336 |
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.