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 |
||
14 | { |
||
15 | /** |
||
16 | * @const ERR_MEMCACHED_NOT_CLASS_DEFINED Exception code if memcache(d) is |
||
17 | * enabled but the class to use is not defined. |
||
18 | */ |
||
19 | const ERR_MEMCACHED_NOT_CLASS_DEFINED = 1301001; |
||
20 | |||
21 | /** |
||
22 | * @const ERR_MEMCACHED_CLASS_NOT_FOUND Exception code if the memcache(d) |
||
23 | * class is not found. |
||
24 | */ |
||
25 | const ERR_MEMCACHED_CLASS_NOT_FOUND = 1301002; |
||
26 | |||
27 | /** |
||
28 | * @const ERR_CLI_NO_FILE_SPECIFIED Exception code if the cli file to run |
||
29 | * is not specified |
||
30 | */ |
||
31 | const ERR_CLI_NO_FILE_SPECIFIED = 1301003; |
||
32 | |||
33 | /** |
||
34 | * @const ERR_CLI_FILE_NOT_FOUND Exception code if the cli file to run is |
||
35 | * not found. |
||
36 | */ |
||
37 | const ERR_CLI_FILE_NOT_FOUND = 1301004; |
||
38 | |||
39 | /** |
||
40 | * @var \BFW\Application|null $instance Application instance (Singleton) |
||
41 | */ |
||
42 | protected static $instance = null; |
||
43 | |||
44 | /** |
||
45 | * @var string $rootDir Path to the application project directory |
||
46 | */ |
||
47 | protected $rootDir = ''; |
||
48 | |||
49 | /** |
||
50 | * @var \BFW\Config $config Config's instance for BFW |
||
51 | */ |
||
52 | protected $config; |
||
53 | |||
54 | /** |
||
55 | * @var \BFW\Core\Options $options Option's instance for the core |
||
56 | */ |
||
57 | protected $options; |
||
58 | |||
59 | /** |
||
60 | * @var \Composer\Autoload\ClassLoader $composerLoader Loader used by |
||
61 | * composer. |
||
62 | */ |
||
63 | protected $composerLoader; |
||
64 | |||
65 | /** |
||
66 | * @var array[] $runSteps All steps used for run the application |
||
67 | */ |
||
68 | protected $runSteps = []; |
||
69 | |||
70 | /** |
||
71 | * @var Object $memcached The class used to connect to memcache(d) server. |
||
72 | * The class name should be declared into config file. |
||
73 | */ |
||
74 | protected $memcached; |
||
75 | |||
76 | /** |
||
77 | * @var \BFW\Request $request Informations about the http request |
||
78 | */ |
||
79 | protected $request; |
||
80 | |||
81 | /** |
||
82 | * @var \BFW\Modules $modules System who manage all modules |
||
83 | */ |
||
84 | protected $modules; |
||
85 | |||
86 | /** |
||
87 | * @var \BFW\Core\Errors $errors System who manage personal errors page |
||
88 | */ |
||
89 | protected $errors; |
||
90 | |||
91 | /** |
||
92 | * @var \BFW\Subjects[] $subjectsList List of all subjects declared |
||
93 | */ |
||
94 | protected $subjectsList; |
||
95 | |||
96 | /** |
||
97 | * @var \stdClass $ctrlRouterInfos Infos from router for controller system |
||
98 | */ |
||
99 | protected $ctrlRouterInfos; |
||
100 | |||
101 | /** |
||
102 | * Constructor |
||
103 | * Init output buffering |
||
104 | * Declare run steps |
||
105 | * Set UTF-8 header |
||
106 | * |
||
107 | * protected for Singleton pattern |
||
108 | */ |
||
109 | protected function __construct() |
||
122 | |||
123 | /** |
||
124 | * Get the Application instance (Singleton pattern) |
||
125 | * |
||
126 | * @param array $options Options passed to application |
||
127 | * |
||
128 | * @return \BFW\Application The current instance of this class |
||
129 | */ |
||
130 | public static function getInstance($options = []) |
||
140 | |||
141 | /** |
||
142 | * Like getInstance. This is to have a keyword easier for users who want |
||
143 | * initialize the application |
||
144 | * |
||
145 | * @param array $options Options passed to application |
||
146 | * |
||
147 | * @return \BFW\Application The current instance of this class |
||
148 | */ |
||
149 | public static function init($options = []) |
||
154 | |||
155 | /** |
||
156 | * Getter to access to composerLoader property |
||
157 | * |
||
158 | * @return \Composer\Autoload\ClassLoader The composer class loader |
||
159 | */ |
||
160 | public function getComposerLoader() |
||
164 | |||
165 | /** |
||
166 | * Getter to access to the config instance |
||
167 | * |
||
168 | * @return \BFW\Config |
||
169 | */ |
||
170 | public function getConfig() |
||
174 | |||
175 | /** |
||
176 | * Getter to access to memcache instance |
||
177 | * |
||
178 | * @return Object|null |
||
179 | */ |
||
180 | public function getMemcached() |
||
184 | |||
185 | /** |
||
186 | * Getter to access to a module |
||
187 | * |
||
188 | * @param string $moduleName The module name to access |
||
189 | * |
||
190 | * @return \BFW\Module |
||
191 | */ |
||
192 | public function getModule($moduleName) |
||
196 | |||
197 | /** |
||
198 | * Getter to access to an option's value |
||
199 | * |
||
200 | * @param string $optionKey The key for the option |
||
201 | * |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function getOption($optionKey) |
||
208 | |||
209 | /** |
||
210 | * Getter to access to the Request instance |
||
211 | * |
||
212 | * @return \BFW\Request |
||
213 | */ |
||
214 | public function getRequest() |
||
218 | |||
219 | /** |
||
220 | * Getter to access to the run step array |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getRunSteps() |
||
228 | |||
229 | /** |
||
230 | * Initialize all components |
||
231 | * |
||
232 | * @param array $options Options passed to application |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | protected function initSystem($options) |
||
248 | |||
249 | /** |
||
250 | * Initialize options with the class \BFW\Core\Options |
||
251 | * |
||
252 | * @param array $options The option passed when initialize this class |
||
253 | */ |
||
254 | protected function initOptions($options) |
||
264 | |||
265 | /** |
||
266 | * Initialize all constants used by framework |
||
267 | * Use helper Constants::create to allow override of constants |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | protected function initConstants() |
||
287 | |||
288 | /** |
||
289 | * Initialize composer loader |
||
290 | * Obtain the composerLoader instance |
||
291 | * Call addComposerNamespaces method to add Application namespaces |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function initComposerLoader() |
||
302 | |||
303 | /** |
||
304 | * Initialize the property config with \BFW\Config instance |
||
305 | * The config class will search all file in "bfw" directory and load files |
||
306 | * |
||
307 | * @return void |
||
308 | */ |
||
309 | protected function initConfig() |
||
314 | |||
315 | /** |
||
316 | * Initialize request property with the \BFW\Request class |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | protected function initRequest() |
||
324 | |||
325 | /** |
||
326 | * Initiliaze php session if option "runSession" is not (bool) false |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | protected function initSession() |
||
342 | |||
343 | /** |
||
344 | * Initialize errors property with the \BFW\Core\Errors class |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | protected function initErrors() |
||
352 | |||
353 | /** |
||
354 | * Initialize taskers |
||
355 | * |
||
356 | * @return void |
||
357 | */ |
||
358 | protected function initRunTasks() |
||
375 | |||
376 | /** |
||
377 | * Initialize modules property with the \BFW\Modules class |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | protected function initModules() |
||
385 | |||
386 | /** |
||
387 | * Add namespaces used by a BFW Application to composer |
||
388 | * |
||
389 | * @return void |
||
390 | */ |
||
391 | protected function addComposerNamespaces() |
||
397 | |||
398 | /** |
||
399 | * Declare all steps to run the application |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | protected function declareRunSteps() |
||
414 | |||
415 | /** |
||
416 | * Run the application |
||
417 | * |
||
418 | * @return void |
||
419 | */ |
||
420 | public function run() |
||
427 | |||
428 | /** |
||
429 | * Connect to memcache(d) server with the class declared in config file |
||
430 | * |
||
431 | * @return Object |
||
432 | * |
||
433 | * @throws Exception If memcached is enabled but no class is define. Or if |
||
434 | * The class declared into the config is not found. |
||
435 | */ |
||
436 | protected function loadMemcached() |
||
461 | |||
462 | /** |
||
463 | * Read all directories in modules directory and add each module to Modules |
||
464 | * class. |
||
465 | * Generate the load tree. |
||
466 | * Not initialize modules ! |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | protected function readAllModules() |
||
487 | |||
488 | /** |
||
489 | * Load core modules defined into config bfw file. |
||
490 | * Only module for controller, router, database and template only. |
||
491 | * |
||
492 | * @return void |
||
493 | */ |
||
494 | protected function loadAllCoreModules() |
||
507 | |||
508 | /** |
||
509 | * Load all modules (except core). |
||
510 | * Get the load tree, read him and load all modules with the order |
||
511 | * declared into the tree. |
||
512 | * |
||
513 | * @return void |
||
514 | */ |
||
515 | protected function loadAllAppModules() |
||
527 | |||
528 | /** |
||
529 | * Load a module |
||
530 | * |
||
531 | * @param string $moduleName The module's name to load |
||
532 | * |
||
533 | * @return void |
||
534 | */ |
||
535 | protected function loadModule($moduleName) |
||
542 | |||
543 | /** |
||
544 | * Run the cli file if we're in cli mode |
||
545 | * |
||
546 | * @return void |
||
547 | * |
||
548 | * @throws Exception If no file is specified or if the file not exist. |
||
549 | */ |
||
550 | protected function runCliFile() |
||
579 | |||
580 | /** |
||
581 | * Create a new observer to controller and router module. |
||
582 | * |
||
583 | * @return void |
||
584 | */ |
||
585 | protected function initCtrlRouterLink() |
||
612 | |||
613 | /** |
||
614 | * Add a new subject to the list |
||
615 | * |
||
616 | * @param \BFW\Subjects $subject The new subject to add |
||
617 | * @param string|null $subjectName (default null) The subject name, if null, |
||
618 | * the name of the class will be used |
||
619 | * |
||
620 | * @return void |
||
621 | */ |
||
622 | public function addSubject(\BFW\Subjects $subject, $subjectName = null) |
||
630 | |||
631 | /** |
||
632 | * Obtain a subject object with this name |
||
633 | * |
||
634 | * @param string $subjectName The name of the subject object |
||
635 | * |
||
636 | * @return \BFW\Subjects |
||
637 | * |
||
638 | * @throws Exception If the subject name not exist |
||
639 | */ |
||
640 | public function getSubjectForName($subjectName) |
||
651 | } |
||
652 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.