Complex classes like Acl 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 Acl, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 36 | class Acl implements AclInterface |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var Samshal\Acl\Registry\RegistryInterface $roleRegistry |
||
| 40 | */ |
||
| 41 | public $roleRegistry; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Samshal\Acl\Registry\RegistryInterface $resourceRegistry |
||
| 45 | */ |
||
| 46 | protected $resourceRegistry; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Samshal\Acl\Registry\RegistryInterface $permissionRegistry |
||
| 50 | */ |
||
| 51 | protected $permissionRegistry; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Samshal\Acl\Registry\RegistryInterface $globalRegistry |
||
| 55 | */ |
||
| 56 | public $globalRegistry; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string[] $sesion |
||
| 60 | */ |
||
| 61 | protected $session = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string SYN_ALLOW |
||
| 65 | */ |
||
| 66 | const SYN_ALLOW = "can"; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string SYN_DENY |
||
| 70 | */ |
||
| 71 | const SYN_DENY = "cannot"; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Performs bootstrapping |
||
| 75 | */ |
||
| 76 | public function __construct() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Initalizes the registries |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | protected function initRegistries() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Initializes the global session array and sets them to the default value |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | protected function initSession() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Listen for and intercept properties that're not set |
||
| 108 | * |
||
| 109 | * @param string $role; |
||
| 110 | * @throws \Exception |
||
| 111 | * @return AclInterface |
||
| 112 | */ |
||
| 113 | public function __get(string $role) : AclInterface |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Listen for and intercept undefined methods |
||
| 143 | * |
||
| 144 | * @param string $permission |
||
| 145 | * @param string[] $args |
||
| 146 | * @throws \Exception |
||
| 147 | * @return boolean|null |
||
| 148 | */ |
||
| 149 | public function __call(string $permission, array $args) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add a new role object to the registry |
||
| 208 | * |
||
| 209 | * @param string[] $role |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function addRole(string ...$role) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Add a new resource object to the registry |
||
| 222 | * |
||
| 223 | * @param string[] $resource |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function addResource(string ...$resource) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add a new permission object to the registry |
||
| 236 | * |
||
| 237 | * @param string[] $permission |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function addPermission(string ...$permission) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Adds objects lazily. |
||
| 250 | * |
||
| 251 | * Automatically determine the type of an object and call the appropriate |
||
| 252 | * add method on it. |
||
| 253 | * |
||
| 254 | * @param ObjectInterface[] $objects |
||
| 255 | * @throws \Exception |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | public function add(ObjectInterface ...$objects) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Allows roles to inherit from the registries of other roles |
||
| 288 | * |
||
| 289 | * @param string[] $roles |
||
| 290 | */ |
||
| 291 | public function inherits(string ...$roles) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Change the status option of an assigned permission to true |
||
| 312 | * |
||
| 313 | * @param string $role; |
||
| 314 | * @param string $permission |
||
| 315 | * @param string $resource |
||
| 316 | * @param boolean $status Optional |
||
| 317 | * @throws \Exception |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public function allow(string $role, string $permission, string $resource, bool $status=null) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Change the status option of an assigned permission to false |
||
| 355 | * |
||
| 356 | * @param string $role; |
||
| 357 | * @param string $permission |
||
| 358 | * @param string $resource |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function deny(string $role, string $permission, string $resource) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Retrieve the status of a permission assigned to a role |
||
| 368 | * |
||
| 369 | * @param string $role; |
||
| 370 | * @param string $permission |
||
| 371 | * @param string $resource |
||
| 372 | * @return boolean |
||
| 373 | */ |
||
| 374 | public function getPermissionStatus(string $role, string $permission, string $resource) : bool |
||
| 424 | } |
||
| 425 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..