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.1 |
||
| 132 | * |
||
| 133 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
| 134 | */ |
||
| 135 | public function createRoleDraft($roleId, Request $request) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Loads list of roles. |
||
| 156 | * |
||
| 157 | * @return \eZ\Publish\Core\REST\Server\Values\RoleList |
||
| 158 | */ |
||
| 159 | public function listRoles(Request $request) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Loads role. |
||
| 185 | * |
||
| 186 | * @param $roleId |
||
| 187 | * |
||
| 188 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 189 | */ |
||
| 190 | public function loadRole($roleId) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Loads a role draft. |
||
| 197 | * |
||
| 198 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
| 199 | * |
||
| 200 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
| 201 | */ |
||
| 202 | public function loadRoleDraft($roleId) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Updates a role. |
||
| 216 | * |
||
| 217 | * @param $roleId |
||
| 218 | * |
||
| 219 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 220 | */ |
||
| 221 | public function updateRole($roleId, Request $request) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Updates a role draft. |
||
| 238 | * |
||
| 239 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
| 240 | * |
||
| 241 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
| 242 | */ |
||
| 243 | public function updateRoleDraft($roleId, Request $request) |
||
| 244 | { |
||
| 245 | $createStruct = $this->inputDispatcher->parse( |
||
| 246 | new Message( |
||
| 247 | array('Content-Type' => $request->headers->get('Content-Type')), |
||
| 248 | $request->getContent() |
||
| 249 | ) |
||
| 250 | ); |
||
| 251 | |||
| 252 | try { |
||
| 253 | // First try to load the draft for given role. |
||
| 254 | $roleDraft = $this->roleService->loadRoleDraftByRoleId($roleId); |
||
| 255 | } catch (NotFoundException $e) { |
||
| 256 | // We might want a newly created role, so try to load it by its ID. |
||
| 257 | // loadRoleDraft() might throw a NotFoundException (wrong $roleId). If so, let it bubble up. |
||
| 258 | $roleDraft = $this->roleService->loadRoleDraft($roleId); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $this->roleService->updateRoleDraft($roleDraft, $this->mapToUpdateStruct($createStruct)); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Publishes a role draft. |
||
| 266 | * |
||
| 267 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
| 268 | * @return Values\RestRole |
||
| 269 | */ |
||
| 270 | public function publishRoleDraft($roleId) |
||
| 271 | { |
||
| 272 | try { |
||
| 273 | // First try to load the draft for given role. |
||
| 274 | $roleDraft = $this->roleService->loadRoleDraftByRoleId($roleId); |
||
| 275 | } catch (NotFoundException $e) { |
||
| 276 | // We might want a newly created role, so try to load it by its ID. |
||
| 277 | // loadRoleDraft() might throw a NotFoundException (wrong $roleId). If so, let it bubble up. |
||
| 278 | $roleDraft = $this->roleService->loadRoleDraft($roleId); |
||
| 279 | } |
||
| 280 | |||
| 281 | $this->roleService->publishRoleDraft($roleDraft); |
||
| 282 | $publishedRole = $this->roleService->loadRole($roleDraft->id); |
||
| 283 | |||
| 284 | return new Values\CreatedRole(['role' => new Values\RestRole($publishedRole)]); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Delete a role by ID. |
||
| 289 | * |
||
| 290 | * @param $roleId |
||
| 291 | * |
||
| 292 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 293 | */ |
||
| 294 | public function deleteRole($roleId) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Delete a role draft by ID. |
||
| 305 | * |
||
| 306 | * @param $roleId |
||
| 307 | * |
||
| 308 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 309 | */ |
||
| 310 | public function deleteRoleDraft($roleId) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Loads the policies for the role. |
||
| 321 | * |
||
| 322 | * @param $roleId |
||
| 323 | * |
||
| 324 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 325 | */ |
||
| 326 | public function loadPolicies($roleId, Request $request) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Deletes all policies from a role. |
||
| 335 | * |
||
| 336 | * @param $roleId |
||
| 337 | * |
||
| 338 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 339 | */ |
||
| 340 | public function deletePolicies($roleId) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Loads a policy. |
||
| 353 | * |
||
| 354 | * @param $roleId |
||
| 355 | * @param $policyId |
||
| 356 | * |
||
| 357 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 358 | * |
||
| 359 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 360 | */ |
||
| 361 | public function loadPolicy($roleId, $policyId, Request $request) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Adds a policy to role. |
||
| 375 | * |
||
| 376 | * @param $roleId |
||
| 377 | * |
||
| 378 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function addPolicy($roleId, Request $request) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Adds a policy to a role draft. |
||
| 416 | * |
||
| 417 | * @param $roleId |
||
| 418 | * |
||
| 419 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 420 | */ |
||
| 421 | View Code Duplication | public function addPolicyByRoleDraft($roleId, Request $request) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Updates a policy. |
||
| 457 | * |
||
| 458 | * @param $roleId |
||
| 459 | * @param $policyId |
||
| 460 | * |
||
| 461 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 462 | * |
||
| 463 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 464 | */ |
||
| 465 | View Code Duplication | public function updatePolicy($roleId, $policyId, Request $request) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Updates a policy. |
||
| 493 | * |
||
| 494 | * @param $roleId |
||
| 495 | * @param $policyId |
||
| 496 | * |
||
| 497 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 498 | * |
||
| 499 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function updatePolicyByRoleDraft($roleId, $policyId, Request $request) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Delete a policy from role. |
||
| 529 | * |
||
| 530 | * @param $roleId |
||
| 531 | * @param $policyId |
||
| 532 | * |
||
| 533 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 534 | * |
||
| 535 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 536 | */ |
||
| 537 | View Code Duplication | public function deletePolicy($roleId, $policyId, Request $request) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Remove a policy from a role draft. |
||
| 560 | * |
||
| 561 | * @param $roleId |
||
| 562 | * @param $policyId |
||
| 563 | * |
||
| 564 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 565 | * |
||
| 566 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 567 | */ |
||
| 568 | View Code Duplication | public function removePolicyByRoleDraft($roleId, $policyId, Request $request) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Assigns role to user. |
||
| 591 | * |
||
| 592 | * @param $userId |
||
| 593 | * |
||
| 594 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 595 | */ |
||
| 596 | public function assignRoleToUser($userId, Request $request) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Assigns role to user group. |
||
| 621 | * |
||
| 622 | * @param $groupPath |
||
| 623 | * |
||
| 624 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 625 | */ |
||
| 626 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Un-assigns role from user. |
||
| 654 | * |
||
| 655 | * @param $userId |
||
| 656 | * @param $roleId |
||
| 657 | * |
||
| 658 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 659 | */ |
||
| 660 | public function unassignRoleFromUser($userId, $roleId) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Un-assigns role from user group. |
||
| 674 | * |
||
| 675 | * @param $groupPath |
||
| 676 | * @param $roleId |
||
| 677 | * |
||
| 678 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 679 | */ |
||
| 680 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Loads role assignments for user. |
||
| 696 | * |
||
| 697 | * @param $userId |
||
| 698 | * |
||
| 699 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 700 | */ |
||
| 701 | public function loadRoleAssignmentsForUser($userId) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Loads role assignments for user group. |
||
| 712 | * |
||
| 713 | * @param $groupPath |
||
| 714 | * |
||
| 715 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 716 | */ |
||
| 717 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Returns a role assignment to the given user. |
||
| 730 | * |
||
| 731 | * @param $userId |
||
| 732 | * @param $roleId |
||
| 733 | * |
||
| 734 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 735 | * |
||
| 736 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
| 737 | */ |
||
| 738 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Returns a role assignment to the given user group. |
||
| 754 | * |
||
| 755 | * @param $groupPath |
||
| 756 | * @param $roleId |
||
| 757 | * |
||
| 758 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 759 | * |
||
| 760 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
| 761 | */ |
||
| 762 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Search all policies which are applied to a given user. |
||
| 780 | * |
||
| 781 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 782 | */ |
||
| 783 | public function listPoliciesForUser(Request $request) |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
| 795 | * |
||
| 796 | * Needed since both structs are encoded into the same media type on input. |
||
| 797 | * |
||
| 798 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
| 799 | * |
||
| 800 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
| 801 | */ |
||
| 802 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
| 810 | } |
||
| 811 |
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.