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( |
||
| 65 | RoleService $roleService, |
||
| 66 | UserService $userService, |
||
| 67 | LocationService $locationService |
||
| 68 | ) { |
||
| 69 | $this->roleService = $roleService; |
||
| 70 | $this->userService = $userService; |
||
| 71 | $this->locationService = $locationService; |
||
| 72 | } |
||
| 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) |
||
| 164 | { |
||
| 165 | $roles = array(); |
||
| 166 | if ($request->query->has('identifier')) { |
||
| 167 | try { |
||
| 168 | $role = $this->roleService->loadRoleByIdentifier($request->query->get('identifier')); |
||
| 169 | $roles[] = $role; |
||
| 170 | } catch (APINotFoundException $e) { |
||
| 171 | // Do nothing |
||
| 172 | } |
||
| 173 | } else { |
||
| 174 | $offset = $request->query->has('offset') ? (int)$request->query->get('offset') : 0; |
||
| 175 | $limit = $request->query->has('limit') ? (int)$request->query->get('limit') : -1; |
||
| 176 | |||
| 177 | $roles = array_slice( |
||
| 178 | $this->roleService->loadRoles(), |
||
| 179 | $offset >= 0 ? $offset : 0, |
||
| 180 | $limit >= 0 ? $limit : null |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | |||
| 184 | return new Values\RoleList($roles, $request->getPathInfo()); |
||
| 185 | } |
||
| 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) |
||
| 195 | { |
||
| 196 | return $this->roleService->loadRole($roleId); |
||
| 197 | } |
||
| 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) |
||
| 207 | { |
||
| 208 | try { |
||
| 209 | // First try to load the draft for given role. |
||
| 210 | return $this->roleService->loadRoleDraftByRoleId($roleId); |
||
| 211 | } catch (NotFoundException $e) { |
||
| 212 | // We might want a newly created role, so try to load it by its ID. |
||
| 213 | // loadRoleDraft() might throw a NotFoundException (wrong $roleId). If so, let it bubble up. |
||
| 214 | return $this->roleService->loadRoleDraft($roleId); |
||
| 215 | } |
||
| 216 | } |
||
| 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) |
||
| 226 | { |
||
| 227 | $createStruct = $this->inputDispatcher->parse( |
||
| 228 | new Message( |
||
| 229 | array('Content-Type' => $request->headers->get('Content-Type')), |
||
| 230 | $request->getContent() |
||
| 231 | ) |
||
| 232 | ); |
||
| 233 | |||
| 234 | return $this->roleService->updateRole( |
||
| 235 | $this->roleService->loadRole($roleId), |
||
| 236 | $this->mapToUpdateStruct($createStruct) |
||
| 237 | ); |
||
| 238 | } |
||
| 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) |
||
| 299 | { |
||
| 300 | $this->roleService->deleteRole( |
||
| 301 | $this->roleService->loadRole($roleId) |
||
| 302 | ); |
||
| 303 | |||
| 304 | return new Values\NoContent(); |
||
| 305 | } |
||
| 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) |
||
| 333 | { |
||
| 334 | $loadedRole = $this->roleService->loadRole($roleId); |
||
| 335 | |||
| 336 | return new Values\PolicyList($loadedRole->getPolicies(), $request->getPathInfo()); |
||
| 337 | } |
||
| 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) |
||
| 368 | { |
||
| 369 | $loadedRole = $this->roleService->loadRole($roleId); |
||
| 370 | foreach ($loadedRole->getPolicies() as $policy) { |
||
| 371 | if ($policy->id == $policyId) { |
||
| 372 | return $policy; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | throw new Exceptions\NotFoundException("Policy not found: '{$request->getPathInfo()}'."); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Adds a policy to role. |
||
| 381 | * |
||
| 382 | * @param $roleId |
||
| 383 | * |
||
| 384 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 385 | */ |
||
| 386 | public function addPolicy($roleId, Request $request) |
||
| 387 | { |
||
| 388 | $createStruct = $this->inputDispatcher->parse( |
||
| 389 | new Message( |
||
| 390 | array('Content-Type' => $request->headers->get('Content-Type')), |
||
| 391 | $request->getContent() |
||
| 392 | ) |
||
| 393 | ); |
||
| 394 | |||
| 395 | try { |
||
| 396 | $role = $this->roleService->addPolicy( |
||
| 397 | $this->roleService->loadRole($roleId), |
||
| 398 | $createStruct |
||
| 399 | ); |
||
| 400 | } catch (LimitationValidationException $e) { |
||
| 401 | throw new BadRequestException($e->getMessage()); |
||
| 402 | } |
||
| 403 | |||
| 404 | return new Values\CreatedPolicy( |
||
| 405 | array( |
||
| 406 | 'policy' => $this->getLastAddedPolicy($role), |
||
| 407 | ) |
||
| 408 | ); |
||
| 409 | } |
||
| 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 | public function addPolicyByRoleDraft($roleId, Request $request) |
||
| 421 | { |
||
| 422 | $createStruct = $this->inputDispatcher->parse( |
||
| 423 | new Message( |
||
| 424 | array('Content-Type' => $request->headers->get('Content-Type')), |
||
| 425 | $request->getContent() |
||
| 426 | ) |
||
| 427 | ); |
||
| 428 | |||
| 429 | try { |
||
| 430 | $role = $this->roleService->addPolicyByRoleDraft( |
||
| 431 | $this->roleService->loadRoleDraft($roleId), |
||
| 432 | $createStruct |
||
| 433 | ); |
||
| 434 | } catch (LimitationValidationException $e) { |
||
| 435 | throw new BadRequestException($e->getMessage()); |
||
| 436 | } |
||
| 437 | |||
| 438 | return new Values\CreatedPolicy( |
||
| 439 | array( |
||
| 440 | 'policy' => $this->getLastAddedPolicy($role), |
||
| 441 | ) |
||
| 442 | ); |
||
| 443 | } |
||
| 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) |
|
| 480 | { |
||
| 481 | $updateStruct = $this->inputDispatcher->parse( |
||
| 482 | new Message( |
||
| 483 | array('Content-Type' => $request->headers->get('Content-Type')), |
||
| 484 | $request->getContent() |
||
| 485 | ) |
||
| 486 | ); |
||
| 487 | |||
| 488 | $role = $this->roleService->loadRole($roleId); |
||
| 489 | foreach ($role->getPolicies() as $policy) { |
||
| 490 | if ($policy->id == $policyId) { |
||
| 491 | try { |
||
| 492 | return $this->roleService->updatePolicy( |
||
| 493 | $policy, |
||
| 494 | $updateStruct |
||
| 495 | ); |
||
| 496 | } catch (LimitationValidationException $e) { |
||
| 497 | throw new BadRequestException($e->getMessage()); |
||
| 498 | } |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | throw new Exceptions\NotFoundException("Policy not found: '{$request->getPathInfo()}'."); |
||
| 503 | } |
||
| 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) |
|
| 554 | { |
||
| 555 | $role = $this->roleService->loadRole($roleId); |
||
| 556 | |||
| 557 | $policy = null; |
||
| 558 | foreach ($role->getPolicies() as $rolePolicy) { |
||
| 559 | if ($rolePolicy->id == $policyId) { |
||
| 560 | $policy = $rolePolicy; |
||
| 561 | break; |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | if ($policy !== null) { |
||
| 566 | $this->roleService->deletePolicy($policy); |
||
| 567 | |||
| 568 | return new Values\NoContent(); |
||
| 569 | } |
||
| 570 | |||
| 571 | throw new Exceptions\NotFoundException("Policy not found: '{$request->getPathInfo()}'."); |
||
| 572 | } |
||
| 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) |
||
| 615 | { |
||
| 616 | $roleAssignment = $this->inputDispatcher->parse( |
||
| 617 | new Message( |
||
| 618 | array('Content-Type' => $request->headers->get('Content-Type')), |
||
| 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.