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 |
||
32 | class Role extends RestController |
||
33 | { |
||
34 | /** |
||
35 | * Role service. |
||
36 | * |
||
37 | * @var \eZ\Publish\API\Repository\RoleService |
||
38 | */ |
||
39 | protected $roleService; |
||
40 | |||
41 | /** |
||
42 | * User service. |
||
43 | * |
||
44 | * @var \eZ\Publish\API\Repository\UserService |
||
45 | */ |
||
46 | protected $userService; |
||
47 | |||
48 | /** |
||
49 | * Location service. |
||
50 | * |
||
51 | * @var \eZ\Publish\API\Repository\LocationService |
||
52 | */ |
||
53 | protected $locationService; |
||
54 | |||
55 | /** |
||
56 | * Construct controller. |
||
57 | * |
||
58 | * @param \eZ\Publish\API\Repository\RoleService $roleService |
||
59 | * @param \eZ\Publish\API\Repository\UserService $userService |
||
60 | * @param \eZ\Publish\API\Repository\LocationService $locationService |
||
61 | */ |
||
62 | public function __construct( |
||
71 | |||
72 | /** |
||
73 | * Create new role. |
||
74 | * |
||
75 | * Defaults to publishing the role, but you can create a draft instead by setting the POST parameter publish=false |
||
76 | * |
||
77 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
78 | */ |
||
79 | public function createRole(Request $request) |
||
125 | |||
126 | /** |
||
127 | * Creates a new RoleDraft for an existing Role. |
||
128 | * |
||
129 | * @since 6.2 |
||
130 | * |
||
131 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the Role already has a Role Draft that will need to be removed first, |
||
132 | * or if the authenticated user is not allowed to create a role |
||
133 | * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException if a policy limitation in the $roleCreateStruct is not valid |
||
134 | * |
||
135 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
136 | */ |
||
137 | public function createRoleDraft($roleId, Request $request) |
||
155 | |||
156 | /** |
||
157 | * Loads list of roles. |
||
158 | * |
||
159 | * @return \eZ\Publish\Core\REST\Server\Values\RoleList |
||
160 | */ |
||
161 | public function listRoles(Request $request) |
||
184 | |||
185 | /** |
||
186 | * Loads role. |
||
187 | * |
||
188 | * @param $roleId |
||
189 | * |
||
190 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
191 | */ |
||
192 | public function loadRole($roleId) |
||
196 | |||
197 | /** |
||
198 | * Loads a role draft. |
||
199 | * |
||
200 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
201 | * |
||
202 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
203 | */ |
||
204 | public function loadRoleDraft($roleId) |
||
215 | |||
216 | /** |
||
217 | * Updates a role. |
||
218 | * |
||
219 | * @param $roleId |
||
220 | * |
||
221 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
222 | */ |
||
223 | public function updateRole($roleId, Request $request) |
||
237 | |||
238 | /** |
||
239 | * Updates a role draft. |
||
240 | * |
||
241 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
242 | * |
||
243 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
244 | */ |
||
245 | public function updateRoleDraft($roleId, Request $request) |
||
265 | |||
266 | /** |
||
267 | * Publishes a role draft. |
||
268 | * |
||
269 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
270 | * |
||
271 | * @return \eZ\Publish\Core\REST\Server\Values\PublishedRole |
||
272 | */ |
||
273 | public function publishRoleDraft($roleId) |
||
290 | |||
291 | /** |
||
292 | * Delete a role by ID. |
||
293 | * |
||
294 | * @param $roleId |
||
295 | * |
||
296 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
297 | */ |
||
298 | public function deleteRole($roleId) |
||
306 | |||
307 | /** |
||
308 | * Delete a role draft by ID. |
||
309 | * |
||
310 | * @since 6.2 |
||
311 | * |
||
312 | * @param $roleId |
||
313 | * |
||
314 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
315 | */ |
||
316 | public function deleteRoleDraft($roleId) |
||
324 | |||
325 | /** |
||
326 | * Loads the policies for the role. |
||
327 | * |
||
328 | * @param $roleId |
||
329 | * |
||
330 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
331 | */ |
||
332 | public function loadPolicies($roleId, Request $request) |
||
338 | |||
339 | /** |
||
340 | * Deletes all policies from a role. |
||
341 | * |
||
342 | * @param $roleId |
||
343 | * |
||
344 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
345 | */ |
||
346 | public function deletePolicies($roleId) |
||
356 | |||
357 | /** |
||
358 | * Loads a policy. |
||
359 | * |
||
360 | * @param $roleId |
||
361 | * @param $policyId |
||
362 | * |
||
363 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
364 | * |
||
365 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
366 | */ |
||
367 | public function loadPolicy($roleId, $policyId, Request $request) |
||
378 | |||
379 | /** |
||
380 | * Adds a policy to role. |
||
381 | * |
||
382 | * @param $roleId int ID of a role or a role draft |
||
383 | * |
||
384 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
385 | */ |
||
386 | public function addPolicy($roleId, Request $request) |
||
417 | |||
418 | /** |
||
419 | * Adds a policy to a role draft. |
||
420 | * |
||
421 | * @since 6.2 |
||
422 | * @deprecated since 6.3, use {@see addPolicy} |
||
423 | * |
||
424 | * @param $roleId |
||
425 | * |
||
426 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
427 | */ |
||
428 | public function addPolicyByRoleDraft($roleId, Request $request) |
||
429 | { |
||
430 | $createStruct = $this->inputDispatcher->parse( |
||
431 | new Message( |
||
432 | ['Content-Type' => $request->headers->get('Content-Type')], |
||
433 | $request->getContent() |
||
434 | ) |
||
435 | ); |
||
436 | |||
437 | try { |
||
438 | $role = $this->roleService->addPolicyByRoleDraft( |
||
439 | $this->roleService->loadRoleDraft($roleId), |
||
440 | $createStruct |
||
441 | ); |
||
442 | } catch (LimitationValidationException $e) { |
||
443 | throw new BadRequestException($e->getMessage()); |
||
444 | } |
||
445 | |||
446 | return new Values\CreatedPolicy( |
||
447 | [ |
||
448 | 'policy' => $this->getLastAddedPolicy($role), |
||
449 | ] |
||
450 | ); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Get the last added policy for $role. |
||
455 | * |
||
456 | * This is needed because the RoleService addPolicy() and addPolicyByRoleDraft() methods return the role, |
||
457 | * not the policy. |
||
458 | * |
||
459 | * @param $role \eZ\Publish\API\Repository\Values\User\Role |
||
460 | * |
||
461 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
462 | */ |
||
463 | private function getLastAddedPolicy($role) |
||
476 | |||
477 | /** |
||
478 | * Updates a policy. |
||
479 | * |
||
480 | * @param $roleId int ID of a role or a role draft |
||
481 | * @param $policyId int ID of a policy |
||
482 | * |
||
483 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
484 | * |
||
485 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
486 | */ |
||
487 | public function updatePolicy($roleId, $policyId, Request $request) |
||
530 | |||
531 | /** |
||
532 | * Updates a policy. |
||
533 | * |
||
534 | * @since 6.2 |
||
535 | * @deprecated since 6.3, use {@see updatePolicy} |
||
536 | * |
||
537 | * @param $roleId |
||
538 | * @param $policyId |
||
539 | * |
||
540 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
541 | * |
||
542 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
543 | */ |
||
544 | public function updatePolicyByRoleDraft($roleId, $policyId, Request $request) |
||
569 | |||
570 | /** |
||
571 | * Delete a policy from role. |
||
572 | * |
||
573 | * @param $roleId int ID of a role or a role draft |
||
574 | * @param $policyId int ID of a policy |
||
575 | * |
||
576 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
577 | * |
||
578 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
579 | */ |
||
580 | public function deletePolicy($roleId, $policyId, Request $request) |
||
620 | |||
621 | /** |
||
622 | * Remove a policy from a role draft. |
||
623 | * |
||
624 | * @since 6.2 |
||
625 | * @deprecated since 6.3, use {@see deletePolicy} |
||
626 | * |
||
627 | * @param $roleId |
||
628 | * @param $policyId |
||
629 | * |
||
630 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
631 | * |
||
632 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
633 | */ |
||
634 | View Code Duplication | public function removePolicyByRoleDraft($roleId, $policyId, Request $request) |
|
654 | |||
655 | /** |
||
656 | * Assigns role to user. |
||
657 | * |
||
658 | * @param $userId |
||
659 | * |
||
660 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
661 | */ |
||
662 | public function assignRoleToUser($userId, Request $request) |
||
684 | |||
685 | /** |
||
686 | * Assigns role to user group. |
||
687 | * |
||
688 | * @param $groupPath |
||
689 | * |
||
690 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
691 | */ |
||
692 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
717 | |||
718 | /** |
||
719 | * Un-assigns role from user. |
||
720 | * |
||
721 | * @param $userId |
||
722 | * @param $roleId |
||
723 | * |
||
724 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
725 | */ |
||
726 | public function unassignRoleFromUser($userId, $roleId) |
||
737 | |||
738 | /** |
||
739 | * Un-assigns role from user group. |
||
740 | * |
||
741 | * @param $groupPath |
||
742 | * @param $roleId |
||
743 | * |
||
744 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
745 | */ |
||
746 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
759 | |||
760 | /** |
||
761 | * Loads role assignments for user. |
||
762 | * |
||
763 | * @param $userId |
||
764 | * |
||
765 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
766 | */ |
||
767 | public function loadRoleAssignmentsForUser($userId) |
||
775 | |||
776 | /** |
||
777 | * Loads role assignments for user group. |
||
778 | * |
||
779 | * @param $groupPath |
||
780 | * |
||
781 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
782 | */ |
||
783 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
793 | |||
794 | /** |
||
795 | * Returns a role assignment to the given user. |
||
796 | * |
||
797 | * @param $userId |
||
798 | * @param $roleId |
||
799 | * |
||
800 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
801 | * |
||
802 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
803 | */ |
||
804 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
817 | |||
818 | /** |
||
819 | * Returns a role assignment to the given user group. |
||
820 | * |
||
821 | * @param $groupPath |
||
822 | * @param $roleId |
||
823 | * |
||
824 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
825 | * |
||
826 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
827 | */ |
||
828 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
843 | |||
844 | /** |
||
845 | * Search all policies which are applied to a given user. |
||
846 | * |
||
847 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
848 | */ |
||
849 | public function listPoliciesForUser(Request $request) |
||
858 | |||
859 | /** |
||
860 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
861 | * |
||
862 | * Needed since both structs are encoded into the same media type on input. |
||
863 | * |
||
864 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
865 | * |
||
866 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
867 | */ |
||
868 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
876 | } |
||
877 |
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.