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