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) |
|
235 | |||
236 | /** |
||
237 | * Check if rule can be used. |
||
238 | * |
||
239 | * @param $neeRuleName |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 25 | protected function isRuleMatched($neeRuleName) |
|
247 | |||
248 | /** |
||
249 | * @return string |
||
250 | */ |
||
251 | 29 | public function getName() |
|
255 | |||
256 | /** |
||
257 | * @param string $name |
||
258 | */ |
||
259 | 49 | public function setName($name) |
|
263 | |||
264 | /** |
||
265 | * @return Role |
||
266 | */ |
||
267 | 7 | public function getRole() |
|
271 | |||
272 | /** |
||
273 | * @param Role|null $role |
||
274 | */ |
||
275 | 37 | public function setRole(Role $role = null) |
|
279 | |||
280 | /** |
||
281 | * @return \SimpleAcl\Resource |
||
282 | */ |
||
283 | 30 | public function getResource() |
|
287 | |||
288 | /** |
||
289 | * @param \SimpleAcl\Resource $resource |
||
290 | */ |
||
291 | 37 | public function setResource(\SimpleAcl\Resource $resource = null) |
|
295 | |||
296 | /** |
||
297 | * Check if $role and $resource match to need role and resource. |
||
298 | * |
||
299 | * @param Role|null $role |
||
300 | * @param \SimpleAcl\Resource $resource |
||
301 | * @param string $needRoleName |
||
302 | * @param string $needResourceName |
||
303 | * @param $priority |
||
304 | * |
||
305 | * @return RuleResult|null |
||
306 | */ |
||
307 | 26 | protected function match(Role $role = null, \SimpleAcl\Resource $resource = null, $needRoleName, $needResourceName, $priority) |
|
327 | |||
328 | /** |
||
329 | * @return int |
||
330 | */ |
||
331 | 28 | public function getPriority() |
|
335 | |||
336 | /** |
||
337 | * @param int $priority |
||
338 | */ |
||
339 | 1 | public function setPriority($priority) |
|
343 | |||
344 | /** |
||
345 | * Creates an id for rule. |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | 49 | protected function generateId() |
|
355 | } |
||
356 |
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.