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 ExceptionConversion 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 ExceptionConversion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ExceptionConversion extends Gateway |
||
23 | { |
||
24 | /** |
||
25 | * The wrapped gateway. |
||
26 | * |
||
27 | * @var Gateway |
||
28 | */ |
||
29 | protected $innerGateway; |
||
30 | |||
31 | /** |
||
32 | * Creates a new exception conversion gateway around $innerGateway. |
||
33 | * |
||
34 | * @param Gateway $innerGateway |
||
35 | */ |
||
36 | public function __construct(Gateway $innerGateway) |
||
40 | |||
41 | /** |
||
42 | * Create new role. |
||
43 | * |
||
44 | * @param Role $role |
||
45 | * |
||
46 | * @return Role |
||
47 | */ |
||
48 | View Code Duplication | public function createRole(Role $role) |
|
58 | |||
59 | /** |
||
60 | * Copy an existing role. |
||
61 | * |
||
62 | * @param Role $role |
||
63 | * |
||
64 | * @return Role |
||
65 | */ |
||
66 | View Code Duplication | public function copyRole(Role $role) |
|
76 | |||
77 | /** |
||
78 | * Loads a specified role by $roleId. |
||
79 | * |
||
80 | * @param mixed $roleId |
||
81 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | View Code Duplication | public function loadRole($roleId, $status = Role::STATUS_DEFINED) |
|
95 | |||
96 | /** |
||
97 | * Loads a specified role by $identifier. |
||
98 | * |
||
99 | * @param string $identifier |
||
100 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | View Code Duplication | public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED) |
|
114 | |||
115 | /** |
||
116 | * Loads a role draft by the original role ID. |
||
117 | * |
||
118 | * @param mixed $roleId ID of the role the draft was created from. |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | View Code Duplication | public function loadRoleDraftByRoleId($roleId) |
|
132 | |||
133 | /** |
||
134 | * Loads all roles. |
||
135 | * |
||
136 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | View Code Duplication | public function loadRoles($status = Role::STATUS_DEFINED) |
|
150 | |||
151 | /** |
||
152 | * Loads all roles associated with the given content objects. |
||
153 | * |
||
154 | * @param array $contentIds |
||
155 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | View Code Duplication | public function loadRolesForContentObjects($contentIds, $status = Role::STATUS_DEFINED) |
|
169 | |||
170 | /** |
||
171 | * Loads role assignment for specified assignment ID. |
||
172 | * |
||
173 | * @param mixed $roleAssignmentId |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | View Code Duplication | public function loadRoleAssignment($roleAssignmentId) |
|
187 | |||
188 | /** |
||
189 | * Loads role assignments for specified content ID. |
||
190 | * |
||
191 | * @param mixed $groupId |
||
192 | * @param bool $inherited |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | View Code Duplication | public function loadRoleAssignmentsByGroupId($groupId, $inherited = false) |
|
206 | |||
207 | /** |
||
208 | * Loads role assignments for given role ID. |
||
209 | * |
||
210 | * @param mixed $roleId |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | View Code Duplication | public function loadRoleAssignmentsByRoleId($roleId) |
|
224 | |||
225 | /** |
||
226 | * Returns the user policies associated with the user. |
||
227 | * |
||
228 | * @param mixed $userId |
||
229 | * |
||
230 | * @return UserPolicy[] |
||
231 | */ |
||
232 | View Code Duplication | public function loadPoliciesByUserId($userId) |
|
242 | |||
243 | /** |
||
244 | * Update role (draft). |
||
245 | * |
||
246 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
247 | * |
||
248 | * @param RoleUpdateStruct $role |
||
249 | */ |
||
250 | View Code Duplication | public function updateRole(RoleUpdateStruct $role) |
|
260 | |||
261 | /** |
||
262 | * Delete the specified role (draft). |
||
263 | * If it's not a draft, the role assignments will also be deleted. |
||
264 | * |
||
265 | * @param mixed $roleId |
||
266 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
267 | */ |
||
268 | View Code Duplication | public function deleteRole($roleId, $status = Role::STATUS_DEFINED) |
|
278 | |||
279 | /** |
||
280 | * Publish the specified role draft. |
||
281 | * If the draft was created from an existing role, published version will take the original role ID. |
||
282 | * |
||
283 | * @param mixed $roleDraftId |
||
284 | * @param mixed|null $originalRoleId ID of role the draft was created from. Will be null if the role draft was completely new. |
||
285 | */ |
||
286 | View Code Duplication | public function publishRoleDraft($roleDraftId, $originalRoleId = null) |
|
296 | |||
297 | /** |
||
298 | * Adds a policy to a role. |
||
299 | * |
||
300 | * @param mixed $roleId |
||
301 | * @param Policy $policy |
||
302 | */ |
||
303 | View Code Duplication | public function addPolicy($roleId, Policy $policy) |
|
313 | |||
314 | /** |
||
315 | * Adds limitations to an existing policy. |
||
316 | * |
||
317 | * @param int $policyId |
||
318 | * @param array $limitations |
||
319 | */ |
||
320 | View Code Duplication | public function addPolicyLimitations($policyId, array $limitations) |
|
330 | |||
331 | /** |
||
332 | * Removes a policy from a role. |
||
333 | * |
||
334 | * @param mixed $policyId |
||
335 | */ |
||
336 | View Code Duplication | public function removePolicy($policyId) |
|
346 | |||
347 | /** |
||
348 | * Removes a policy from a role. |
||
349 | * |
||
350 | * @param mixed $policyId |
||
351 | */ |
||
352 | View Code Duplication | public function removePolicyLimitations($policyId) |
|
362 | } |
||
363 |