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 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
| 78 | */ |
||
| 79 | public function createRole(Request $request) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Loads list of roles. |
||
| 119 | * |
||
| 120 | * @return \eZ\Publish\Core\REST\Server\Values\RoleList |
||
| 121 | */ |
||
| 122 | public function listRoles(Request $request) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Loads role. |
||
| 148 | * |
||
| 149 | * @param $roleId |
||
| 150 | * |
||
| 151 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 152 | */ |
||
| 153 | public function loadRole($roleId) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Loads a role draft. |
||
| 160 | * |
||
| 161 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
| 162 | * |
||
| 163 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
| 164 | */ |
||
| 165 | public function loadRoleDraft($roleId) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Updates a role. |
||
| 179 | * |
||
| 180 | * @param $roleId |
||
| 181 | * |
||
| 182 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 183 | */ |
||
| 184 | public function updateRole($roleId, Request $request) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Updates 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 updateRoleDraft($roleId, Request $request) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Publishes a role draft. |
||
| 229 | * |
||
| 230 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
| 231 | * @return Values\RestRole |
||
| 232 | */ |
||
| 233 | public function publishRoleDraft($roleId) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Delete a role by ID. |
||
| 252 | * |
||
| 253 | * @param $roleId |
||
| 254 | * |
||
| 255 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 256 | */ |
||
| 257 | public function deleteRole($roleId) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Loads the policies for the role. |
||
| 268 | * |
||
| 269 | * @param $roleId |
||
| 270 | * |
||
| 271 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 272 | */ |
||
| 273 | public function loadPolicies($roleId, Request $request) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Deletes all policies from a role. |
||
| 282 | * |
||
| 283 | * @param $roleId |
||
| 284 | * |
||
| 285 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 286 | */ |
||
| 287 | public function deletePolicies($roleId) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Loads a policy. |
||
| 300 | * |
||
| 301 | * @param $roleId |
||
| 302 | * @param $policyId |
||
| 303 | * |
||
| 304 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 305 | * |
||
| 306 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 307 | */ |
||
| 308 | public function loadPolicy($roleId, $policyId, Request $request) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Adds a policy to role. |
||
| 322 | * |
||
| 323 | * @param $roleId |
||
| 324 | * |
||
| 325 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
| 326 | */ |
||
| 327 | public function addPolicy($roleId, Request $request) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Updates a policy. |
||
| 363 | * |
||
| 364 | * @param $roleId |
||
| 365 | * @param $policyId |
||
| 366 | * |
||
| 367 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 368 | * |
||
| 369 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
| 370 | */ |
||
| 371 | public function updatePolicy($roleId, $policyId, Request $request) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Delete a policy from role. |
||
| 399 | * |
||
| 400 | * @param $roleId |
||
| 401 | * @param $policyId |
||
| 402 | * |
||
| 403 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 404 | * |
||
| 405 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
| 406 | */ |
||
| 407 | View Code Duplication | public function deletePolicy($roleId, $policyId, Request $request) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Assigns role to user. |
||
| 430 | * |
||
| 431 | * @param $userId |
||
| 432 | * |
||
| 433 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 434 | */ |
||
| 435 | public function assignRoleToUser($userId, Request $request) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Assigns role to user group. |
||
| 460 | * |
||
| 461 | * @param $groupPath |
||
| 462 | * |
||
| 463 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 464 | */ |
||
| 465 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Un-assigns role from user. |
||
| 493 | * |
||
| 494 | * @param $userId |
||
| 495 | * @param $roleId |
||
| 496 | * |
||
| 497 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 498 | */ |
||
| 499 | public function unassignRoleFromUser($userId, $roleId) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Un-assigns role from user group. |
||
| 513 | * |
||
| 514 | * @param $groupPath |
||
| 515 | * @param $roleId |
||
| 516 | * |
||
| 517 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 518 | */ |
||
| 519 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Loads role assignments for user. |
||
| 535 | * |
||
| 536 | * @param $userId |
||
| 537 | * |
||
| 538 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 539 | */ |
||
| 540 | public function loadRoleAssignmentsForUser($userId) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Loads role assignments for user group. |
||
| 551 | * |
||
| 552 | * @param $groupPath |
||
| 553 | * |
||
| 554 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
| 555 | */ |
||
| 556 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Returns a role assignment to the given user. |
||
| 569 | * |
||
| 570 | * @param $userId |
||
| 571 | * @param $roleId |
||
| 572 | * |
||
| 573 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 574 | * |
||
| 575 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
| 576 | */ |
||
| 577 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns a role assignment to the given user group. |
||
| 593 | * |
||
| 594 | * @param $groupPath |
||
| 595 | * @param $roleId |
||
| 596 | * |
||
| 597 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
| 598 | * |
||
| 599 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
| 600 | */ |
||
| 601 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Search all policies which are applied to a given user. |
||
| 619 | * |
||
| 620 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
| 621 | */ |
||
| 622 | public function listPoliciesForUser(Request $request) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
| 634 | * |
||
| 635 | * Needed since both structs are encoded into the same media type on input. |
||
| 636 | * |
||
| 637 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
| 638 | * |
||
| 639 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
| 640 | */ |
||
| 641 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
| 649 | } |
||
| 650 |
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.