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 PermissionHandler 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 PermissionHandler, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Newbb; |
||
28 | class PermissionHandler extends \XoopsGroupPermHandler |
||
29 | { |
||
30 | public $_handler; |
||
31 | /** @var \Xmf\Module\Helper\Cache */ |
||
32 | protected $cacheHelper; |
||
33 | |||
34 | /** |
||
35 | * @param $db |
||
36 | */ |
||
37 | public function __construct(\XoopsDatabase $db) |
||
44 | |||
45 | /** |
||
46 | * @param $name |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function loadHandler($name) |
||
59 | |||
60 | /** |
||
61 | * @param bool $fullname |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function getValidForumPerms($fullname = false) |
||
70 | |||
71 | /** |
||
72 | * @param int $forum |
||
73 | * @param bool $topic_locked |
||
74 | * @param bool $isAdmin |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false) |
||
84 | |||
85 | /** |
||
86 | * @param $forum_id |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public function deleteByForum($forum_id) |
||
96 | |||
97 | /** |
||
98 | * @param $cat_id |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function deleteByCategory($cat_id) |
||
108 | |||
109 | /** |
||
110 | * @param $category |
||
111 | * @param array $groups |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function setCategoryPermission($category, array $groups = []) |
||
121 | |||
122 | /** |
||
123 | * @param $type |
||
124 | * @param string $gperm_name |
||
125 | * @param int $id |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function getPermission($type, $gperm_name = 'access', $id = 0) |
||
150 | |||
151 | /** |
||
152 | * @param string $perm_name |
||
153 | * @return array |
||
154 | */ |
||
155 | public function &getCategories($perm_name = 'access') |
||
161 | |||
162 | /** |
||
163 | * @param string $perm_name |
||
164 | * @return array |
||
165 | */ |
||
166 | public function getForums($perm_name = 'access') |
||
172 | |||
173 | /** |
||
174 | * @param $type |
||
175 | * @param $perm_name |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getAllowedItems($type, $perm_name) |
||
206 | |||
207 | /** |
||
208 | * @param $gperm_name |
||
209 | * @param int $id |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getGroups($gperm_name, $id = 0) |
||
220 | |||
221 | /** |
||
222 | * @param string $perm_name |
||
223 | * @return array |
||
224 | */ |
||
225 | public function createPermData($perm_name = 'forum_all') |
||
274 | |||
275 | /** |
||
276 | * @param string $perm_name |
||
277 | * @return array|mixed|null |
||
278 | */ |
||
279 | public function &loadPermData($perm_name = 'forum_access') |
||
287 | |||
288 | /** |
||
289 | * @param $perm |
||
290 | * @param $itemid |
||
291 | * @param $groupid |
||
292 | * @param null $mid |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function validateRight($perm, $itemid, $groupid, $mid = null) |
||
316 | |||
317 | /** |
||
318 | * Check permission (directly) |
||
319 | * |
||
320 | * @param string $gperm_name Name of permission |
||
321 | * @param int $gperm_itemid ID of an item |
||
322 | * @param int /array $gperm_groupid A group ID or an array of group IDs |
||
323 | * @param int $gperm_modid ID of a module |
||
324 | * |
||
325 | * @return bool TRUE if permission is enabled |
||
326 | */ |
||
327 | public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1) |
||
351 | |||
352 | /** |
||
353 | * @param $perm |
||
354 | * @param $itemid |
||
355 | * @param $groupid |
||
356 | * @param null $mid |
||
357 | * @return bool |
||
358 | */ |
||
359 | public function deleteRight($perm, $itemid, $groupid, $mid = null) |
||
391 | |||
392 | /** |
||
393 | * @param $forum |
||
394 | * @param int $mid |
||
395 | * @return mixed |
||
396 | */ |
||
397 | public function applyTemplate($forum, $mid = 0) |
||
404 | |||
405 | /** |
||
406 | * @return mixed |
||
407 | */ |
||
408 | public function getTemplate() |
||
415 | |||
416 | /** |
||
417 | * @param $perms |
||
418 | * @return mixed |
||
419 | */ |
||
420 | public function setTemplate($perms) |
||
426 | } |
||
427 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.