Complex classes like Rule 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 Rule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Rule |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Holds rule id. |
||
| 17 | * |
||
| 18 | * @var null|int |
||
| 19 | */ |
||
| 20 | public $id; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Hold name of rule. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Action used when determining is rule allow access to its Resource and Role. |
||
| 31 | * |
||
| 32 | * @var mixed |
||
| 33 | */ |
||
| 34 | protected $action = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Role |
||
| 38 | */ |
||
| 39 | protected $role; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \SimpleAcl\Resource |
||
| 43 | */ |
||
| 44 | protected $resource; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var RoleAggregateInterface |
||
| 48 | */ |
||
| 49 | protected $roleAggregate; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var ResourceAggregateInterface |
||
| 53 | */ |
||
| 54 | protected $resourceAggregate; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create Rule with given name. |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | 47 | */ |
|
| 61 | public function __construct($name) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set aggregate objects. |
||
| 69 | * |
||
| 70 | * @param RoleAggregateInterface|mixed $roleAggregate |
||
| 71 | * @param ResourceAggregateInterface|mixed $resourceAggregate |
||
| 72 | 17 | */ |
|
| 73 | public function resetAggregate($roleAggregate, $resourceAggregate) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return ResourceAggregateInterface |
||
| 90 | 2 | */ |
|
| 91 | public function getResourceAggregate(): ResourceAggregateInterface |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param ResourceAggregateInterface $resourceAggregate |
||
| 98 | 4 | */ |
|
| 99 | public function setResourceAggregate(ResourceAggregateInterface $resourceAggregate) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return RoleAggregateInterface |
||
| 106 | 2 | */ |
|
| 107 | public function getRoleAggregate(): RoleAggregateInterface |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param RoleAggregateInterface $roleAggregate |
||
| 114 | 5 | */ |
|
| 115 | public function setRoleAggregate(RoleAggregateInterface $roleAggregate) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return null|int |
||
| 122 | 4 | */ |
|
| 123 | public function getId() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param int|null $id |
||
| 130 | 47 | */ |
|
| 131 | public function setId(int $id = null) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param RuleResult|null $ruleResult |
||
| 142 | * |
||
| 143 | * @return bool|null |
||
| 144 | 30 | */ |
|
| 145 | public function getAction(RuleResult $ruleResult = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param mixed $action |
||
| 172 | 38 | */ |
|
| 173 | public function setAction($action) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Check owing Role & Resource and match its with $roleName & $resourceName; |
||
| 180 | * if match was found depending on action allow or deny access to $resourceName for $roleName. |
||
| 181 | * |
||
| 182 | * @param string|null $needRuleName |
||
| 183 | * @param string|null $needRoleName |
||
| 184 | * @param string|null $needResourceName |
||
| 185 | * |
||
| 186 | * @return RuleResult|null null is returned if there is no matched Role & Resource in this rule. |
||
| 187 | * RuleResult otherwise. |
||
| 188 | 25 | */ |
|
| 189 | public function isAllowed($needRuleName, $needRoleName, $needResourceName) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Check if rule can be used. |
||
| 282 | * |
||
| 283 | * @param string|null $needRuleName |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | 1 | */ |
|
| 287 | protected function isRuleMatched($needRuleName): bool |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return string |
||
| 294 | 20 | */ |
|
| 295 | public function getName(): string |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $name |
||
| 302 | 47 | */ |
|
| 303 | public function setName(string $name) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return Role|null |
||
| 310 | 22 | */ |
|
| 311 | public function getRole() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param Role|null $role |
||
| 318 | 35 | */ |
|
| 319 | public function setRole(Role $role = null) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return \SimpleAcl\Resource|null |
||
| 326 | 22 | */ |
|
| 327 | public function getResource() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param \SimpleAcl\Resource $resource |
||
| 334 | 35 | */ |
|
| 335 | public function setResource(\SimpleAcl\Resource $resource = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Creates an id for rule. |
||
| 342 | * |
||
| 343 | * @return int |
||
| 344 | 47 | */ |
|
| 345 | protected function generateId(): int |
||
| 351 | } |
||
| 352 |