Complex classes like App 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 App, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class App |
||
20 | { |
||
21 | /** |
||
22 | * Reference to instantiated controller object. |
||
23 | * |
||
24 | * @var object |
||
25 | */ |
||
26 | protected static $instance = false; |
||
27 | |||
28 | /** |
||
29 | * Available configuration files |
||
30 | * |
||
31 | * @var object |
||
32 | */ |
||
33 | private $files; |
||
34 | |||
35 | /** |
||
36 | * System configuration |
||
37 | * |
||
38 | * @var object |
||
39 | */ |
||
40 | public $configuration; |
||
41 | |||
42 | /** |
||
43 | * System service management |
||
44 | * |
||
45 | * @var object |
||
46 | */ |
||
47 | private $services; |
||
48 | |||
49 | /** |
||
50 | * Events |
||
51 | */ |
||
52 | private static $events; |
||
53 | |||
54 | /** |
||
55 | * Return value from application |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $output = false; |
||
60 | |||
61 | /** |
||
62 | * Namespaced controller path |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $class; |
||
67 | |||
68 | /** |
||
69 | * Instantiated class object |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $instantiatedClass; |
||
74 | |||
75 | /** |
||
76 | * Module being accessed |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $module; |
||
81 | |||
82 | /** |
||
83 | * Controller being accessed |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $controller; |
||
88 | |||
89 | /** |
||
90 | * Method being accessed |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | private $method; |
||
95 | |||
96 | /** |
||
97 | * Params being passed |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | private $params; |
||
102 | |||
103 | /** |
||
104 | * Instantiated router class |
||
105 | * |
||
106 | * @var object |
||
107 | */ |
||
108 | private $router; |
||
109 | |||
110 | /** |
||
111 | * Instantiated request class |
||
112 | * |
||
113 | * @var object |
||
114 | */ |
||
115 | private $request; |
||
116 | |||
117 | /** |
||
118 | * Application bootstrap process |
||
119 | * |
||
120 | * The application registers the configuration in the app/config/core.php |
||
121 | * and then processes, and makes available the configured resources |
||
122 | */ |
||
123 | 28 | public function __construct() |
|
146 | |||
147 | /** |
||
148 | * Calls the proper shell for app execution |
||
149 | * |
||
150 | * @access private |
||
151 | */ |
||
152 | public function initialize() |
||
157 | |||
158 | /** |
||
159 | * App preparation cycle |
||
160 | */ |
||
161 | 28 | private function prepare() |
|
181 | |||
182 | 18 | private function prepareServices() |
|
195 | |||
196 | 3 | public function getService($service, $new = false, $options = []) |
|
209 | |||
210 | 18 | public function setService($service, $class) |
|
218 | |||
219 | /** |
||
220 | * @param mixed string with reference to config |
||
221 | * @return mixed bool or config values |
||
222 | */ |
||
223 | 28 | public function getConfiguration($config = null) |
|
246 | |||
247 | /** |
||
248 | * @param $config mixed null|string |
||
249 | * @param null|object|array optional array of configuration data |
||
250 | * |
||
251 | * @return bool |
||
252 | * @throws Exception\StateException |
||
253 | */ |
||
254 | 18 | public function setConfiguration($config = null, $configObject = null) |
|
263 | |||
264 | /** |
||
265 | * Registers the session object |
||
266 | * |
||
267 | * @access private |
||
268 | */ |
||
269 | 28 | private function registerSession() |
|
291 | |||
292 | /** |
||
293 | * Verifies the provided application request is a valid request |
||
294 | * |
||
295 | * @access private |
||
296 | */ |
||
297 | private function processRequest() |
||
311 | |||
312 | /** |
||
313 | * Processes the application request |
||
314 | * |
||
315 | * @access private |
||
316 | */ |
||
317 | private function start() |
||
332 | /** |
||
333 | * Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called. |
||
334 | * |
||
335 | * @param string $event name |
||
336 | * @param mixed $value the optional value to pass to each callback |
||
337 | * @param mixed $callback the method or function to call - FALSE to remove all callbacks for event |
||
338 | */ |
||
339 | |||
340 | public static function addEvent($event, $callback = false) |
||
349 | |||
350 | 28 | public function callEvent($event, $method = false, $arguments = []) |
|
372 | |||
373 | |||
374 | /** |
||
375 | * Prepare application return value into a string |
||
376 | * |
||
377 | * @access public |
||
378 | * @return string |
||
379 | */ |
||
380 | public function __toString() |
||
390 | |||
391 | |||
392 | /** |
||
393 | * Returns a reference of object once instantiated |
||
394 | * |
||
395 | * @access public |
||
396 | * @return object |
||
397 | */ |
||
398 | 28 | public static function getInstance() |
|
406 | } |
||
407 |
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..