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 |
||
14 | class Rule |
||
15 | { |
||
16 | /** |
||
17 | * Holds rule id. |
||
18 | * |
||
19 | * @var mixed |
||
20 | */ |
||
21 | public $id; |
||
22 | |||
23 | /** |
||
24 | * Rule priority affect the order the rule is applied. |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $priority = 0; |
||
29 | |||
30 | /** |
||
31 | * Hold name of rule. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $name; |
||
36 | |||
37 | /** |
||
38 | * Action used when determining is rule allow access to its Resource and Role. |
||
39 | * |
||
40 | * @var mixed |
||
41 | */ |
||
42 | protected $action = false; |
||
43 | |||
44 | /** |
||
45 | * @var Role |
||
46 | */ |
||
47 | protected $role; |
||
48 | |||
49 | /** |
||
50 | * @var \SimpleAcl\Resource |
||
51 | */ |
||
52 | protected $resource; |
||
53 | |||
54 | /** |
||
55 | * @var RoleAggregateInterface |
||
56 | */ |
||
57 | protected $roleAggregate; |
||
58 | |||
59 | /** |
||
60 | * @var ResourceAggregateInterface |
||
61 | */ |
||
62 | protected $resourceAggregate; |
||
63 | |||
64 | /** |
||
65 | * Create Rule with given name. |
||
66 | * |
||
67 | * @param $name |
||
68 | */ |
||
69 | 49 | public function __construct($name) |
|
74 | |||
75 | /** |
||
76 | * Set aggregate objects. |
||
77 | * |
||
78 | * @param $roleAggregate |
||
79 | * @param $resourceAggregate |
||
80 | */ |
||
81 | 26 | public function resetAggregate($roleAggregate, $resourceAggregate) |
|
95 | |||
96 | /** |
||
97 | * @return ResourceAggregateInterface |
||
98 | */ |
||
99 | 2 | public function getResourceAggregate() |
|
103 | |||
104 | /** |
||
105 | * @param ResourceAggregateInterface $resourceAggregate |
||
106 | */ |
||
107 | 4 | public function setResourceAggregate(ResourceAggregateInterface $resourceAggregate) |
|
111 | |||
112 | /** |
||
113 | * @return RoleAggregateInterface |
||
114 | */ |
||
115 | 2 | public function getRoleAggregate() |
|
119 | |||
120 | /** |
||
121 | * @param RoleAggregateInterface $roleAggregate |
||
122 | */ |
||
123 | 5 | public function setRoleAggregate(RoleAggregateInterface $roleAggregate) |
|
127 | |||
128 | /** |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 4 | public function getId() |
|
135 | |||
136 | /** |
||
137 | * @param mixed $id |
||
138 | */ |
||
139 | 49 | public function setId($id = null) |
|
147 | |||
148 | /** |
||
149 | * @param RuleResult|null $ruleResult |
||
150 | * |
||
151 | * @return bool|null |
||
152 | */ |
||
153 | 32 | public function getAction(RuleResult $ruleResult = null) |
|
154 | { |
||
155 | 32 | $actionResult = $this->action; |
|
156 | |||
157 | if ( |
||
158 | 32 | null === $ruleResult |
|
159 | 32 | || |
|
160 | 32 | !is_callable($actionResult) |
|
161 | 32 | ) { |
|
162 | 29 | if (null !== $actionResult) { |
|
163 | 29 | return (bool)$actionResult; |
|
164 | } else { |
||
165 | 3 | return null; |
|
166 | } |
||
167 | } |
||
168 | |||
169 | 4 | $actionResult = call_user_func($this->action, $ruleResult); |
|
170 | |||
171 | 4 | if (null !== $actionResult) { |
|
172 | 4 | return (bool)$actionResult; |
|
173 | } else { |
||
174 | return null; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param mixed $action |
||
180 | */ |
||
181 | 40 | public function setAction($action) |
|
185 | |||
186 | /** |
||
187 | * Check owing Role & Resource and match its with $roleName & $resourceName; |
||
188 | * if match was found depending on action allow or deny access to $resourceName for $roleName. |
||
189 | * |
||
190 | * @param $needRuleName |
||
191 | * @param string $needRoleName |
||
192 | * @param string $needResourceName |
||
193 | * |
||
194 | * @return RuleResult|null null is returned if there is no matched Role & Resource in this rule. |
||
195 | * RuleResult otherwise. |
||
196 | */ |
||
197 | 27 | public function isAllowed($needRuleName, $needRoleName, $needResourceName) |
|
293 | |||
294 | /** |
||
295 | * Check if rule can be used. |
||
296 | * |
||
297 | * @param $needRuleName |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | 1 | protected function isRuleMatched($needRuleName) |
|
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | 29 | public function getName() |
|
313 | |||
314 | /** |
||
315 | * @param string $name |
||
316 | */ |
||
317 | 49 | public function setName($name) |
|
321 | |||
322 | /** |
||
323 | * @return Role |
||
324 | */ |
||
325 | 7 | public function getRole() |
|
329 | |||
330 | /** |
||
331 | * @param Role|null $role |
||
332 | */ |
||
333 | 37 | public function setRole(Role $role = null) |
|
337 | |||
338 | /** |
||
339 | * @return \SimpleAcl\Resource |
||
340 | */ |
||
341 | 7 | public function getResource() |
|
345 | |||
346 | /** |
||
347 | * @param \SimpleAcl\Resource $resource |
||
348 | */ |
||
349 | 37 | public function setResource(\SimpleAcl\Resource $resource = null) |
|
353 | |||
354 | /** |
||
355 | * @return int |
||
356 | */ |
||
357 | 28 | public function getPriority() |
|
361 | |||
362 | /** |
||
363 | * @param int $priority |
||
364 | */ |
||
365 | 1 | public function setPriority($priority) |
|
369 | |||
370 | /** |
||
371 | * Creates an id for rule. |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | 49 | protected function generateId() |
|
381 | } |
||
382 |