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 | * Defaults to publishing the role, but you can create a draft instead by setting the POST parameter publish=false |
||
78 | * |
||
79 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
80 | */ |
||
81 | public function createRole(Request $request) |
||
127 | |||
128 | /** |
||
129 | * Creates a new RoleDraft for an existing Role. |
||
130 | * |
||
131 | * @since 6.2 |
||
132 | * |
||
133 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the Role already has a Role Draft that will need to be removed first, |
||
134 | * or if the authenticated user is not allowed to create a role |
||
135 | * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException if a policy limitation in the $roleCreateStruct is not valid |
||
136 | * |
||
137 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
138 | */ |
||
139 | public function createRoleDraft($roleId, Request $request) |
||
157 | |||
158 | /** |
||
159 | * Loads list of roles. |
||
160 | * |
||
161 | * @return \eZ\Publish\Core\REST\Server\Values\RoleList |
||
162 | */ |
||
163 | public function listRoles(Request $request) |
||
186 | |||
187 | /** |
||
188 | * Loads role. |
||
189 | * |
||
190 | * @param $roleId |
||
191 | * |
||
192 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
193 | */ |
||
194 | public function loadRole($roleId) |
||
198 | |||
199 | /** |
||
200 | * Loads a role draft. |
||
201 | * |
||
202 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
203 | * |
||
204 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
205 | */ |
||
206 | public function loadRoleDraft($roleId) |
||
217 | |||
218 | /** |
||
219 | * Updates a role. |
||
220 | * |
||
221 | * @param $roleId |
||
222 | * |
||
223 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
224 | */ |
||
225 | public function updateRole($roleId, Request $request) |
||
239 | |||
240 | /** |
||
241 | * Updates a role draft. |
||
242 | * |
||
243 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
244 | * |
||
245 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
246 | */ |
||
247 | public function updateRoleDraft($roleId, Request $request) |
||
267 | |||
268 | /** |
||
269 | * Publishes a role draft. |
||
270 | * |
||
271 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
272 | * @return Values\RestRole |
||
273 | */ |
||
274 | 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) |
||
347 | { |
||
348 | $loadedRole = $this->roleService->loadRole($roleId); |
||
349 | |||
350 | foreach ($loadedRole->getPolicies() as $policy) { |
||
351 | $this->roleService->deletePolicy($policy); |
||
352 | } |
||
353 | |||
354 | return new Values\NoContent(); |
||
355 | } |
||
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) |
||
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.