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 NewbbPermissionForumHandler 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 NewbbPermissionForumHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class NewbbPermissionForumHandler extends NewbbPermissionHandler |
||
27 | { |
||
28 | /** |
||
29 | * @param XoopsDatabase|null $db |
||
30 | */ |
||
31 | public function __construct(XoopsDatabase $db) |
||
36 | |||
37 | /** |
||
38 | * @param bool $fullname |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getValidPerms($fullname = false) |
||
57 | |||
58 | /** |
||
59 | * @param $mid |
||
60 | * @param int $id |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getValidItems($mid, $id = 0) |
||
97 | |||
98 | /* |
||
99 | * Returns permissions for a certain type |
||
100 | * |
||
101 | * @param int $id id of the item (forum, topic or possibly post) to get permissions for |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | /** |
||
106 | * @param int $id |
||
107 | * @return bool|array |
||
108 | */ |
||
109 | public function getPermissions($id = 0) |
||
155 | |||
156 | /** |
||
157 | * @param NewbbForum|int $forum |
||
158 | * @param bool $topic_locked |
||
159 | * @param bool $isAdmin |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false) |
||
163 | { |
||
164 | $perm = []; |
||
165 | |||
166 | $forumId = $forum; |
||
167 | if (is_object($forum)) { |
||
168 | $forumId = $forum->getVar('forum_id'); |
||
169 | } |
||
170 | |||
171 | $permission_set = $this->getPermissions($forumId); |
||
172 | |||
173 | $permItems = $this->getValidPerms(); |
||
174 | foreach ($permItems as $item) { |
||
175 | if ($item === 'access') { |
||
176 | continue; |
||
177 | } |
||
178 | if ($isAdmin |
||
179 | || (isset($permission_set[$forumId]['forum_' . $item]) |
||
180 | && (!$topic_locked |
||
181 | || $item === 'view'))) { |
||
182 | $perm[] = constant('_MD_NEWBB_CAN_' . strtoupper($item)); |
||
183 | } else { |
||
184 | $perm[] = constant('_MD_NEWBB_CANNOT_' . strtoupper($item)); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | return $perm; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param $forum_id |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function deleteByForum($forum_id) |
||
209 | |||
210 | /** |
||
211 | * @param $forum |
||
212 | * @param int $mid |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function applyTemplate($forum, $mid = 0) |
||
250 | |||
251 | /** |
||
252 | * @return mixed|null |
||
253 | */ |
||
254 | public function getTemplate() |
||
261 | |||
262 | /** |
||
263 | * @param $perms |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function setTemplate($perms) |
||
272 | } |
||
273 |
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.