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 |
||
| 383 | * |
||
| 384 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function addPolicy($roleId, Request $request) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Adds a policy to a role draft. |
||
| 413 | * |
||
| 414 | * @since 6.2 |
||
| 415 | * |
||
| 416 | * @param $roleId |
||
| 417 | * |
||
| 418 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 419 | */ |
||
| 420 | View Code Duplication | public function addPolicyByRoleDraft($roleId, Request $request) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Get the last added policy for $role. |
||
| 447 | * |
||
| 448 | * This is needed because the RoleService addPolicy() and addPolicyByRoleDraft() methods return the role, |
||
| 449 | * not the policy. |
||
| 450 | * |
||
| 451 | * @param $role \eZ\Publish\API\Repository\Values\User\Role |
||
| 452 | * |
||
| 453 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 454 | */ |
||
| 455 | private function getLastAddedPolicy($role) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Updates a policy. |
||
| 471 | * |
||
| 472 | * @param $roleId |
||
| 473 | * @param $policyId |
||
| 474 | * |
||
| 475 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 476 | * |
||
| 477 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 478 | */ |
||
| 479 | View Code Duplication | public function updatePolicy($roleId, $policyId, Request $request) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Updates a policy. |
||
| 507 | * |
||
| 508 | * @since 6.2 |
||
| 509 | * |
||
| 510 | * @param $roleId |
||
| 511 | * @param $policyId |
||
| 512 | * |
||
| 513 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 514 | * |
||
| 515 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 516 | */ |
||
| 517 | View Code Duplication | public function updatePolicyByRoleDraft($roleId, $policyId, Request $request) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Delete a policy from role. |
||
| 545 | * |
||
| 546 | * @param $roleId |
||
| 547 | * @param $policyId |
||
| 548 | * |
||
| 549 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 550 | * |
||
| 551 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 552 | */ |
||
| 553 | View Code Duplication | public function deletePolicy($roleId, $policyId, Request $request) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Remove a policy from a role draft. |
||
| 576 | * |
||
| 577 | * @since 6.2 |
||
| 578 | * |
||
| 579 | * @param $roleId |
||
| 580 | * @param $policyId |
||
| 581 | * |
||
| 582 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 583 | * |
||
| 584 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 585 | */ |
||
| 586 | View Code Duplication | public function removePolicyByRoleDraft($roleId, $policyId, Request $request) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Assigns role to user. |
||
| 609 | * |
||
| 610 | * @param $userId |
||
| 611 | * |
||
| 612 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 613 | */ |
||
| 614 | public function assignRoleToUser($userId, Request $request) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Assigns role to user group. |
||
| 639 | * |
||
| 640 | * @param $groupPath |
||
| 641 | * |
||
| 642 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 643 | */ |
||
| 644 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Un-assigns role from user. |
||
| 672 | * |
||
| 673 | * @param $userId |
||
| 674 | * @param $roleId |
||
| 675 | * |
||
| 676 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 677 | */ |
||
| 678 | public function unassignRoleFromUser($userId, $roleId) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Un-assigns role from user group. |
||
| 692 | * |
||
| 693 | * @param $groupPath |
||
| 694 | * @param $roleId |
||
| 695 | * |
||
| 696 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 697 | */ |
||
| 698 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Loads role assignments for user. |
||
| 714 | * |
||
| 715 | * @param $userId |
||
| 716 | * |
||
| 717 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 718 | */ |
||
| 719 | public function loadRoleAssignmentsForUser($userId) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Loads role assignments for user group. |
||
| 730 | * |
||
| 731 | * @param $groupPath |
||
| 732 | * |
||
| 733 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 734 | */ |
||
| 735 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Returns a role assignment to the given user. |
||
| 748 | * |
||
| 749 | * @param $userId |
||
| 750 | * @param $roleId |
||
| 751 | * |
||
| 752 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 753 | * |
||
| 754 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
| 755 | */ |
||
| 756 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Returns a role assignment to the given user group. |
||
| 772 | * |
||
| 773 | * @param $groupPath |
||
| 774 | * @param $roleId |
||
| 775 | * |
||
| 776 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 777 | * |
||
| 778 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
| 779 | */ |
||
| 780 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Search all policies which are applied to a given user. |
||
| 798 | * |
||
| 799 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 800 | */ |
||
| 801 | public function listPoliciesForUser(Request $request) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
| 813 | * |
||
| 814 | * Needed since both structs are encoded into the same media type on input. |
||
| 815 | * |
||
| 816 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
| 817 | * |
||
| 818 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
| 819 | */ |
||
| 820 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
| 828 | } |
||
| 829 |
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.