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) |
|
278 | |||
279 | /** |
||
280 | * Check if rule can be used. |
||
281 | * |
||
282 | * @param $needRuleName |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 1 | protected function isRuleMatched($needRuleName) |
|
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | */ |
||
294 | 28 | public function getName() |
|
298 | |||
299 | /** |
||
300 | * @param string $name |
||
301 | */ |
||
302 | 47 | public function setName($name) |
|
306 | |||
307 | /** |
||
308 | * @return Role |
||
309 | */ |
||
310 | 22 | public function getRole() |
|
314 | |||
315 | /** |
||
316 | * @param Role|null $role |
||
317 | */ |
||
318 | 35 | public function setRole(Role $role = null) |
|
322 | |||
323 | /** |
||
324 | * @return \SimpleAcl\Resource |
||
325 | */ |
||
326 | 22 | public function getResource() |
|
330 | |||
331 | /** |
||
332 | * @param \SimpleAcl\Resource $resource |
||
333 | */ |
||
334 | 35 | public function setResource(\SimpleAcl\Resource $resource = null) |
|
338 | |||
339 | /** |
||
340 | * Creates an id for rule. |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | 47 | protected function generateId() |
|
350 | } |
||
351 |