Complex classes like UserPermissionsComponent 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 UserPermissionsComponent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class UserPermissionsComponent extends Component { |
||
13 | |||
14 | /** |
||
15 | * Controller name |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $controller = null; |
||
20 | |||
21 | /** |
||
22 | * Session |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | public $session = null; |
||
27 | |||
28 | /** |
||
29 | * Components array |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public $components = ['Flash']; |
||
34 | |||
35 | private $actions; |
||
36 | |||
37 | private $allow; |
||
38 | |||
39 | private $redirect; |
||
40 | |||
41 | private $params; |
||
42 | |||
43 | private $message; |
||
44 | |||
45 | private $userType; |
||
46 | |||
47 | private $action; |
||
48 | |||
49 | /** |
||
50 | * Boolean value which holds the configuration for the behavior in case of |
||
51 | * missing handlers. |
||
52 | */ |
||
53 | private $throwEx; |
||
54 | |||
55 | /** |
||
56 | * Initialization to get controller variable |
||
57 | * |
||
58 | * For this component available settings: |
||
59 | * bool throwEx - default false - if set to true, an exception will be |
||
60 | * thrown, if a handler is about to be called but does not exist. |
||
61 | * |
||
62 | * @param array $config Configuration array for the component. |
||
63 | */ |
||
64 | 7 | public function initialize(array $config) |
|
80 | |||
81 | /** |
||
82 | * Initialization to get controller variable |
||
83 | * |
||
84 | * @param array $rules Array of rules for permissions. |
||
85 | * @return bool false if user / group doesn't have permission, true if has permission |
||
86 | */ |
||
87 | 7 | public function allow ($rules) { |
|
97 | |||
98 | 7 | private function setUserValues() |
|
106 | |||
107 | 7 | private function bindConfiguration(array $rules) |
|
140 | |||
141 | 7 | private function applyGroupsRules(array $rules) |
|
153 | |||
154 | 7 | private function searchForApplyGroupRules($key) |
|
164 | |||
165 | 7 | private function notInArrayAction() |
|
169 | |||
170 | 7 | private function applyViewsRules(array $rules) |
|
178 | |||
179 | 4 | private function searchForApplyViewRules($key, $value) |
|
189 | |||
190 | 4 | private function checkForHandler($controller, $handler) |
|
208 | |||
209 | 4 | private function redirectIfIsSet() |
|
220 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..