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 Handler 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 Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Handler implements BaseUserHandler |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Gateway for storing user data. |
||
| 32 | * |
||
| 33 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Gateway |
||
| 34 | */ |
||
| 35 | protected $userGateway; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Gateway for storing role data. |
||
| 39 | * |
||
| 40 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway |
||
| 41 | */ |
||
| 42 | protected $roleGateway; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Mapper for user related objects. |
||
| 46 | * |
||
| 47 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Mapper |
||
| 48 | */ |
||
| 49 | protected $mapper; |
||
| 50 | |||
| 51 | /** @var \eZ\Publish\Core\Persistence\Legacy\User\Role\LimitationConverter */ |
||
| 52 | protected $limitationConverter; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Construct from userGateway. |
||
| 56 | * |
||
| 57 | * @param \eZ\Publish\Core\Persistence\Legacy\User\Gateway $userGateway |
||
| 58 | * @param \eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway $roleGateway |
||
| 59 | * @param \eZ\Publish\Core\Persistence\Legacy\User\Mapper $mapper |
||
| 60 | * @param \eZ\Publish\Core\Persistence\Legacy\User\Role\LimitationConverter $limitationConverter |
||
| 61 | */ |
||
| 62 | public function __construct(Gateway $userGateway, RoleGateway $roleGateway, Mapper $mapper, LimitationConverter $limitationConverter) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create a user. |
||
| 72 | * |
||
| 73 | * The User struct used to create the user will contain an ID which is used |
||
| 74 | * to reference the user. |
||
| 75 | * |
||
| 76 | * @param \eZ\Publish\SPI\Persistence\User $user |
||
| 77 | * |
||
| 78 | * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException |
||
| 79 | */ |
||
| 80 | public function create(User $user) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Loads user with user ID. |
||
| 87 | * |
||
| 88 | * @param mixed $userId |
||
| 89 | * |
||
| 90 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not found |
||
| 91 | * |
||
| 92 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function load($userId) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Loads user with user login. |
||
| 107 | * |
||
| 108 | * @param string $login |
||
| 109 | * |
||
| 110 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not found |
||
| 111 | * |
||
| 112 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function loadByLogin($login) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Loads user(s) with user email. |
||
| 129 | * |
||
| 130 | * As earlier eZ Publish versions supported several users having same email (ini config), |
||
| 131 | * this function may return several users. |
||
| 132 | * |
||
| 133 | * @param string $email |
||
| 134 | * |
||
| 135 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function loadByEmail(string $email): User |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Loads user(s) with user email. |
||
| 152 | * |
||
| 153 | * As earlier eZ Publish versions supported several users having same email (ini config), |
||
| 154 | * this function may return several users. |
||
| 155 | * |
||
| 156 | * @param string $email |
||
| 157 | * |
||
| 158 | * @return \eZ\Publish\SPI\Persistence\User[] |
||
| 159 | */ |
||
| 160 | public function loadUsersByEmail(string $email): array |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Loads user with user hash. |
||
| 173 | * |
||
| 174 | * @param string $hash |
||
| 175 | * |
||
| 176 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not found |
||
| 177 | * |
||
| 178 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function loadUserByToken($hash) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Update the user information specified by the user struct. |
||
| 193 | * |
||
| 194 | * @param \eZ\Publish\SPI\Persistence\User $user |
||
| 195 | * |
||
| 196 | * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException |
||
| 197 | */ |
||
| 198 | public function update(User $user) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Update the user token information specified by the userToken struct. |
||
| 205 | * |
||
| 206 | * @param \eZ\Publish\SPI\Persistence\User\UserTokenUpdateStruct $userTokenUpdateStruct |
||
| 207 | */ |
||
| 208 | public function updateUserToken(UserTokenUpdateStruct $userTokenUpdateStruct) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Expires user account key with user hash. |
||
| 215 | * |
||
| 216 | * @param string $hash |
||
| 217 | */ |
||
| 218 | public function expireUserToken($hash) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Delete user with the given ID. |
||
| 225 | * |
||
| 226 | * @param mixed $userId |
||
| 227 | * |
||
| 228 | * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException |
||
| 229 | */ |
||
| 230 | public function delete($userId) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Create new role draft. |
||
| 237 | * |
||
| 238 | * Sets status to Role::STATUS_DRAFT on the new returned draft. |
||
| 239 | * |
||
| 240 | * @param \eZ\Publish\SPI\Persistence\User\RoleCreateStruct $createStruct |
||
| 241 | * |
||
| 242 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 243 | */ |
||
| 244 | public function createRole(RoleCreateStruct $createStruct) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Creates a draft of existing defined role. |
||
| 251 | * |
||
| 252 | * Sets status to Role::STATUS_DRAFT on the new returned draft. |
||
| 253 | * |
||
| 254 | * @param mixed $roleId |
||
| 255 | * |
||
| 256 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role with defined status is not found |
||
| 257 | * |
||
| 258 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 259 | */ |
||
| 260 | public function createRoleDraft($roleId) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Internal method for creating Role. |
||
| 271 | * |
||
| 272 | * Used by self::createRole() and self::createRoleDraft() |
||
| 273 | * |
||
| 274 | * @param \eZ\Publish\SPI\Persistence\User\RoleCreateStruct $createStruct |
||
| 275 | * @param mixed|null $roleId Used by self::createRoleDraft() to retain Role id in the draft |
||
| 276 | * |
||
| 277 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 278 | */ |
||
| 279 | protected function internalCreateRole(RoleCreateStruct $createStruct, $roleId = null) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Copies an existing role. |
||
| 299 | */ |
||
| 300 | public function copyRole(User\RoleCopyStruct $copyStruct): Role |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Loads a specified role (draft) by $roleId and $status. |
||
| 317 | * |
||
| 318 | * @param mixed $roleId |
||
| 319 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 320 | * |
||
| 321 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role with given status does not exist |
||
| 322 | * |
||
| 323 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function loadRole($roleId, $status = Role::STATUS_DEFINED) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Loads a specified role (draft) by $identifier and $status. |
||
| 343 | * |
||
| 344 | * @param string $identifier |
||
| 345 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 346 | * |
||
| 347 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found |
||
| 348 | * |
||
| 349 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 350 | */ |
||
| 351 | View Code Duplication | public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Loads a role draft by the original role ID. |
||
| 369 | * |
||
| 370 | * @param mixed $roleId ID of the role the draft was created from. |
||
| 371 | * |
||
| 372 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found |
||
| 373 | * |
||
| 374 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 375 | */ |
||
| 376 | View Code Duplication | public function loadRoleDraftByRoleId($roleId) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Loads all roles. |
||
| 394 | * |
||
| 395 | * @return \eZ\Publish\SPI\Persistence\User\Role[] |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function loadRoles() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Update role (draft). |
||
| 413 | * |
||
| 414 | * @param \eZ\Publish\SPI\Persistence\User\RoleUpdateStruct $role |
||
| 415 | */ |
||
| 416 | public function updateRole(RoleUpdateStruct $role) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Delete the specified role (draft). |
||
| 423 | * |
||
| 424 | * @param mixed $roleId |
||
| 425 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 426 | */ |
||
| 427 | public function deleteRole($roleId, $status = Role::STATUS_DEFINED) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Publish the specified role draft. |
||
| 440 | * |
||
| 441 | * @param mixed $roleDraftId |
||
| 442 | */ |
||
| 443 | public function publishRoleDraft($roleDraftId) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Adds a policy to a role draft. |
||
| 473 | * |
||
| 474 | * @param mixed $roleId |
||
| 475 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 476 | * |
||
| 477 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 478 | */ |
||
| 479 | public function addPolicyByRoleDraft($roleId, Policy $policy) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Adds a policy to a role. |
||
| 495 | * |
||
| 496 | * @param mixed $roleId |
||
| 497 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 498 | * |
||
| 499 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 500 | */ |
||
| 501 | public function addPolicy($roleId, Policy $policy) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Update a policy. |
||
| 515 | * |
||
| 516 | * Replaces limitations values with new values. |
||
| 517 | * |
||
| 518 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 519 | */ |
||
| 520 | public function updatePolicy(Policy $policy) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Removes a policy from a role. |
||
| 531 | * |
||
| 532 | * @param mixed $policyId |
||
| 533 | * @param mixed $roleId |
||
| 534 | */ |
||
| 535 | public function deletePolicy($policyId, $roleId) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns the user policies associated with the user (including inherited policies from user groups). |
||
| 545 | * |
||
| 546 | * @param mixed $userId |
||
| 547 | * |
||
| 548 | * @return \eZ\Publish\SPI\Persistence\User\Policy[] |
||
| 549 | */ |
||
| 550 | View Code Duplication | public function loadPoliciesByUserId($userId) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Assigns role to a user or user group with given limitations. |
||
| 565 | * |
||
| 566 | * The limitation array looks like: |
||
| 567 | * <code> |
||
| 568 | * array( |
||
| 569 | * 'Subtree' => array( |
||
| 570 | * '/1/2/', |
||
| 571 | * '/1/4/', |
||
| 572 | * ), |
||
| 573 | * 'Foo' => array( 'Bar' ), |
||
| 574 | * … |
||
| 575 | * ) |
||
| 576 | * </code> |
||
| 577 | * |
||
| 578 | * Where the keys are the limitation identifiers, and the respective values |
||
| 579 | * are an array of limitation values. The limitation parameter is optional. |
||
| 580 | * |
||
| 581 | * @param mixed $contentId The groupId or userId to assign the role to. |
||
| 582 | * @param mixed $roleId |
||
| 583 | * @param array $limitation |
||
| 584 | */ |
||
| 585 | public function assignRole($contentId, $roleId, array $limitation = null) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Un-assign a role. |
||
| 593 | * |
||
| 594 | * @param mixed $contentId The user or user group Id to un-assign the role from. |
||
| 595 | * @param mixed $roleId |
||
| 596 | */ |
||
| 597 | public function unassignRole($contentId, $roleId) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Un-assign a role by assignment ID. |
||
| 604 | * |
||
| 605 | * @param mixed $roleAssignmentId The assignment ID. |
||
| 606 | */ |
||
| 607 | public function removeRoleAssignment($roleAssignmentId) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Loads role assignment for specified assignment ID. |
||
| 614 | * |
||
| 615 | * @param mixed $roleAssignmentId |
||
| 616 | * |
||
| 617 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role assignment is not found |
||
| 618 | * |
||
| 619 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment |
||
| 620 | */ |
||
| 621 | public function loadRoleAssignment($roleAssignmentId) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Loads roles assignments Role. |
||
| 634 | * |
||
| 635 | * Role Assignments with same roleId and limitationIdentifier will be merged together into one. |
||
| 636 | * |
||
| 637 | * @param mixed $roleId |
||
| 638 | * |
||
| 639 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
| 640 | */ |
||
| 641 | View Code Duplication | public function loadRoleAssignmentsByRoleId($roleId) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Loads roles assignments to a user/group. |
||
| 654 | * |
||
| 655 | * Role Assignments with same roleId and limitationIdentifier will be merged together into one. |
||
| 656 | * |
||
| 657 | * @param mixed $groupId In legacy storage engine this is the content object id roles are assigned to in ezuser_role. |
||
| 658 | * By the nature of legacy this can currently also be used to get by $userId. |
||
| 659 | * @param bool $inherit If true also return inherited role assignments from user groups. |
||
| 660 | * |
||
| 661 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
| 662 | */ |
||
| 663 | View Code Duplication | public function loadRoleAssignmentsByGroupId($groupId, $inherit = false) |
|
| 673 | } |
||
| 674 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.