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 |
||
15 | class Rule |
||
16 | { |
||
17 | /** |
||
18 | * Holds rule id. |
||
19 | * |
||
20 | * @var mixed |
||
21 | */ |
||
22 | public $id; |
||
23 | |||
24 | /** |
||
25 | * Rule priority affect the order the rule is applied. |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $priority = 0; |
||
30 | |||
31 | /** |
||
32 | * Hold name of rule. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $name; |
||
37 | |||
38 | /** |
||
39 | * Action used when determining is rule allow access to its Resource and Role. |
||
40 | * |
||
41 | * @var mixed |
||
42 | */ |
||
43 | protected $action = false; |
||
44 | |||
45 | /** |
||
46 | * @var Role |
||
47 | */ |
||
48 | protected $role; |
||
49 | |||
50 | /** |
||
51 | * @var Resource |
||
52 | */ |
||
53 | protected $resource; |
||
54 | |||
55 | /** |
||
56 | * @var RoleAggregateInterface |
||
57 | */ |
||
58 | protected $roleAggregate; |
||
59 | |||
60 | /** |
||
61 | * @var ResourceAggregateInterface |
||
62 | */ |
||
63 | protected $resourceAggregate; |
||
64 | |||
65 | /** |
||
66 | * Create Rule with given name. |
||
67 | * |
||
68 | * @param $name |
||
69 | */ |
||
70 | 49 | public function __construct($name) |
|
75 | |||
76 | /** |
||
77 | * Set aggregate objects. |
||
78 | * |
||
79 | * @param $roleAggregate |
||
80 | * @param $resourceAggregate |
||
81 | */ |
||
82 | 26 | public function resetAggregate($roleAggregate, $resourceAggregate) |
|
96 | |||
97 | /** |
||
98 | * @return ResourceAggregateInterface |
||
99 | */ |
||
100 | 2 | public function getResourceAggregate() |
|
104 | |||
105 | /** |
||
106 | * @param ResourceAggregateInterface $resourceAggregate |
||
107 | */ |
||
108 | 4 | public function setResourceAggregate(ResourceAggregateInterface $resourceAggregate) |
|
112 | |||
113 | /** |
||
114 | * @return RoleAggregateInterface |
||
115 | */ |
||
116 | 2 | public function getRoleAggregate() |
|
120 | |||
121 | /** |
||
122 | * @param RoleAggregateInterface $roleAggregate |
||
123 | */ |
||
124 | 5 | public function setRoleAggregate(RoleAggregateInterface $roleAggregate) |
|
128 | |||
129 | /** |
||
130 | * @return mixed |
||
131 | */ |
||
132 | 4 | public function getId() |
|
136 | |||
137 | /** |
||
138 | * @param mixed $id |
||
139 | */ |
||
140 | 49 | public function setId($id = null) |
|
148 | |||
149 | /** |
||
150 | * @param RuleResult|null $ruleResult |
||
151 | * |
||
152 | * @return bool|null |
||
153 | */ |
||
154 | 32 | public function getAction(RuleResult $ruleResult = null) |
|
178 | |||
179 | /** |
||
180 | * @param mixed $action |
||
181 | */ |
||
182 | 40 | public function setAction($action) |
|
186 | |||
187 | /** |
||
188 | * Check owing Role & Resource and match its with $roleName & $resourceName; |
||
189 | * if match was found depending on action allow or deny access to $resourceName for $roleName. |
||
190 | * |
||
191 | * @param $needRuleName |
||
192 | * @param string $needRoleName |
||
193 | * @param string $needResourceName |
||
194 | * |
||
195 | * @return RuleResult|null null is returned if there is no matched Role & Resource in this rule. |
||
196 | * RuleResult otherwise. |
||
197 | */ |
||
198 | 27 | public function isAllowed($needRuleName, $needRoleName, $needResourceName) |
|
258 | |||
259 | /** |
||
260 | * Check if rule can be used. |
||
261 | * |
||
262 | * @param $neeRuleName |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | 25 | protected function isRuleMatched($neeRuleName) |
|
270 | |||
271 | /** |
||
272 | * @return string |
||
273 | */ |
||
274 | 29 | public function getName() |
|
278 | |||
279 | /** |
||
280 | * @param string $name |
||
281 | */ |
||
282 | 49 | public function setName($name) |
|
286 | |||
287 | /** |
||
288 | * @return Role |
||
289 | */ |
||
290 | 7 | public function getRole() |
|
294 | |||
295 | /** |
||
296 | * @param Role|null $role |
||
297 | */ |
||
298 | 37 | public function setRole(Role $role = null) |
|
302 | |||
303 | /** |
||
304 | * @return \SimpleAcl\Resource |
||
305 | */ |
||
306 | 30 | public function getResource() |
|
310 | |||
311 | /** |
||
312 | * @param \SimpleAcl\Resource $resource |
||
313 | */ |
||
314 | 37 | public function setResource(\SimpleAcl\Resource $resource = null) |
|
318 | |||
319 | /** |
||
320 | * Check if $role and $resource match to need role and resource. |
||
321 | * |
||
322 | * @param Role|null $role |
||
323 | * @param \SimpleAcl\Resource $resource |
||
324 | * @param string $needRoleName |
||
325 | * @param string $needResourceName |
||
326 | * @param $priority |
||
327 | * |
||
328 | * @return RuleResult|null |
||
329 | */ |
||
330 | 26 | protected function match(Role $role = null, \SimpleAcl\Resource $resource = null, $needRoleName, $needResourceName, $priority) |
|
350 | |||
351 | /** |
||
352 | * @return int |
||
353 | */ |
||
354 | 28 | public function getPriority() |
|
358 | |||
359 | /** |
||
360 | * @param int $priority |
||
361 | */ |
||
362 | 1 | public function setPriority($priority) |
|
366 | |||
367 | /** |
||
368 | * Creates an id for rule. |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | 49 | protected function generateId() |
|
378 | } |
||
379 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.