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 Permissions 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 Permissions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Permissions implements PermissionsInterface |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * The type of this Permissions |
||
| 43 | * |
||
| 44 | * default is the Fully qualified class name. |
||
| 45 | * |
||
| 46 | * @ODM\Field(type="string") |
||
| 47 | * @var string |
||
| 48 | * @since 0,18 |
||
| 49 | */ |
||
| 50 | protected $type; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Ids of users, which have view access. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | * @ODM\Collection |
||
| 57 | * @ODM\Index |
||
| 58 | */ |
||
| 59 | protected $view = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Ids of users, which have change access. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | * @ODM\Collection |
||
| 66 | * @ODM\Index |
||
| 67 | */ |
||
| 68 | protected $change = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Specification of assigned resources. |
||
| 72 | * |
||
| 73 | * As of 0.18, the format is: |
||
| 74 | * <pre> |
||
| 75 | * array( |
||
| 76 | * resourceId => array( |
||
| 77 | * permission => array(userId,...), |
||
| 78 | * ... |
||
| 79 | * ), |
||
| 80 | * ... |
||
| 81 | * ); |
||
| 82 | * </pre> |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | * @ODM\Hash |
||
| 86 | */ |
||
| 87 | protected $assigned = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Collection of all assigned resources. |
||
| 91 | * |
||
| 92 | * @var Collection |
||
| 93 | * @ODM\ReferenceMany(discriminatorField="_resource") |
||
| 94 | */ |
||
| 95 | protected $resources; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Flag, wether this permissions has changed or not. |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | protected $hasChanged = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Creates a Permissions instance. |
||
| 106 | * |
||
| 107 | * @param string|null $type The type identifier, defaults to FQCN. |
||
| 108 | */ |
||
| 109 | public function __construct($type = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Clones resources in a new ArrayCollection. |
||
| 116 | * Needed because PHP does not deep cloning objects. |
||
| 117 | * (That means, references stay references pointing to the same |
||
| 118 | * object than the parent.) |
||
| 119 | */ |
||
| 120 | public function __clone() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Provides magic methods. |
||
| 133 | * |
||
| 134 | * - is[View|Change|None|All]Granted($user) |
||
| 135 | * - grant[View|Change|None|All]($user) |
||
| 136 | * - revoke[View|Change|None|All($user) |
||
| 137 | * |
||
| 138 | * @param string $method |
||
| 139 | * @param array $params |
||
| 140 | * |
||
| 141 | * @return self|bool |
||
|
|
|||
| 142 | * @throws \InvalidArgumentException |
||
| 143 | * @throws \BadMethodCallException |
||
| 144 | */ |
||
| 145 | public function __call($method, $params) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the permission type |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | * @since 0.24 |
||
| 174 | */ |
||
| 175 | public function getType() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Grants a permission to a user or resource. |
||
| 182 | * |
||
| 183 | * {@inheritDoc} |
||
| 184 | * |
||
| 185 | * @param bool $build Should the view and change arrays be rebuild? |
||
| 186 | */ |
||
| 187 | public function grant($resource, $permission = null, $build = true) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Revokes a permission from a user or resource. |
||
| 252 | * |
||
| 253 | * {@inheritDoc} |
||
| 254 | * |
||
| 255 | * @param bool $build Should the view and change arrays be rebuild? |
||
| 256 | * |
||
| 257 | * @return $this|PermissionsInterface |
||
| 258 | */ |
||
| 259 | public function revoke($resource, $permission = null, $build = true) |
||
| 273 | |||
| 274 | public function clear() |
||
| 283 | |||
| 284 | public function inherit(PermissionsInterface $permissions, $build = true) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Builds the user id lists. |
||
| 324 | * |
||
| 325 | * This will make database queries faster and also the calls to {@link isGranted()} will be faster. |
||
| 326 | * |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function build() |
||
| 356 | |||
| 357 | private function checkIsGranted($userId, $permission) |
||
| 372 | |||
| 373 | public function isGranted($userOrId, $permission) |
||
| 389 | |||
| 390 | public function isAssigned($resource) |
||
| 395 | |||
| 396 | public function hasChanged() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Gets the assigned specification. |
||
| 403 | * |
||
| 404 | * This is only needed when inheriting this permissions object into another. |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function getAssigned() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Gets the resource collection. |
||
| 415 | * |
||
| 416 | * This is only needed when inheriting. |
||
| 417 | * |
||
| 418 | * @return Collection |
||
| 419 | */ |
||
| 420 | public function getResources() |
||
| 427 | |||
| 428 | |||
| 429 | public function getFrom($resource) |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Gets/Generates the resource id. |
||
| 445 | * |
||
| 446 | * @param string|UserInterface|PermissionsResourceInterface $resource |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | protected function getResourceId($resource) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Checks a valid permission. |
||
| 465 | * |
||
| 466 | * @param string $permission |
||
| 467 | * |
||
| 468 | * @throws \InvalidArgumentException |
||
| 469 | */ |
||
| 470 | protected function checkPermission($permission) |
||
| 484 | } |
||
| 485 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.