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 |
||
| 12 | class Rule |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Holds rule id. |
||
| 16 | * |
||
| 17 | * @var mixed |
||
| 18 | */ |
||
| 19 | public $id; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Hold name of rule. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Action used when determining is rule allow access to its Resource and Role. |
||
| 30 | * |
||
| 31 | * @var mixed |
||
| 32 | */ |
||
| 33 | protected $action = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Role |
||
| 37 | */ |
||
| 38 | protected $role; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \SimpleAcl\Resource |
||
| 42 | */ |
||
| 43 | protected $resource; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var RoleAggregateInterface |
||
| 47 | */ |
||
| 48 | protected $roleAggregate; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var ResourceAggregateInterface |
||
| 52 | */ |
||
| 53 | protected $resourceAggregate; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create Rule with given name. |
||
| 57 | * |
||
| 58 | * @param $name |
||
| 59 | */ |
||
| 60 | 47 | public function __construct($name) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Set aggregate objects. |
||
| 68 | * |
||
| 69 | * @param $roleAggregate |
||
| 70 | * @param $resourceAggregate |
||
| 71 | */ |
||
| 72 | 17 | public function resetAggregate($roleAggregate, $resourceAggregate) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return ResourceAggregateInterface |
||
| 89 | */ |
||
| 90 | 2 | public function getResourceAggregate() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param ResourceAggregateInterface $resourceAggregate |
||
| 97 | */ |
||
| 98 | 4 | public function setResourceAggregate(ResourceAggregateInterface $resourceAggregate) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @return RoleAggregateInterface |
||
| 105 | */ |
||
| 106 | 2 | public function getRoleAggregate() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param RoleAggregateInterface $roleAggregate |
||
| 113 | */ |
||
| 114 | 5 | public function setRoleAggregate(RoleAggregateInterface $roleAggregate) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 4 | public function getId() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param mixed $id |
||
| 129 | */ |
||
| 130 | 47 | public function setId($id = null) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param RuleResult|null $ruleResult |
||
| 141 | * |
||
| 142 | * @return bool|null |
||
| 143 | */ |
||
| 144 | 30 | public function getAction(RuleResult $ruleResult = null) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param mixed $action |
||
| 171 | */ |
||
| 172 | 38 | public function setAction($action) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Check owing Role & Resource and match its with $roleName & $resourceName; |
||
| 179 | * if match was found depending on action allow or deny access to $resourceName for $roleName. |
||
| 180 | * |
||
| 181 | * @param $needRuleName |
||
| 182 | * @param string $needRoleName |
||
| 183 | * @param string $needResourceName |
||
| 184 | * |
||
| 185 | * @return RuleResult|null null is returned if there is no matched Role & Resource in this rule. |
||
| 186 | * RuleResult otherwise. |
||
| 187 | */ |
||
| 188 | 25 | public function isAllowed($needRuleName, $needRoleName, $needResourceName) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Check if rule can be used. |
||
| 287 | * |
||
| 288 | * @param $needRuleName |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | 1 | protected function isRuleMatched($needRuleName) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 28 | public function getName() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $name |
||
| 307 | */ |
||
| 308 | 47 | public function setName($name) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @return Role |
||
| 315 | */ |
||
| 316 | 22 | public function getRole() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param Role|null $role |
||
| 323 | */ |
||
| 324 | 35 | public function setRole(Role $role = null) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @return \SimpleAcl\Resource |
||
| 331 | */ |
||
| 332 | 22 | public function getResource() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param \SimpleAcl\Resource $resource |
||
| 339 | */ |
||
| 340 | 35 | public function setResource(\SimpleAcl\Resource $resource = null) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Creates an id for rule. |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 47 | protected function generateId() |
|
| 356 | } |
||
| 357 |