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 | public function __construct() |
||
144 | |||
145 | /** |
||
146 | * Calls the proper shell for app execution |
||
147 | * |
||
148 | * @access private |
||
149 | */ |
||
150 | public function initialize() |
||
165 | |||
166 | /** |
||
167 | * App preparation cycle |
||
168 | */ |
||
169 | private function prepare() |
||
189 | |||
190 | private function prepareServices() |
||
203 | |||
204 | public function getService($service, $new = false, $options = []) |
||
217 | |||
218 | public function setService($service, $class) |
||
226 | |||
227 | /** |
||
228 | * @param mixed string with reference to config |
||
229 | * @return mixed bool or config values |
||
230 | */ |
||
231 | 28 | public function getConfiguration($config = null) |
|
254 | |||
255 | /** |
||
256 | * @param $config mixed null|string |
||
257 | * @param null|object|array optional array of configuration data |
||
258 | * |
||
259 | * @return bool |
||
260 | * @throws Exception\StateException |
||
261 | */ |
||
262 | 18 | public function setConfiguration($config = null, $configObject = null) |
|
271 | |||
272 | /** |
||
273 | * Registers the session object |
||
274 | * |
||
275 | * @access private |
||
276 | */ |
||
277 | private function registerSession() |
||
299 | |||
300 | /** |
||
301 | * Verifies the provided application request is a valid request |
||
302 | * |
||
303 | * @access private |
||
304 | */ |
||
305 | private function processRequest() |
||
319 | |||
320 | /** |
||
321 | * Processes the application request |
||
322 | * |
||
323 | * @access private |
||
324 | */ |
||
325 | private function start() |
||
340 | /** |
||
341 | * Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called. |
||
342 | * |
||
343 | * @param string $event name |
||
344 | * @param mixed $value the optional value to pass to each callback |
||
345 | * @param mixed $callback the method or function to call - FALSE to remove all callbacks for event |
||
346 | */ |
||
347 | |||
348 | public static function addEvent($event, $callback = false) |
||
357 | |||
358 | public function callEvent($event, $method = false, $arguments = []) |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Prepare application return value into a string |
||
384 | * |
||
385 | * @access public |
||
386 | * @return string |
||
387 | */ |
||
388 | public function __toString() |
||
398 | |||
399 | |||
400 | /** |
||
401 | * Returns a reference of object once instantiated |
||
402 | * |
||
403 | * @access public |
||
404 | * @return object |
||
405 | */ |
||
406 | 28 | public static function getInstance() |
|
414 | } |
||
415 |
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..