Total Complexity | 62 |
Total Lines | 397 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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) |
||
38 | { |
||
39 | $this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
||
40 | |||
41 | $this->db = $db; |
||
42 | parent::__construct($db); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param $name |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function loadHandler($name) |
||
50 | { |
||
51 | if (!isset($this->_handler[$name])) { |
||
52 | // require_once __DIR__ . "/permission.{$name}.php"; |
||
53 | $className = '\\XoopsModules\\Newbb\\Permission' . ucfirst($name) . 'Handler'; |
||
54 | $this->_handler[$name] = new $className($this->db); |
||
55 | } |
||
56 | |||
57 | return $this->_handler[$name]; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param bool $fullname |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function getValidForumPerms($fullname = false) |
||
65 | { |
||
66 | $handler = $this->loadHandler('forum'); |
||
67 | |||
68 | return $handler->getValidPerms($fullname); |
||
69 | } |
||
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) |
||
78 | { |
||
79 | $handler = $this->loadHandler('forum'); |
||
80 | $perm = $handler->getPermissionTable($forum, $topic_locked, $isAdmin); |
||
81 | |||
82 | return $perm; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $forum_id |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public function deleteByForum($forum_id) |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param $cat_id |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function deleteByCategory($cat_id) |
||
102 | { |
||
103 | $this->cacheHelper->delete('permission_category'); |
||
104 | $handler = $this->loadHandler('category'); |
||
105 | |||
106 | return $handler->deleteByCategory($cat_id); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $category |
||
111 | * @param array $groups |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function setCategoryPermission($category, array $groups = []) |
||
115 | { |
||
116 | $this->cacheHelper->delete('permission_category'); |
||
117 | $handler = $this->loadHandler('category'); |
||
118 | |||
119 | return $handler->setCategoryPermission($category, $groups); |
||
120 | } |
||
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) |
||
129 | { |
||
130 | global $xoopsModule; |
||
131 | $ret = false; |
||
132 | if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) { |
||
133 | $ret = true; |
||
134 | } |
||
135 | |||
136 | $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
137 | if (!$groups) { |
||
138 | $ret = false; |
||
139 | } |
||
140 | if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) { |
||
141 | $ret = false; |
||
142 | } |
||
143 | |||
144 | if (count(array_intersect($allowed_groups, $groups)) > 0) { |
||
145 | $ret = true; |
||
146 | } |
||
147 | |||
148 | return $ret; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $perm_name |
||
153 | * @return array |
||
154 | */ |
||
155 | public function &getCategories($perm_name = 'access') |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param string $perm_name |
||
164 | * @return array |
||
165 | */ |
||
166 | public function getForums($perm_name = 'access') |
||
167 | { |
||
168 | $ret = $this->getAllowedItems('forum', "forum_{$perm_name}"); |
||
169 | |||
170 | return $ret; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param $type |
||
175 | * @param $perm_name |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getAllowedItems($type, $perm_name) |
||
179 | { |
||
180 | $ret = []; |
||
181 | |||
182 | $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
183 | if (count($groups) < 1) { |
||
184 | return $ret; |
||
185 | } |
||
186 | |||
187 | if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) { |
||
188 | return $ret; |
||
189 | } |
||
190 | |||
191 | $allowed_items = []; |
||
192 | foreach ($_cachedPerms as $id => $allowed_groups) { |
||
193 | if (0 == $id || empty($allowed_groups)) { |
||
194 | continue; |
||
195 | } |
||
196 | |||
197 | if (array_intersect($groups, $allowed_groups)) { |
||
198 | $allowed_items[$id] = 1; |
||
199 | } |
||
200 | } |
||
201 | unset($_cachedPerms); |
||
202 | $ret = array_keys($allowed_items); |
||
203 | |||
204 | return $ret; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param $gperm_name |
||
209 | * @param int $id |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getGroups($gperm_name, $id = 0) |
||
213 | { |
||
214 | $_cachedPerms = $this->loadPermData($gperm_name); |
||
215 | $groups = empty($_cachedPerms[$id]) ? [] : array_unique($_cachedPerms[$id]); |
||
216 | unset($_cachedPerms); |
||
217 | |||
218 | return $groups; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param string $perm_name |
||
223 | * @return array |
||
224 | */ |
||
225 | public function createPermData($perm_name = 'forum_all') |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param string $perm_name |
||
277 | * @return array|mixed|null |
||
278 | */ |
||
279 | public function &loadPermData($perm_name = 'forum_access') |
||
280 | { |
||
281 | if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) { |
||
282 | $perms = $this->createPermData($perm_name); |
||
283 | } |
||
284 | |||
285 | return $perms; |
||
286 | } |
||
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) |
||
315 | } |
||
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) |
||
328 | { |
||
329 | $ret = false; |
||
330 | $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid)); |
||
331 | $criteria->add(new \Criteria('gperm_name', $gperm_name)); |
||
332 | $gperm_itemid = (int)$gperm_itemid; |
||
333 | if ($gperm_itemid > 0) { |
||
334 | $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid)); |
||
335 | } |
||
336 | if (is_array($gperm_groupid)) { |
||
337 | $criteria2 = new \CriteriaCompo(); |
||
338 | foreach ($gperm_groupid as $gid) { |
||
339 | $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR'); |
||
340 | } |
||
341 | $criteria->add($criteria2); |
||
342 | } else { |
||
343 | $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid)); |
||
344 | } |
||
345 | if ($this->getCount($criteria) > 0) { |
||
346 | $ret = true; |
||
347 | } |
||
348 | |||
349 | return $ret; |
||
350 | } |
||
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) |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @param $forum |
||
394 | * @param int $mid |
||
395 | * @return mixed |
||
396 | */ |
||
397 | public function applyTemplate($forum, $mid = 0) |
||
398 | { |
||
399 | $this->cacheHelper->delete('permission_forum'); |
||
400 | $handler = $this->loadHandler('forum'); |
||
401 | |||
402 | return $handler->applyTemplate($forum, $mid); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @return mixed |
||
407 | */ |
||
408 | public function getTemplate() |
||
409 | { |
||
410 | $handler = $this->loadHandler('forum'); |
||
411 | $template = $handler->getTemplate(); |
||
412 | |||
413 | return $template; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param $perms |
||
418 | * @return mixed |
||
419 | */ |
||
420 | public function setTemplate($perms) |
||
425 | } |
||
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.