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 PermissionService 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 PermissionService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class PermissionService implements PermissionServiceInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Counter for the current sudo nesting level {@see sudo()}. |
||
| 32 | * |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | private $sudoNestingLevel = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \eZ\Publish\API\Repository\Repository |
||
| 39 | */ |
||
| 40 | private $repository; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \eZ\Publish\API\Repository\UserService |
||
| 44 | */ |
||
| 45 | private $userService; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \eZ\Publish\Core\Repository\Helper\RoleDomainMapper |
||
| 49 | */ |
||
| 50 | private $roleDomainMapper; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \eZ\Publish\Core\Repository\Helper\LimitationService |
||
| 54 | */ |
||
| 55 | private $limitationService; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \eZ\Publish\SPI\Persistence\User\Handler |
||
| 59 | */ |
||
| 60 | private $userHandler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Currently logged in user object if already loaded. |
||
| 64 | * |
||
| 65 | * @var \eZ\Publish\API\Repository\Values\User\User|null |
||
| 66 | */ |
||
| 67 | private $currentUser; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Currently logged in user reference for permission purposes. |
||
| 71 | * |
||
| 72 | * @var \eZ\Publish\API\Repository\Values\User\UserReference |
||
| 73 | */ |
||
| 74 | private $currentUserRef; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 78 | * @param \eZ\Publish\API\Repository\UserService $userService |
||
| 79 | * @param \eZ\Publish\Core\Repository\Helper\RoleDomainMapper $roleDomainMapper |
||
| 80 | * @param \eZ\Publish\Core\Repository\Helper\LimitationService $limitationService |
||
| 81 | * @param \eZ\Publish\SPI\Persistence\User\Handler $userHandler |
||
| 82 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $userReference |
||
| 83 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 84 | */ |
||
| 85 | public function __construct( |
||
| 102 | |||
| 103 | public function getCurrentUser() |
||
| 113 | |||
| 114 | public function getCurrentUserReference() |
||
| 118 | |||
| 119 | View Code Duplication | public function setCurrentUserReference(APIUserReference $user) |
|
| 120 | { |
||
| 121 | $id = $user->getUserId(); |
||
| 122 | if (!$id) { |
||
| 123 | throw new InvalidArgumentValue('$user->getUserId()', $id); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($user instanceof User) { |
||
| 127 | $this->currentUser = $user; |
||
| 128 | $this->currentUserRef = new UserReference($id); |
||
| 129 | } else { |
||
| 130 | $this->currentUser = null; |
||
| 131 | $this->currentUserRef = $user; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function hasAccess($module, $function, APIUserReference $user = null) |
||
| 194 | |||
| 195 | public function canUser($module, $function, ValueObject $object, $targets = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Allows API execution to be performed with full access sand-boxed. |
||
| 282 | * |
||
| 283 | * The closure sandbox will do a catch all on exceptions and rethrow after |
||
| 284 | * re-setting the sudo flag. |
||
| 285 | * |
||
| 286 | * Example use: |
||
| 287 | * $location = $repository->sudo( |
||
| 288 | * function ( Repository $repo ) use ( $locationId ) |
||
| 289 | * { |
||
| 290 | * return $repo->getLocationService()->loadLocation( $locationId ) |
||
| 291 | * } |
||
| 292 | * ); |
||
| 293 | * |
||
| 294 | * |
||
| 295 | * @param \Closure $callback |
||
| 296 | * @param \eZ\Publish\API\Repository\Repository $outerRepository |
||
| 297 | * |
||
| 298 | * @throws \RuntimeException Thrown on recursive sudo() use. |
||
| 299 | * @throws \Exception Re throws exceptions thrown inside $callback |
||
| 300 | * |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | public function sudo(\Closure $callback, RepositoryInterface $outerRepository = null) |
||
| 317 | } |
||
| 318 |
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.