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 |
||
| 26 | class Handler implements BaseUserHandler |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Gateway for storing user data. |
||
| 30 | * |
||
| 31 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Gateway |
||
| 32 | */ |
||
| 33 | protected $userGateway; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Gateway for storing role data. |
||
| 37 | * |
||
| 38 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Role\Gateway |
||
| 39 | */ |
||
| 40 | protected $roleGateway; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Mapper for user related objects. |
||
| 44 | * |
||
| 45 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Mapper |
||
| 46 | */ |
||
| 47 | protected $mapper; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \eZ\Publish\Core\Persistence\Legacy\User\Role\LimitationConverter |
||
| 51 | */ |
||
| 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 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 79 | */ |
||
| 80 | public function create(User $user) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Loads user with user ID. |
||
| 89 | * |
||
| 90 | * @param mixed $userId |
||
| 91 | * |
||
| 92 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not found |
||
| 93 | * |
||
| 94 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 95 | */ |
||
| 96 | public function load($userId) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Loads user with user login. |
||
| 109 | * |
||
| 110 | * @param string $login |
||
| 111 | * |
||
| 112 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not found |
||
| 113 | * |
||
| 114 | * @return \eZ\Publish\SPI\Persistence\User |
||
| 115 | */ |
||
| 116 | public function loadByLogin($login) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Loads user(s) with user email. |
||
| 131 | * |
||
| 132 | * As earlier eZ Publish versions supported several users having same email (ini config), |
||
| 133 | * this function may return several users. |
||
| 134 | * |
||
| 135 | * @param string $email |
||
| 136 | * |
||
| 137 | * @return \eZ\Publish\SPI\Persistence\User[] |
||
| 138 | */ |
||
| 139 | public function loadByEmail($email) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Update the user information specified by the user struct. |
||
| 152 | * |
||
| 153 | * @param \eZ\Publish\SPI\Persistence\User $user |
||
| 154 | */ |
||
| 155 | public function update(User $user) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Delete user with the given ID. |
||
| 162 | * |
||
| 163 | * @param mixed $userId |
||
| 164 | */ |
||
| 165 | public function delete($userId) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Create new role draft. |
||
| 172 | * |
||
| 173 | * Sets status to Role::STATUS_DRAFT on the new returned draft. |
||
| 174 | * |
||
| 175 | * @param \eZ\Publish\SPI\Persistence\User\RoleCreateStruct $createStruct |
||
| 176 | * |
||
| 177 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 178 | */ |
||
| 179 | public function createRole(RoleCreateStruct $createStruct) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Creates a draft of existing defined role. |
||
| 186 | * |
||
| 187 | * Sets status to Role::STATUS_DRAFT on the new returned draft. |
||
| 188 | * |
||
| 189 | * @param mixed $roleId |
||
| 190 | * |
||
| 191 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role with defined status is not found |
||
| 192 | * |
||
| 193 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 194 | */ |
||
| 195 | public function createRoleDraft($roleId) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Internal method for creating Role. |
||
| 206 | * |
||
| 207 | * Used by self::createRole() and self::createRoleDraft() |
||
| 208 | * |
||
| 209 | * @param \eZ\Publish\SPI\Persistence\User\RoleCreateStruct $createStruct |
||
| 210 | * @param mixed|null $roleId Used by self::createRoleDraft() to retain Role id in the draft |
||
| 211 | * |
||
| 212 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 213 | */ |
||
| 214 | protected function internalCreateRole(RoleCreateStruct $createStruct, $roleId = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Loads a specified role (draft) by $roleId and $status. |
||
| 234 | * |
||
| 235 | * @param mixed $roleId |
||
| 236 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 237 | * |
||
| 238 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role with given status does not exist |
||
| 239 | * |
||
| 240 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function loadRole($roleId, $status = Role::STATUS_DEFINED) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Loads a specified role (draft) by $identifier and $status. |
||
| 260 | * |
||
| 261 | * @param string $identifier |
||
| 262 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 263 | * |
||
| 264 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found |
||
| 265 | * |
||
| 266 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Loads a role draft by the original role ID. |
||
| 286 | * |
||
| 287 | * @param mixed $roleId ID of the role the draft was created from. |
||
| 288 | * |
||
| 289 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found |
||
| 290 | * |
||
| 291 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function loadRoleDraftByRoleId($roleId) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Loads all roles. |
||
| 311 | * |
||
| 312 | * @return \eZ\Publish\SPI\Persistence\User\Role[] |
||
| 313 | */ |
||
| 314 | View Code Duplication | public function loadRoles() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Update role (draft). |
||
| 330 | * |
||
| 331 | * @param \eZ\Publish\SPI\Persistence\User\RoleUpdateStruct $role |
||
| 332 | */ |
||
| 333 | public function updateRole(RoleUpdateStruct $role) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Delete the specified role (draft). |
||
| 340 | * |
||
| 341 | * @param mixed $roleId |
||
| 342 | * @param int $status One of Role::STATUS_DEFINED|Role::STATUS_DRAFT |
||
| 343 | */ |
||
| 344 | public function deleteRole($roleId, $status = Role::STATUS_DEFINED) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Publish the specified role draft. |
||
| 357 | * |
||
| 358 | * @param mixed $roleDraftId |
||
| 359 | */ |
||
| 360 | public function publishRoleDraft($roleDraftId) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Adds a policy to a role draft. |
||
| 390 | * |
||
| 391 | * @param mixed $roleId |
||
| 392 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 393 | * |
||
| 394 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 395 | */ |
||
| 396 | public function addPolicyByRoleDraft($roleId, Policy $policy) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Adds a policy to a role. |
||
| 412 | * |
||
| 413 | * @param mixed $roleId |
||
| 414 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 415 | * |
||
| 416 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 417 | */ |
||
| 418 | public function addPolicy($roleId, Policy $policy) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Update a policy. |
||
| 432 | * |
||
| 433 | * Replaces limitations values with new values. |
||
| 434 | * |
||
| 435 | * @param \eZ\Publish\SPI\Persistence\User\Policy $policy |
||
| 436 | */ |
||
| 437 | public function updatePolicy(Policy $policy) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Removes a policy from a role. |
||
| 448 | * |
||
| 449 | * @param mixed $policyId |
||
| 450 | */ |
||
| 451 | public function deletePolicy($policyId) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns the user policies associated with the user (including inherited policies from user groups). |
||
| 461 | * |
||
| 462 | * @param mixed $userId |
||
| 463 | * |
||
| 464 | * @return \eZ\Publish\SPI\Persistence\User\Policy[] |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function loadPoliciesByUserId($userId) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Assigns role to a user or user group with given limitations. |
||
| 481 | * |
||
| 482 | * The limitation array looks like: |
||
| 483 | * <code> |
||
| 484 | * array( |
||
| 485 | * 'Subtree' => array( |
||
| 486 | * '/1/2/', |
||
| 487 | * '/1/4/', |
||
| 488 | * ), |
||
| 489 | * 'Foo' => array( 'Bar' ), |
||
| 490 | * … |
||
| 491 | * ) |
||
| 492 | * </code> |
||
| 493 | * |
||
| 494 | * Where the keys are the limitation identifiers, and the respective values |
||
| 495 | * are an array of limitation values. The limitation parameter is optional. |
||
| 496 | * |
||
| 497 | * @param mixed $contentId The groupId or userId to assign the role to. |
||
| 498 | * @param mixed $roleId |
||
| 499 | * @param array $limitation |
||
| 500 | */ |
||
| 501 | public function assignRole($contentId, $roleId, array $limitation = null) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Un-assign a role. |
||
| 509 | * |
||
| 510 | * @param mixed $contentId The user or user group Id to un-assign the role from. |
||
| 511 | * @param mixed $roleId |
||
| 512 | */ |
||
| 513 | public function unassignRole($contentId, $roleId) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Un-assign a role by assignment ID. |
||
| 520 | * |
||
| 521 | * @param mixed $roleAssignmentId The assignment ID. |
||
| 522 | */ |
||
| 523 | public function removeRoleAssignment($roleAssignmentId) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Loads role assignment for specified assignment ID. |
||
| 530 | * |
||
| 531 | * @param mixed $roleAssignmentId |
||
| 532 | * |
||
| 533 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role assignment is not found |
||
| 534 | * |
||
| 535 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment |
||
| 536 | */ |
||
| 537 | public function loadRoleAssignment($roleAssignmentId) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Loads roles assignments Role. |
||
| 550 | * |
||
| 551 | * Role Assignments with same roleId and limitationIdentifier will be merged together into one. |
||
| 552 | * |
||
| 553 | * @param mixed $roleId |
||
| 554 | * |
||
| 555 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
| 556 | */ |
||
| 557 | View Code Duplication | public function loadRoleAssignmentsByRoleId($roleId) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Loads roles assignments to a user/group. |
||
| 570 | * |
||
| 571 | * Role Assignments with same roleId and limitationIdentifier will be merged together into one. |
||
| 572 | * |
||
| 573 | * @param mixed $groupId In legacy storage engine this is the content object id roles are assigned to in ezuser_role. |
||
| 574 | * By the nature of legacy this can currently also be used to get by $userId. |
||
| 575 | * @param bool $inherit If true also return inherited role assignments from user groups. |
||
| 576 | * |
||
| 577 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
| 578 | */ |
||
| 579 | View Code Duplication | public function loadRoleAssignmentsByGroupId($groupId, $inherit = false) |
|
| 589 | } |
||
| 590 |
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.