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 | * Loads a specified role (draft) by $roleId and $status. |
||
| 299 | * |
||
| 300 | * @param mixed $roleId |
||
| 301 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 302 | * |
||
| 303 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role with given status does not exist |
||
| 304 | * |
||
| 305 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 306 | */ |
||
| 307 | View Code Duplication | public function loadRole($roleId, $status = Role::STATUS_DEFINED) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Loads a specified role (draft) by $identifier and $status. |
||
| 325 | * |
||
| 326 | * @param string $identifier |
||
| 327 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 328 | * |
||
| 329 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found |
||
| 330 | * |
||
| 331 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Loads a role draft by the original role ID. |
||
| 351 | * |
||
| 352 | * @param mixed $roleId ID of the role the draft was created from. |
||
| 353 | * |
||
| 354 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found |
||
| 355 | * |
||
| 356 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function loadRoleDraftByRoleId($roleId) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Loads all roles. |
||
| 376 | * |
||
| 377 | * @return \eZ\Publish\SPI\Persistence\User\Role[] |
||
| 378 | */ |
||
| 379 | View Code Duplication | public function loadRoles() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Update role (draft). |
||
| 395 | * |
||
| 396 | * @param \eZ\Publish\SPI\Persistence\User\RoleUpdateStruct $role |
||
| 397 | */ |
||
| 398 | public function updateRole(RoleUpdateStruct $role) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Delete the specified role (draft). |
||
| 405 | * |
||
| 406 | * @param mixed $roleId |
||
| 407 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 408 | */ |
||
| 409 | public function deleteRole($roleId, $status = Role::STATUS_DEFINED) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Publish the specified role draft. |
||
| 422 | * |
||
| 423 | * @param mixed $roleDraftId |
||
| 424 | */ |
||
| 425 | public function publishRoleDraft($roleDraftId) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Adds a policy to a role draft. |
||
| 455 | * |
||
| 456 | * @param mixed $roleId |
||
| 457 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 458 | * |
||
| 459 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 460 | */ |
||
| 461 | public function addPolicyByRoleDraft($roleId, Policy $policy) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Adds a policy to a role. |
||
| 477 | * |
||
| 478 | * @param mixed $roleId |
||
| 479 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 480 | * |
||
| 481 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 482 | */ |
||
| 483 | public function addPolicy($roleId, Policy $policy) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Update a policy. |
||
| 497 | * |
||
| 498 | * Replaces limitations values with new values. |
||
| 499 | * |
||
| 500 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 501 | */ |
||
| 502 | public function updatePolicy(Policy $policy) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Removes a policy from a role. |
||
| 513 | * |
||
| 514 | * @param mixed $policyId |
||
| 515 | * @param mixed $roleId |
||
| 516 | */ |
||
| 517 | public function deletePolicy($policyId, $roleId) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Returns the user policies associated with the user (including inherited policies from user groups). |
||
| 527 | * |
||
| 528 | * @param mixed $userId |
||
| 529 | * |
||
| 530 | * @return \eZ\Publish\SPI\Persistence\User\Policy[] |
||
| 531 | */ |
||
| 532 | View Code Duplication | public function loadPoliciesByUserId($userId) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Assigns role to a user or user group with given limitations. |
||
| 547 | * |
||
| 548 | * The limitation array looks like: |
||
| 549 | * <code> |
||
| 550 | * array( |
||
| 551 | * 'Subtree' => array( |
||
| 552 | * '/1/2/', |
||
| 553 | * '/1/4/', |
||
| 554 | * ), |
||
| 555 | * 'Foo' => array( 'Bar' ), |
||
| 556 | * … |
||
| 557 | * ) |
||
| 558 | * </code> |
||
| 559 | * |
||
| 560 | * Where the keys are the limitation identifiers, and the respective values |
||
| 561 | * are an array of limitation values. The limitation parameter is optional. |
||
| 562 | * |
||
| 563 | * @param mixed $contentId The groupId or userId to assign the role to. |
||
| 564 | * @param mixed $roleId |
||
| 565 | * @param array $limitation |
||
| 566 | */ |
||
| 567 | public function assignRole($contentId, $roleId, array $limitation = null) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Un-assign a role. |
||
| 575 | * |
||
| 576 | * @param mixed $contentId The user or user group Id to un-assign the role from. |
||
| 577 | * @param mixed $roleId |
||
| 578 | */ |
||
| 579 | public function unassignRole($contentId, $roleId) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Un-assign a role by assignment ID. |
||
| 586 | * |
||
| 587 | * @param mixed $roleAssignmentId The assignment ID. |
||
| 588 | */ |
||
| 589 | public function removeRoleAssignment($roleAssignmentId) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Loads role assignment for specified assignment ID. |
||
| 596 | * |
||
| 597 | * @param mixed $roleAssignmentId |
||
| 598 | * |
||
| 599 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role assignment is not found |
||
| 600 | * |
||
| 601 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment |
||
| 602 | */ |
||
| 603 | public function loadRoleAssignment($roleAssignmentId) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Loads roles assignments Role. |
||
| 616 | * |
||
| 617 | * Role Assignments with same roleId and limitationIdentifier will be merged together into one. |
||
| 618 | * |
||
| 619 | * @param mixed $roleId |
||
| 620 | * |
||
| 621 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
| 622 | */ |
||
| 623 | View Code Duplication | public function loadRoleAssignmentsByRoleId($roleId) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Loads roles assignments to a user/group. |
||
| 636 | * |
||
| 637 | * Role Assignments with same roleId and limitationIdentifier will be merged together into one. |
||
| 638 | * |
||
| 639 | * @param mixed $groupId In legacy storage engine this is the content object id roles are assigned to in ezuser_role. |
||
| 640 | * By the nature of legacy this can currently also be used to get by $userId. |
||
| 641 | * @param bool $inherit If true also return inherited role assignments from user groups. |
||
| 642 | * |
||
| 643 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
| 644 | */ |
||
| 645 | View Code Duplication | public function loadRoleAssignmentsByGroupId($groupId, $inherit = false) |
|
| 655 | } |
||
| 656 |
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.