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 | 6 | 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 | 6 | public function allow ($rules) { |
|
| 97 | |||
| 98 | 6 | private function setUserValues() |
|
| 106 | |||
| 107 | 6 | private function bindConfiguration(array $rules) |
|
| 140 | |||
| 141 | 6 | private function applyGroupsRules(array $rules) |
|
| 153 | |||
| 154 | 6 | private function searchForApplyGroupRules($key) |
|
| 164 | |||
| 165 | 6 | private function notInArrayAction() |
|
| 169 | |||
| 170 | 6 | private function applyViewsRules(array $rules) |
|
| 178 | |||
| 179 | 3 | private function searchForApplyViewRules($key, $value) |
|
| 191 | |||
| 192 | 3 | private function checkForHandler($controller, $handler) |
|
| 193 | { |
||
| 194 | 3 | if(!method_exists($controller, $handler)) { |
|
| 195 | 1 | $msg = sprintf( |
|
| 196 | 1 | "Controller %s=%s has no method called '%s'", |
|
| 197 | 1 | is_object($controller) ? "class" : "type", |
|
| 198 | 1 | is_object($controller) ? get_class($controller) : gettype($controller), |
|
| 199 | 1 | $handler |
|
| 200 | ); |
||
| 201 | 1 | Log::write("debug", $msg); |
|
| 202 | 1 | if($this->throwEx) { |
|
| 203 | throw new MissingHandlerException($msg); |
||
| 204 | } |
||
| 205 | 1 | return false; |
|
| 206 | } |
||
| 207 | |||
| 208 | 2 | return true; |
|
| 209 | } |
||
| 210 | |||
| 211 | 3 | private function redirectIfIsSet() |
|
| 222 | } |
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..