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 Role 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 Role, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Role extends RestController |
||
35 | { |
||
36 | /** |
||
37 | * Role service. |
||
38 | * |
||
39 | * @var \eZ\Publish\API\Repository\RoleService |
||
40 | */ |
||
41 | protected $roleService; |
||
42 | |||
43 | /** |
||
44 | * User service. |
||
45 | * |
||
46 | * @var \eZ\Publish\API\Repository\UserService |
||
47 | */ |
||
48 | protected $userService; |
||
49 | |||
50 | /** |
||
51 | * Location service. |
||
52 | * |
||
53 | * @var \eZ\Publish\API\Repository\LocationService |
||
54 | */ |
||
55 | protected $locationService; |
||
56 | |||
57 | /** |
||
58 | * Construct controller. |
||
59 | * |
||
60 | * @param \eZ\Publish\API\Repository\RoleService $roleService |
||
61 | * @param \eZ\Publish\API\Repository\UserService $userService |
||
62 | * @param \eZ\Publish\API\Repository\LocationService $locationService |
||
63 | */ |
||
64 | public function __construct( |
||
73 | |||
74 | /** |
||
75 | * Create new role. |
||
76 | * |
||
77 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
78 | */ |
||
79 | public function createRole(Request $request) |
||
116 | |||
117 | /** |
||
118 | * Creates a new RoleDraft for an existing Role. |
||
119 | * |
||
120 | * @since 6.1 |
||
121 | * |
||
122 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRoleDraft |
||
123 | */ |
||
124 | public function createRoleDraft(Request $request) |
||
128 | |||
129 | /** |
||
130 | * Loads list of roles. |
||
131 | * |
||
132 | * @return \eZ\Publish\Core\REST\Server\Values\RoleList |
||
133 | */ |
||
134 | public function listRoles(Request $request) |
||
157 | |||
158 | /** |
||
159 | * Loads role. |
||
160 | * |
||
161 | * @param $roleId |
||
162 | * |
||
163 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
164 | */ |
||
165 | public function loadRole($roleId) |
||
169 | |||
170 | /** |
||
171 | * Loads a role draft. |
||
172 | * |
||
173 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
174 | * |
||
175 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
176 | */ |
||
177 | public function loadRoleDraft($roleId) |
||
188 | |||
189 | /** |
||
190 | * Updates a role. |
||
191 | * |
||
192 | * @param $roleId |
||
193 | * |
||
194 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
195 | */ |
||
196 | public function updateRole($roleId, Request $request) |
||
210 | |||
211 | /** |
||
212 | * Updates a role draft. |
||
213 | * |
||
214 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
215 | * |
||
216 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
217 | */ |
||
218 | public function updateRoleDraft($roleId, Request $request) |
||
238 | |||
239 | /** |
||
240 | * Publishes a role draft. |
||
241 | * |
||
242 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
243 | * @return Values\RestRole |
||
244 | */ |
||
245 | public function publishRoleDraft($roleId) |
||
261 | |||
262 | /** |
||
263 | * Delete a role by ID. |
||
264 | * |
||
265 | * @param $roleId |
||
266 | * |
||
267 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
268 | */ |
||
269 | public function deleteRole($roleId) |
||
277 | |||
278 | /** |
||
279 | * Loads the policies for the role. |
||
280 | * |
||
281 | * @param $roleId |
||
282 | * |
||
283 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
284 | */ |
||
285 | public function loadPolicies($roleId, Request $request) |
||
291 | |||
292 | /** |
||
293 | * Deletes all policies from a role. |
||
294 | * |
||
295 | * @param $roleId |
||
296 | * |
||
297 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
298 | */ |
||
299 | public function deletePolicies($roleId) |
||
309 | |||
310 | /** |
||
311 | * Loads a policy. |
||
312 | * |
||
313 | * @param $roleId |
||
314 | * @param $policyId |
||
315 | * |
||
316 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
317 | * |
||
318 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
319 | */ |
||
320 | public function loadPolicy($roleId, $policyId, Request $request) |
||
331 | |||
332 | /** |
||
333 | * Adds a policy to role. |
||
334 | * |
||
335 | * @param $roleId |
||
336 | * |
||
337 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
338 | */ |
||
339 | public function addPolicy($roleId, Request $request) |
||
372 | |||
373 | /** |
||
374 | * Updates a policy. |
||
375 | * |
||
376 | * @param $roleId |
||
377 | * @param $policyId |
||
378 | * |
||
379 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
380 | * |
||
381 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
382 | */ |
||
383 | public function updatePolicy($roleId, $policyId, Request $request) |
||
408 | |||
409 | /** |
||
410 | * Delete a policy from role. |
||
411 | * |
||
412 | * @param $roleId |
||
413 | * @param $policyId |
||
414 | * |
||
415 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
416 | * |
||
417 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
418 | */ |
||
419 | View Code Duplication | public function deletePolicy($roleId, $policyId, Request $request) |
|
439 | |||
440 | /** |
||
441 | * Assigns role to user. |
||
442 | * |
||
443 | * @param $userId |
||
444 | * |
||
445 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
446 | */ |
||
447 | public function assignRoleToUser($userId, Request $request) |
||
469 | |||
470 | /** |
||
471 | * Assigns role to user group. |
||
472 | * |
||
473 | * @param $groupPath |
||
474 | * |
||
475 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
476 | */ |
||
477 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
502 | |||
503 | /** |
||
504 | * Un-assigns role from user. |
||
505 | * |
||
506 | * @param $userId |
||
507 | * @param $roleId |
||
508 | * |
||
509 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
510 | */ |
||
511 | public function unassignRoleFromUser($userId, $roleId) |
||
522 | |||
523 | /** |
||
524 | * Un-assigns role from user group. |
||
525 | * |
||
526 | * @param $groupPath |
||
527 | * @param $roleId |
||
528 | * |
||
529 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
530 | */ |
||
531 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
544 | |||
545 | /** |
||
546 | * Loads role assignments for user. |
||
547 | * |
||
548 | * @param $userId |
||
549 | * |
||
550 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
551 | */ |
||
552 | public function loadRoleAssignmentsForUser($userId) |
||
560 | |||
561 | /** |
||
562 | * Loads role assignments for user group. |
||
563 | * |
||
564 | * @param $groupPath |
||
565 | * |
||
566 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
567 | */ |
||
568 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
578 | |||
579 | /** |
||
580 | * Returns a role assignment to the given user. |
||
581 | * |
||
582 | * @param $userId |
||
583 | * @param $roleId |
||
584 | * |
||
585 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
586 | * |
||
587 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
588 | */ |
||
589 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
602 | |||
603 | /** |
||
604 | * Returns a role assignment to the given user group. |
||
605 | * |
||
606 | * @param $groupPath |
||
607 | * @param $roleId |
||
608 | * |
||
609 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
610 | * |
||
611 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
612 | */ |
||
613 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
628 | |||
629 | /** |
||
630 | * Search all policies which are applied to a given user. |
||
631 | * |
||
632 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
633 | */ |
||
634 | public function listPoliciesForUser(Request $request) |
||
643 | |||
644 | /** |
||
645 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
646 | * |
||
647 | * Needed since both structs are encoded into the same media type on input. |
||
648 | * |
||
649 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
650 | * |
||
651 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
652 | */ |
||
653 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
661 | } |
||
662 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.