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) |
||
| 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) |
||
| 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 | * Loads the policies for the role. |
||
| 305 | * |
||
| 306 | * @param $roleId |
||
| 307 | * |
||
| 308 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 309 | */ |
||
| 310 | public function loadPolicies($roleId, Request $request) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Deletes all policies from a role. |
||
| 319 | * |
||
| 320 | * @param $roleId |
||
| 321 | * |
||
| 322 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 323 | */ |
||
| 324 | public function deletePolicies($roleId) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Loads a policy. |
||
| 337 | * |
||
| 338 | * @param $roleId |
||
| 339 | * @param $policyId |
||
| 340 | * |
||
| 341 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 342 | * |
||
| 343 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 344 | */ |
||
| 345 | public function loadPolicy($roleId, $policyId, Request $request) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Adds a policy to role. |
||
| 359 | * |
||
| 360 | * @param $roleId |
||
| 361 | * |
||
| 362 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 363 | */ |
||
| 364 | public function addPolicy($roleId, Request $request) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Updates a policy. |
||
| 400 | * |
||
| 401 | * @param $roleId |
||
| 402 | * @param $policyId |
||
| 403 | * |
||
| 404 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 405 | * |
||
| 406 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 407 | */ |
||
| 408 | public function updatePolicy($roleId, $policyId, Request $request) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Delete a policy from role. |
||
| 436 | * |
||
| 437 | * @param $roleId |
||
| 438 | * @param $policyId |
||
| 439 | * |
||
| 440 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 441 | * |
||
| 442 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 443 | */ |
||
| 444 | View Code Duplication | public function deletePolicy($roleId, $policyId, Request $request) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Assigns role to user. |
||
| 467 | * |
||
| 468 | * @param $userId |
||
| 469 | * |
||
| 470 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 471 | */ |
||
| 472 | public function assignRoleToUser($userId, Request $request) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Assigns role to user group. |
||
| 497 | * |
||
| 498 | * @param $groupPath |
||
| 499 | * |
||
| 500 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 501 | */ |
||
| 502 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Un-assigns role from user. |
||
| 530 | * |
||
| 531 | * @param $userId |
||
| 532 | * @param $roleId |
||
| 533 | * |
||
| 534 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 535 | */ |
||
| 536 | public function unassignRoleFromUser($userId, $roleId) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Un-assigns role from user group. |
||
| 550 | * |
||
| 551 | * @param $groupPath |
||
| 552 | * @param $roleId |
||
| 553 | * |
||
| 554 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 555 | */ |
||
| 556 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Loads role assignments for user. |
||
| 572 | * |
||
| 573 | * @param $userId |
||
| 574 | * |
||
| 575 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 576 | */ |
||
| 577 | public function loadRoleAssignmentsForUser($userId) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Loads role assignments for user group. |
||
| 588 | * |
||
| 589 | * @param $groupPath |
||
| 590 | * |
||
| 591 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 592 | */ |
||
| 593 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Returns a role assignment to the given user. |
||
| 606 | * |
||
| 607 | * @param $userId |
||
| 608 | * @param $roleId |
||
| 609 | * |
||
| 610 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 611 | * |
||
| 612 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
| 613 | */ |
||
| 614 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Returns a role assignment to the given user group. |
||
| 630 | * |
||
| 631 | * @param $groupPath |
||
| 632 | * @param $roleId |
||
| 633 | * |
||
| 634 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 635 | * |
||
| 636 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
| 637 | */ |
||
| 638 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Search all policies which are applied to a given user. |
||
| 656 | * |
||
| 657 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 658 | */ |
||
| 659 | public function listPoliciesForUser(Request $request) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
| 671 | * |
||
| 672 | * Needed since both structs are encoded into the same media type on input. |
||
| 673 | * |
||
| 674 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
| 675 | * |
||
| 676 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
| 677 | */ |
||
| 678 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
| 686 | } |
||
| 687 |
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.