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 |
||
12 | class Permissions |
||
13 | { |
||
14 | private $db; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $reserved_permissions = array( |
||
20 | 'admin_forum', |
||
21 | 'manage_membergroups', |
||
22 | 'manage_permissions', |
||
23 | ); |
||
24 | |||
25 | /** |
||
26 | * @var string[] |
||
27 | */ |
||
28 | private $illegal_permissions = array(); |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $illegal_guest_permissions = array( |
||
34 | 'delete_replies', |
||
35 | 'karma_edit', |
||
36 | 'poll_add_own', |
||
37 | 'pm_read', |
||
38 | 'pm_send', |
||
39 | 'profile_identity', |
||
40 | 'profile_extra', |
||
41 | 'profile_title', |
||
42 | 'profile_remove', |
||
43 | 'profile_set_avatar', |
||
44 | 'profile_view_own', |
||
45 | 'mark_any_notify', |
||
46 | 'mark_notify', |
||
47 | 'admin_forum', |
||
48 | 'manage_boards', |
||
49 | 'manage_attachments', |
||
50 | 'manage_smileys', |
||
51 | 'edit_news', |
||
52 | 'access_mod_center', |
||
53 | 'moderate_forum', |
||
54 | 'issue_warning', |
||
55 | 'manage_membergroups', |
||
56 | 'manage_permissions', |
||
57 | 'manage_bans', |
||
58 | 'move_own', |
||
59 | 'modify_replies', |
||
60 | 'send_mail', |
||
61 | 'approve_posts', |
||
62 | 'postby_email', |
||
63 | 'approve_emails', |
||
64 | 'like_posts', |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * Load a few illegal permissions into context. |
||
69 | * |
||
70 | * Calls hook: integrate_load_illegal_permissions |
||
71 | */ |
||
72 | 4 | public function __construct() |
|
78 | |||
79 | /** |
||
80 | * @return string[] |
||
81 | */ |
||
82 | 4 | public function getIllegalPermissions() |
|
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | 4 | public function getIllegalGuestPermissions() |
|
94 | |||
95 | /** |
||
96 | * @return array |
||
97 | */ |
||
98 | 4 | public function loadIllegal() |
|
99 | { |
||
100 | 4 | global $context; |
|
101 | |||
102 | 4 | $illegal_permissions = array(); |
|
103 | 4 | foreach ($this->reserved_permissions as $illegal_permission) |
|
104 | { |
||
105 | 4 | if (!allowedTo($illegal_permission)) |
|
106 | { |
||
107 | 4 | $illegal_permissions[] = $illegal_permission; |
|
108 | } |
||
109 | } |
||
110 | 4 | $context['illegal_permissions'] = $illegal_permissions; |
|
111 | 4 | call_integration_hook('integrate_load_illegal_permissions'); |
|
112 | |||
113 | 4 | $this->illegal_permissions = $illegal_permissions; |
|
114 | 4 | } |
|
115 | |||
116 | /** |
||
117 | * Loads those permissions guests cannot have, into context. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 4 | public function loadIllegalGuest() |
|
130 | |||
131 | /** |
||
132 | * Deletes permissions. |
||
133 | * |
||
134 | * @param string[] $permissions |
||
135 | * @param string[] $where |
||
136 | * @param mixed[] $where_parameters = array() or values used in the where statement |
||
137 | */ |
||
138 | 2 | public function deletePermissions($permissions, $where = array(), $where_parameters = array()) |
|
154 | |||
155 | /** |
||
156 | * This function updates the permissions of any groups based on the given groups. |
||
157 | * |
||
158 | * @param mixed[]|int $parents (array or int) group or groups whose children are to be updated |
||
159 | * @param int|null $profile = null an int or null for the customized profile, if any |
||
160 | */ |
||
161 | 2 | public function updateChild($parents, $profile = null) |
|
286 | } |
||
287 |