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 /** MicroDbACL */ |
||
22 | class DbAcl extends Acl |
||
23 | { |
||
24 | /** |
||
25 | * Constructor DB acl |
||
26 | * |
||
27 | * @access public |
||
28 | * |
||
29 | * @param IConnection $db |
||
30 | * @param array $params config array |
||
31 | * |
||
32 | * @result void |
||
33 | */ |
||
34 | public function __construct(IConnection $db, array $params = []) |
||
64 | |||
65 | /** |
||
66 | * Check user access to permission |
||
67 | * |
||
68 | * @access public |
||
69 | * |
||
70 | * @param integer $userId user id |
||
71 | * @param string $permission checked permission |
||
72 | * @param array $data for compatible, not used! |
||
73 | * |
||
74 | * @return bool |
||
75 | * @throws \Micro\Base\Exception |
||
76 | */ |
||
77 | public function check($userId, $permission, array $data = []) |
||
97 | |||
98 | /** |
||
99 | * Create new role |
||
100 | * |
||
101 | * @access public |
||
102 | * |
||
103 | * @param string $name role name |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function createRole($name) |
||
113 | |||
114 | /** |
||
115 | * Create new permission |
||
116 | * |
||
117 | * @access public |
||
118 | * |
||
119 | * @param string $name permission name |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function createPermission($name) |
||
129 | |||
130 | /** |
||
131 | * Delete permission by name |
||
132 | * |
||
133 | * @access public |
||
134 | * |
||
135 | * @param string $name permission name |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | public function deletePermission($name) |
||
143 | |||
144 | /** |
||
145 | * Delete role by name |
||
146 | * |
||
147 | * @access public |
||
148 | * |
||
149 | * @param string $name role name |
||
150 | * |
||
151 | * @return void |
||
152 | * @throws \Micro\Base\Exception |
||
153 | */ |
||
154 | public function deleteRole($name) |
||
162 | |||
163 | /** |
||
164 | * Get role perms |
||
165 | * |
||
166 | * @access public |
||
167 | * |
||
168 | * @param string $role role name |
||
169 | * |
||
170 | * @return array |
||
171 | * @throws \Micro\Base\Exception |
||
172 | */ |
||
173 | View Code Duplication | protected function rolePerms($role) |
|
183 | |||
184 | /** |
||
185 | * Assign role permission |
||
186 | * |
||
187 | * @access public |
||
188 | * |
||
189 | * @param string $role role name |
||
190 | * @param string $permission permission name |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | public function assignRolePermission($role, $permission) |
||
198 | |||
199 | /** |
||
200 | * Revoke role permission |
||
201 | * |
||
202 | * @access public |
||
203 | * |
||
204 | * @param string $role role name |
||
205 | * @param string $permission permission name |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public function revokeRolePermission($role, $permission) |
||
213 | |||
214 | /** |
||
215 | * Grant privilege to user |
||
216 | * |
||
217 | * @access public |
||
218 | * |
||
219 | * @param integer $userId user ID |
||
220 | * @param integer $privilege privilege ID |
||
221 | * @param boolean $asRole as role? |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | public function grantPrivilege($userId, $privilege = null, $asRole = true) |
||
233 | |||
234 | /** |
||
235 | * Forbid privilege |
||
236 | * |
||
237 | * @access public |
||
238 | * |
||
239 | * @param integer $userId user ID |
||
240 | * @param integer $privilege privilege ID |
||
241 | * @param bool $asRole as role? |
||
242 | */ |
||
243 | public function forbidPrivilege($userId, $privilege = null, $asRole = true) |
||
251 | } |
||
252 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.