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:
Complex classes like Group often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Group, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Group extends \FreeIPA\APIAccess\Base |
||
40 | { |
||
41 | /** |
||
42 | * Adds a group |
||
43 | * The main fields in $data: |
||
44 | * 'description' => group description |
||
45 | * If $data is string will be a group description |
||
46 | * |
||
47 | * @param string $name group name |
||
48 | * @param array|string $data see above |
||
49 | * @return object|bool Object with new group data or false if the group was not found |
||
50 | * @since GIT: 0.1.0 |
||
51 | * @version GIT: 0.1.0 |
||
52 | * @see ../../docs/return_samples/group_add.txt |
||
53 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
54 | */ |
||
55 | public function add($name = null, $data = array()) |
||
86 | |||
87 | /** |
||
88 | * Delete a group |
||
89 | * |
||
90 | * @param string $name group name |
||
91 | * @param array|string $data see above |
||
92 | * @return object|bool Object with new group data or false if the group was not found |
||
93 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
94 | */ |
||
95 | public function del($name = null, $data = array()) |
||
120 | |||
121 | /** |
||
122 | * Adds members (users or other groups) to group |
||
123 | * The main fields in $data: |
||
124 | * 'user' => array that contain users that will be added |
||
125 | * 'group' => array that contain groups that will be added |
||
126 | * If $data is a string, will be user uid |
||
127 | * |
||
128 | * @param string $group_name group name |
||
129 | * @param array|string $data See explanation above |
||
130 | * @return mixed Array containing information about processing and group data OR false on error |
||
131 | * @since GIT: 0.1.0 |
||
132 | * @version GIT: 0.1.0 |
||
133 | * @see ../../docs/return_samples/group_add_member.txt |
||
134 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
135 | * @throws \Exception if the request was not completed successfully |
||
136 | */ |
||
137 | View Code Duplication | public function addMember($group_name = null, $data = array()) |
|
185 | |||
186 | /** |
||
187 | * Deletes members (users or other groups) to group |
||
188 | * The main fields in $data: |
||
189 | * 'user' => array that contain users that will be added |
||
190 | * 'group' => array that contain groups that will be added |
||
191 | * If $data is a string, will be user uid |
||
192 | * |
||
193 | * @param string $group_name group name |
||
194 | * @param array|string $data See explanation above |
||
195 | * @return mixed Array containing information about processing and group data OR false on error |
||
196 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
197 | * @throws \Exception if the request was not completed successfully |
||
198 | */ |
||
199 | View Code Duplication | public function removeMember($group_name = null, $data = array()) |
|
247 | |||
248 | /** |
||
249 | * Search group through of group_find method. |
||
250 | * |
||
251 | * @param array $args arguments for user_find method |
||
252 | * @param array $options options for user_find method |
||
253 | * @return array|bool false if the group was not found |
||
254 | * @throws \Exception if error in json return |
||
255 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
256 | */ |
||
257 | public function find($args = array(), $options = array()) |
||
288 | |||
289 | /** |
||
290 | * Search group by field |
||
291 | * |
||
292 | * @param array $field field name. |
||
293 | * @param string $value field value |
||
294 | * @return array|bool false if the group was not found |
||
295 | * @see find() |
||
296 | */ |
||
297 | View Code Duplication | public function findBy($field = null, $value = null) |
|
306 | |||
307 | /** |
||
308 | * Get group data by cn through group_show method |
||
309 | * |
||
310 | * @param string|array $params cn or some parameters |
||
311 | * @param array $options options for group_show method |
||
312 | * @return array|bool false if the group was not found |
||
313 | * @throws \Exception se houver erro no retorno json |
||
314 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
315 | */ |
||
316 | View Code Duplication | public function get($params = null, $options = array()) |
|
359 | |||
360 | /** |
||
361 | * Change group data |
||
362 | * |
||
363 | * If group does not exists, the \FreeIPA\APIAccess\Connection\buildRequest() method will return \Exception. |
||
364 | * |
||
365 | * @param string $cn of group that will be changed |
||
366 | * @param array $data See above |
||
367 | * @return object|bool Object with new group data or false if the group was not found |
||
368 | * @see \FreeIPA\APIAccess\Connection\buildRequest() |
||
369 | */ |
||
370 | View Code Duplication | public function modify($cn = null, $data = array()) |
|
396 | } |
||
397 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: