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 |
||
14 | class Acl |
||
15 | { |
||
16 | /** |
||
17 | * Contains registered rules. |
||
18 | * |
||
19 | * @var Rule[] |
||
20 | */ |
||
21 | protected $rules = array(); |
||
22 | |||
23 | /** |
||
24 | * Class name used when rule created from string. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $ruleClass = 'SimpleAcl\Rule'; |
||
29 | |||
30 | /** |
||
31 | * Adds rule. |
||
32 | * |
||
33 | * Assign $role, $resource and $action to added rule. |
||
34 | * If rule was already registered only change $role, $resource and $action for that rule. |
||
35 | * |
||
36 | * This method accept 1, 2, 3 or 4 arguments: |
||
37 | * |
||
38 | * addRule($rule) |
||
39 | * addRule($rule, $action) |
||
40 | * addRule($role, $resource, $rule) |
||
41 | * addRule($role, $resource, $rule, $action) |
||
42 | * |
||
43 | * @param Role ..$role |
||
44 | * @param Resource ..$resource |
||
45 | * @param Rule|string ..$rule |
||
46 | * @param mixed ..$action |
||
47 | * |
||
48 | * @throws InvalidArgumentException |
||
49 | */ |
||
50 | 37 | public function addRule() |
|
122 | |||
123 | /** |
||
124 | * Return rule class. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 5 | public function getRuleClass() |
|
132 | |||
133 | /** |
||
134 | * Set rule class. |
||
135 | * |
||
136 | * @param string $ruleClass |
||
137 | */ |
||
138 | 5 | public function setRuleClass($ruleClass) |
|
154 | |||
155 | /** |
||
156 | * Return true if rule was already added. |
||
157 | * |
||
158 | * @param Rule | mixed $needRule Rule or rule's id |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | 33 | public function hasRule($needRule) |
|
178 | |||
179 | /** |
||
180 | * Checks is access allowed. |
||
181 | * |
||
182 | * @param string|RoleAggregateInterface $roleName |
||
183 | * @param string|ResourceAggregateInterface $resourceName |
||
184 | * @param string $ruleName |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | 28 | public function isAllowed($roleName, $resourceName, $ruleName) |
|
192 | |||
193 | /** |
||
194 | * Simple checks is access allowed. |
||
195 | * |
||
196 | * @param string|RoleAggregateInterface $roleAggregate |
||
197 | * @param string|ResourceAggregateInterface $resourceAggregate |
||
198 | * @param string $ruleName |
||
199 | * @param RuleResultCollection $ruleResultCollection |
||
200 | * |
||
201 | * @return RuleResultCollection|null null if there wasn't a clear result |
||
202 | */ |
||
203 | 28 | protected function isAllowedReturnResultSimple($roleAggregate, $resourceAggregate, $ruleName, $ruleResultCollection) |
|
251 | |||
252 | /** |
||
253 | * Checks is access allowed. |
||
254 | * |
||
255 | * @param string|RoleAggregateInterface $roleAggregate |
||
256 | * @param string|ResourceAggregateInterface $resourceAggregate |
||
257 | * @param string $ruleName |
||
258 | * |
||
259 | * @return RuleResultCollection |
||
260 | */ |
||
261 | 28 | public function isAllowedReturnResult($roleAggregate, $resourceAggregate, $ruleName) |
|
299 | |||
300 | /** |
||
301 | * Get names. |
||
302 | * |
||
303 | * @param string|RoleAggregateInterface|ResourceAggregateInterface $object |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | 22 | protected function getNames($object) |
|
319 | |||
320 | /** |
||
321 | * Remove rules by rule name and (or) role and resource. |
||
322 | * |
||
323 | * @param null|string $roleName |
||
324 | * @param null|string $resourceName |
||
325 | * @param null|string $ruleName |
||
326 | * @param bool $all |
||
327 | */ |
||
328 | 3 | public function removeRule($roleName = null, $resourceName = null, $ruleName = null, $all = true) |
|
385 | |||
386 | /** |
||
387 | * Remove all rules. |
||
388 | * |
||
389 | */ |
||
390 | 3 | public function removeAllRules() |
|
394 | |||
395 | /** |
||
396 | * Removes rule by its id. |
||
397 | * |
||
398 | * @param mixed $ruleId |
||
399 | */ |
||
400 | 2 | public function removeRuleById($ruleId) |
|
410 | } |
||
411 |