Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Manager extends Adapter |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var DbAdapter |
||
| 39 | */ |
||
| 40 | protected $connection; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Roles table. |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $roles; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Resources table. |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $resources; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Resources Accesses table. |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $resourcesAccesses; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Access List table. |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $accessList; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Roles Inherits table. |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $rolesInherits; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Default action for no arguments is allow. |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected $noArgumentsDefaultAction = Acl::ALLOW; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Company Object. |
||
| 80 | * |
||
| 81 | * @var Companies |
||
| 82 | */ |
||
| 83 | protected $company; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * App Objc. |
||
| 87 | * |
||
| 88 | * @var Apps |
||
| 89 | */ |
||
| 90 | protected $app; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Class constructor. |
||
| 94 | * |
||
| 95 | * @param array $options Adapter config |
||
| 96 | * @throws Exception |
||
| 97 | */ |
||
| 98 | 1 | public function __construct(array $options) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Set current user Company. |
||
| 105 | * |
||
| 106 | * @param Companies $company |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | public function setCompany(Companies $company) : void |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set current user app. |
||
| 116 | * |
||
| 117 | * @param Apps $app |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function setApp(Apps $app) : void |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the current App. |
||
| 127 | * |
||
| 128 | * @return void |
||
|
|
|||
| 129 | */ |
||
| 130 | public function getApp() : Apps |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the current App. |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public function getCompany() : Companies |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | * |
||
| 158 | * Example: |
||
| 159 | * <code> |
||
| 160 | * $acl->addRole(new Phalcon\Acl\Role('administrator'), 'consultor'); |
||
| 161 | * $acl->addRole('administrator', 'consultor'); |
||
| 162 | * </code> |
||
| 163 | * |
||
| 164 | * @param \Phalcon\Acl\Role|string $role |
||
| 165 | * @param int $scope |
||
| 166 | * @param string $accessInherits |
||
| 167 | * @return boolean |
||
| 168 | * @throws \Phalcon\Acl\Exception |
||
| 169 | */ |
||
| 170 | public function addRole($role, $scope = 0, $accessInherits = null) : bool |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | * |
||
| 211 | * @param string $roleName |
||
| 212 | * @return boolean |
||
| 213 | */ |
||
| 214 | public function isRole($roleName) : bool |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | * |
||
| 222 | * @param string $resourceName |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | public function isResource($resourceName) : bool |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | * |
||
| 233 | * @param string $roleName |
||
| 234 | * @param string $roleToInherit |
||
| 235 | * @throws \Phalcon\Acl\Exception |
||
| 236 | */ |
||
| 237 | public function addInherit($roleName, $roleToInherit) : bool |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Given a resource with a dot CRM.Leads , it will set the app. |
||
| 244 | * |
||
| 245 | * @param string $resource |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | protected function setAppByResource(string $resource) : string |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Given a resource with a dot CRM.Leads , it will set the app. |
||
| 267 | * |
||
| 268 | * @param string $resource |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | protected function setAppByRole(string $role) : string |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | * Example: |
||
| 291 | * <code> |
||
| 292 | * //Add a resource to the the list allowing access to an action |
||
| 293 | * $acl->addResource(new Phalcon\Acl\Resource('customers'), 'search'); |
||
| 294 | * $acl->addResource('customers', 'search'); |
||
| 295 | * //Add a resource with an access list |
||
| 296 | * $acl->addResource(new Phalcon\Acl\Resource('customers'), ['create', 'search']); |
||
| 297 | * $acl->addResource('customers', ['create', 'search']); |
||
| 298 | * $acl->addResource('App.customers', ['create', 'search']); |
||
| 299 | * </code>. |
||
| 300 | * |
||
| 301 | * @param \Phalcon\Acl\Resource|string $resource |
||
| 302 | * @param array|string $accessList |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public function addResource($resource, $accessList = null) : bool |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | * |
||
| 332 | * @param string $resourceName |
||
| 333 | * @param array|string $accessList |
||
| 334 | * @return boolean |
||
| 335 | * @throws \Phalcon\Acl\Exception |
||
| 336 | */ |
||
| 337 | public function addResourceAccess($resourceName, $accessList) : bool |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | * |
||
| 366 | * @return \Phalcon\Acl\Resource[] |
||
| 367 | */ |
||
| 368 | public function getResources() : \Phalcon\Acl\ResourceInterface |
||
| 377 | |||
| 378 | /** |
||
| 379 | * {@inheritdoc} |
||
| 380 | * |
||
| 381 | * @return RoleInterface[] |
||
| 382 | */ |
||
| 383 | public function getRoles() : \Phalcon\Acl\RoleInterface |
||
| 392 | |||
| 393 | /** |
||
| 394 | * {@inheritdoc} |
||
| 395 | * |
||
| 396 | * @param string $resourceName |
||
| 397 | * @param array|string $accessList |
||
| 398 | */ |
||
| 399 | public function dropResourceAccess($resourceName, $accessList) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | * You can use '*' as wildcard |
||
| 407 | * Example: |
||
| 408 | * <code> |
||
| 409 | * //Allow access to guests to search on customers |
||
| 410 | * $acl->allow('guests', 'customers', 'search'); |
||
| 411 | * //Allow access to guests to search or create on customers |
||
| 412 | * $acl->allow('guests', 'customers', ['search', 'create']); |
||
| 413 | * //Allow access to any role to browse on products |
||
| 414 | * $acl->allow('*', 'products', 'browse'); |
||
| 415 | * //Allow access to any role to browse on any resource |
||
| 416 | * $acl->allow('*', '*', 'browse'); |
||
| 417 | * </code>. |
||
| 418 | * |
||
| 419 | * @param string $roleName |
||
| 420 | * @param string $resourceName |
||
| 421 | * @param array|string $access |
||
| 422 | * @param mixed $func |
||
| 423 | */ |
||
| 424 | public function allow($roleName, $resourceName, $access, $func = null) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritdoc} |
||
| 431 | * You can use '*' as wildcard |
||
| 432 | * Example: |
||
| 433 | * <code> |
||
| 434 | * //Deny access to guests to search on customers |
||
| 435 | * $acl->deny('guests', 'customers', 'search'); |
||
| 436 | * //Deny access to guests to search or create on customers |
||
| 437 | * $acl->deny('guests', 'customers', ['search', 'create']); |
||
| 438 | * //Deny access to any role to browse on products |
||
| 439 | * $acl->deny('*', 'products', 'browse'); |
||
| 440 | * //Deny access to any role to browse on any resource |
||
| 441 | * $acl->deny('*', '*', 'browse'); |
||
| 442 | * </code>. |
||
| 443 | * |
||
| 444 | * @param string $roleName |
||
| 445 | * @param string $resourceName |
||
| 446 | * @param array|string $access |
||
| 447 | * @param mixed $func |
||
| 448 | * @return boolean |
||
| 449 | */ |
||
| 450 | public function deny($roleName, $resourceName, $access, $func = null) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * {@inheritdoc} |
||
| 457 | * Example: |
||
| 458 | * <code> |
||
| 459 | * //Does Andres have access to the customers resource to create? |
||
| 460 | * $acl->isAllowed('Andres', 'Products', 'create'); |
||
| 461 | * //Do guests have access to any resource to edit? |
||
| 462 | * $acl->isAllowed('guests', '*', 'edit'); |
||
| 463 | * </code>. |
||
| 464 | * |
||
| 465 | * @param string $role |
||
| 466 | * @param string $resource |
||
| 467 | * @param string $access |
||
| 468 | * @param array $parameters |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function isAllowed($role, $resource, $access, array $parameters = null) : bool |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Returns the default ACL access level for no arguments provided |
||
| 517 | * in isAllowed action if there exists func for accessKey. |
||
| 518 | * |
||
| 519 | * @return int |
||
| 520 | */ |
||
| 521 | public function getNoArgumentsDefaultAction() : int |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Sets the default access level for no arguments provided |
||
| 528 | * in isAllowed action if there exists func for accessKey. |
||
| 529 | * |
||
| 530 | * @param int $defaultAccess Phalcon\Acl::ALLOW or Phalcon\Acl::DENY |
||
| 531 | */ |
||
| 532 | public function setNoArgumentsDefaultAction($defaultAccess) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Inserts/Updates a permission in the access list. |
||
| 539 | * |
||
| 540 | * @param string $roleName |
||
| 541 | * @param string $resourceName |
||
| 542 | * @param string $accessName |
||
| 543 | * @param integer $action |
||
| 544 | * @return boolean |
||
| 545 | * @throws \Phalcon\Acl\Exception |
||
| 546 | */ |
||
| 547 | protected function insertOrUpdateAccess($roleName, $resourceName, $accessName, $action) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Inserts/Updates a permission in the access list. |
||
| 604 | * |
||
| 605 | * @param string $roleName |
||
| 606 | * @param string $resourceName |
||
| 607 | * @param array|string $access |
||
| 608 | * @param integer $action |
||
| 609 | * @throws \Phalcon\Acl\Exception |
||
| 610 | */ |
||
| 611 | protected function allowOrDeny($roleName, $resourceName, $access, $action) |
||
| 625 | } |
||
| 626 |
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.