Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Application extends Subjects |
||
14 | { |
||
15 | /** |
||
16 | * @var \BFW\Application|null $instance Application instance (Singleton) |
||
17 | */ |
||
18 | protected static $instance = null; |
||
19 | |||
20 | /** |
||
21 | * @var string $rootDir Path to application project directory |
||
22 | */ |
||
23 | protected $rootDir = ''; |
||
24 | |||
25 | /** |
||
26 | * @var \BFW\Config $config Config loaded for BFW application |
||
27 | */ |
||
28 | protected $config; |
||
29 | |||
30 | /** |
||
31 | * @var \BFW\Core\Options $options Option passed to this instance |
||
32 | */ |
||
33 | protected $options; |
||
34 | |||
35 | /** |
||
36 | * @var \Composer\Autoload\ClassLoader $composerLoader loader used by |
||
37 | * composer. |
||
38 | */ |
||
39 | protected $composerLoader; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var array[] $runPhases All steps to init Application |
||
44 | */ |
||
45 | protected $runPhases = []; |
||
46 | |||
47 | /** |
||
48 | * @var Object $memcached The class used to connect to memcache(d) server. |
||
49 | * The class used should be declared on config |
||
50 | */ |
||
51 | protected $memcached; |
||
52 | |||
53 | /** |
||
54 | * @var \BFW\Request $request Informations about the http request |
||
55 | */ |
||
56 | protected $request; |
||
57 | |||
58 | /** |
||
59 | * @var \BFW\Modules $modules System who manage all modules |
||
60 | */ |
||
61 | protected $modules; |
||
62 | |||
63 | /** |
||
64 | * @var \BFW\Core\Errors $errors System who manage personnel errors page |
||
65 | */ |
||
66 | protected $errors; |
||
67 | |||
68 | /** |
||
69 | * Constructor |
||
70 | * Initialize all components |
||
71 | * |
||
72 | * protected for Singleton pattern |
||
73 | * |
||
74 | * @param array $options Options passed to application |
||
75 | */ |
||
76 | protected function __construct($options) |
||
95 | |||
96 | /** |
||
97 | * get the Application instance (Singleton pattern) |
||
98 | * |
||
99 | * @param array $options Options passed to application |
||
100 | * |
||
101 | * @return \BFW\Application The current instance of this class |
||
102 | */ |
||
103 | public static function getInstance($options = []) |
||
104 | { |
||
105 | if (self::$instance === null) { |
||
106 | $calledClass = get_called_class(); //Autorize extends this class |
||
107 | |||
108 | self::$instance = new $calledClass($options); |
||
109 | } |
||
110 | |||
111 | return self::$instance; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Like getInstance. This is to have a keyword easier for users who want |
||
116 | * initialize the application |
||
117 | * |
||
118 | * @param array $options Options passed to application |
||
119 | * |
||
120 | * @return \BFW\Application The current instance of this class |
||
121 | */ |
||
122 | public static function init($options = []) |
||
126 | |||
127 | /** |
||
128 | * Getter to access to composerLoader attribut |
||
129 | * |
||
130 | * @return \Composer\Autoload\ClassLoader The composer class loader |
||
131 | */ |
||
132 | public function getComposerLoader() |
||
136 | |||
137 | /** |
||
138 | * Getter to access to a BFW config value |
||
139 | * |
||
140 | * @param string $configKey The key in the config file |
||
141 | * |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function getConfig($configKey) |
||
148 | |||
149 | /** |
||
150 | * Getter to access to a module |
||
151 | * |
||
152 | * @param string $moduleName The module name to access |
||
153 | * |
||
154 | * @return \BFW\Module |
||
155 | */ |
||
156 | public function getModule($moduleName) |
||
160 | |||
161 | /** |
||
162 | * Getter to access to a BFW option value |
||
163 | * |
||
164 | * @param string $optionKey The key for the option |
||
165 | * |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function getOption($optionKey) |
||
172 | |||
173 | /** |
||
174 | * Getter to access to Request instance |
||
175 | * |
||
176 | * @return \BFW\Request |
||
177 | */ |
||
178 | public function getRequest() |
||
182 | |||
183 | /** |
||
184 | * Initialize attribute options with the class \BFW\Core\Options |
||
185 | * |
||
186 | * @param array $options The option passed when initialize this class |
||
187 | */ |
||
188 | protected function initOptions($options) |
||
198 | |||
199 | /** |
||
200 | * Initialize all constants used by framework |
||
201 | * Use helper Constants::create to allow override of constants |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | protected function initConstants() |
||
221 | |||
222 | /** |
||
223 | * Initialize composer loader |
||
224 | * Get composerLoader instance |
||
225 | * Call addComposerNamespaces method to add Application namespace |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | protected function initComposerLoader() |
||
234 | |||
235 | /** |
||
236 | * Initialize attribute config with \BFW\Config instance |
||
237 | * The config class will search all file in "bfw" directory and load files |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | protected function initConfig() |
||
246 | |||
247 | /** |
||
248 | * Initialize request attribute with \BFW\Request class |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | protected function initRequest() |
||
256 | |||
257 | /** |
||
258 | * Initiliaze php session if option "runSession" is not (bool) false |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | protected function initSession() |
||
274 | |||
275 | /** |
||
276 | * Initialize errors attribute with \BFW\Core\Errors class |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | protected function initErrors() |
||
284 | |||
285 | /** |
||
286 | * Initialize modules attribut with \BFW\Modules class |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | protected function initModules() |
||
294 | |||
295 | /** |
||
296 | * Add namespace used by a BFW Application to composer |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | protected function addComposerNamespaces() |
||
306 | |||
307 | /** |
||
308 | * Declare all step to run application |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | protected function declareRunPhases() |
||
322 | |||
323 | /** |
||
324 | * Run the application |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function run() |
||
343 | |||
344 | /** |
||
345 | * Connect to memcache(d) server with the class declared in config file |
||
346 | * |
||
347 | * @return Object |
||
348 | * |
||
349 | * @throws Exception If memcached is enabled but no class is define. Or if |
||
350 | * The class declared in config is not found. |
||
351 | */ |
||
352 | protected function loadMemcached() |
||
371 | |||
372 | /** |
||
373 | * Read all directory in modules directory and add the module to Modules |
||
374 | * class. |
||
375 | * Generate the load tree. |
||
376 | * Not initialize modules ! |
||
377 | * |
||
378 | * @return void |
||
379 | */ |
||
380 | protected function readAllModules() |
||
396 | |||
397 | /** |
||
398 | * Load modules define in config bfw file. |
||
399 | * It's module for controller, router, database and template only. |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | protected function loadAllCoreModules() |
||
416 | |||
417 | /** |
||
418 | * Load all modules (except core). |
||
419 | * Get the load tree, read this and load modules in order declared in tree. |
||
420 | * |
||
421 | * @return void |
||
422 | */ |
||
423 | protected function loadAllAppModules() |
||
435 | |||
436 | /** |
||
437 | * Load a module |
||
438 | * |
||
439 | * @param string $moduleName The module's name to load |
||
440 | * |
||
441 | * @return void |
||
442 | */ |
||
443 | protected function loadModule($moduleName) |
||
448 | |||
449 | /** |
||
450 | * Run the cli file if we're in cli mode |
||
451 | * |
||
452 | * @return void |
||
453 | * |
||
454 | * @throws Exception If no file is specified or if the file not exist. |
||
455 | */ |
||
456 | protected function runCliFile() |
||
479 | } |
||
480 |