Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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_MEMCACHED_NOT_IMPLEMENT_INTERFACE Exception code the |
||
29 | * memcache(d) class not implement the interface. |
||
30 | */ |
||
31 | const ERR_MEMCACHED_NOT_IMPLEMENT_INTERFACE = 1301003; |
||
32 | |||
33 | /** |
||
34 | * @var \BFW\Application|null $instance Application instance (Singleton) |
||
35 | */ |
||
36 | protected static $instance = null; |
||
37 | |||
38 | /** |
||
39 | * @var string $rootDir Path to the application project directory |
||
40 | */ |
||
41 | protected $rootDir = ''; |
||
42 | |||
43 | /** |
||
44 | * @var \BFW\Config $config Config's instance for BFW |
||
45 | */ |
||
46 | protected $config; |
||
47 | |||
48 | /** |
||
49 | * @var \BFW\Core\Options $options Option's instance for the core |
||
50 | */ |
||
51 | protected $options; |
||
52 | |||
53 | /** |
||
54 | * @var \Composer\Autoload\ClassLoader $composerLoader Loader used by |
||
55 | * composer. |
||
56 | */ |
||
57 | protected $composerLoader; |
||
58 | |||
59 | /** |
||
60 | * @var array[] $runSteps All steps used for run the application |
||
61 | */ |
||
62 | protected $runSteps = []; |
||
63 | |||
64 | /** |
||
65 | * @var Object $memcached The class used to connect to memcache(d) server. |
||
66 | * The class name should be declared into config file. |
||
67 | */ |
||
68 | protected $memcached; |
||
69 | |||
70 | /** |
||
71 | * @var \BFW\Request $request Informations about the http request |
||
72 | */ |
||
73 | protected $request; |
||
74 | |||
75 | /** |
||
76 | * @var \BFW\Modules $modules System who manage all modules |
||
77 | */ |
||
78 | protected $modules; |
||
79 | |||
80 | /** |
||
81 | * @var \BFW\Core\Errors $errors System who manage personal errors page |
||
82 | */ |
||
83 | protected $errors; |
||
84 | |||
85 | /** |
||
86 | * @var \BFW\Core\Cli $cli Cli system |
||
87 | */ |
||
88 | protected $cli; |
||
89 | |||
90 | /** |
||
91 | * @var \BFW\SubjectList $subjectList System who manage subjects list |
||
92 | */ |
||
93 | protected $subjectList; |
||
94 | |||
95 | /** |
||
96 | * @var \stdClass $ctrlRouterInfos Infos from router for controller system |
||
97 | */ |
||
98 | protected $ctrlRouterInfos; |
||
99 | |||
100 | /** |
||
101 | * Constructor |
||
102 | * Init output buffering |
||
103 | * Declare run steps |
||
104 | * Set UTF-8 header |
||
105 | * |
||
106 | * protected for Singleton pattern |
||
107 | */ |
||
108 | protected function __construct() |
||
121 | |||
122 | /** |
||
123 | * Get the Application instance (Singleton pattern) |
||
124 | * |
||
125 | * @return \BFW\Application The current instance of this class |
||
126 | */ |
||
127 | View Code Duplication | public static function getInstance() |
|
136 | |||
137 | /** |
||
138 | * Getter to access to cli property |
||
139 | * |
||
140 | * @return \BFW\Core\Cli |
||
141 | */ |
||
142 | public function getCli() |
||
146 | |||
147 | /** |
||
148 | * Getter to access to composerLoader property |
||
149 | * |
||
150 | * @return \Composer\Autoload\ClassLoader The composer class loader |
||
151 | */ |
||
152 | public function getComposerLoader() |
||
156 | |||
157 | /** |
||
158 | * Getter to access to the config instance |
||
159 | * |
||
160 | * @return \BFW\Config |
||
161 | */ |
||
162 | public function getConfig() |
||
166 | |||
167 | /** |
||
168 | * Getter to access to the errors instance |
||
169 | * |
||
170 | * @return \BFW\Errors |
||
171 | */ |
||
172 | public function getErrors() |
||
176 | |||
177 | /** |
||
178 | * Getter to access to the ctrlRouterInfos property |
||
179 | * |
||
180 | * @return null|\stdClass |
||
181 | */ |
||
182 | public function getCtrlRouterInfos() |
||
186 | |||
187 | /** |
||
188 | * Getter to access to memcache instance |
||
189 | * |
||
190 | * @return Object|null |
||
191 | */ |
||
192 | public function getMemcached() |
||
196 | |||
197 | /** |
||
198 | * Getter to access to modules system |
||
199 | * |
||
200 | * @return \BFW\Modules |
||
201 | */ |
||
202 | public function getModules() |
||
206 | |||
207 | /** |
||
208 | * Getter to access to a module |
||
209 | * |
||
210 | * @param string $moduleName The module name to access |
||
211 | * |
||
212 | * @return \BFW\Module |
||
213 | */ |
||
214 | public function getModuleForName($moduleName) |
||
218 | |||
219 | /** |
||
220 | * Getter to access to the options system |
||
221 | * |
||
222 | * @return mixed |
||
223 | */ |
||
224 | public function getOptions() |
||
228 | |||
229 | /** |
||
230 | * Getter to access to the Request instance |
||
231 | * |
||
232 | * @return \BFW\Request |
||
233 | */ |
||
234 | public function getRequest() |
||
238 | |||
239 | /** |
||
240 | * Getter to access to the run step array |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function getRunSteps() |
||
248 | |||
249 | /** |
||
250 | * Getter to access to the subjects list |
||
251 | * |
||
252 | * @return \BFW\SubjectList |
||
253 | */ |
||
254 | public function getSubjectList() |
||
258 | |||
259 | /** |
||
260 | * Initialize all components |
||
261 | * |
||
262 | * @param array $options Options passed to application |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function initSystem($options) |
||
282 | |||
283 | /** |
||
284 | * Initialize options with the class \BFW\Core\Options |
||
285 | * |
||
286 | * @param array $options The option passed when initialize this class |
||
287 | */ |
||
288 | protected function initOptions($options) |
||
302 | |||
303 | /** |
||
304 | * Initialize all constants used by framework |
||
305 | * Use helper Constants::create to allow override of constants |
||
306 | * |
||
307 | * @return void |
||
308 | */ |
||
309 | protected function initConstants() |
||
325 | |||
326 | /** |
||
327 | * Initialize composer loader |
||
328 | * Obtain the composerLoader instance |
||
329 | * Call addComposerNamespaces method to add Application namespaces |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | protected function initComposerLoader() |
||
340 | |||
341 | /** |
||
342 | * Initialize the subjectList object |
||
343 | * |
||
344 | * @return void |
||
345 | */ |
||
346 | protected function initSubjectList() |
||
350 | |||
351 | /** |
||
352 | * Initialize the property config with \BFW\Config instance |
||
353 | * The config class will search all file in "bfw" directory and load files |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | protected function initConfig() |
||
362 | |||
363 | /** |
||
364 | * Initialize request property with the \BFW\Request class |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | protected function initRequest() |
||
373 | |||
374 | /** |
||
375 | * Initiliaze php session if option "runSession" is not (bool) false |
||
376 | * |
||
377 | * @return void |
||
378 | */ |
||
379 | protected function initSession() |
||
391 | |||
392 | /** |
||
393 | * Initialize errors property with the \BFW\Core\Errors class |
||
394 | * |
||
395 | * @return void |
||
396 | */ |
||
397 | protected function initErrors() |
||
401 | |||
402 | /** |
||
403 | * Initialize cli property with the \BFW\Core\Cli class |
||
404 | * |
||
405 | * @return void |
||
406 | */ |
||
407 | protected function initCli() |
||
411 | |||
412 | /** |
||
413 | * Initialize taskers |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | protected function initRunTasks() |
||
441 | |||
442 | /** |
||
443 | * Initialize modules property with the \BFW\Modules class |
||
444 | * |
||
445 | * @return void |
||
446 | */ |
||
447 | protected function initModules() |
||
451 | |||
452 | /** |
||
453 | * Add namespaces used by a BFW Application to composer |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | protected function addComposerNamespaces() |
||
463 | |||
464 | /** |
||
465 | * Declare all steps to run the application |
||
466 | * |
||
467 | * @return void |
||
468 | */ |
||
469 | protected function declareRunSteps() |
||
481 | |||
482 | /** |
||
483 | * Run the application |
||
484 | * |
||
485 | * @return void |
||
486 | */ |
||
487 | public function run() |
||
494 | |||
495 | /** |
||
496 | * Connect to memcache(d) server with the class declared in config file |
||
497 | * |
||
498 | * @return Object |
||
499 | * |
||
500 | * @throws Exception If memcached is enabled but no class is define. Or if |
||
501 | * The class declared into the config is not found. |
||
502 | */ |
||
503 | protected function loadMemcached() |
||
537 | |||
538 | /** |
||
539 | * Read all directories in modules directory and add each module to Modules |
||
540 | * class. |
||
541 | * Generate the load tree. |
||
542 | * Not initialize modules ! |
||
543 | * |
||
544 | * @return void |
||
545 | */ |
||
546 | protected function loadAllModules() |
||
563 | |||
564 | /** |
||
565 | * Load core modules defined into config bfw file. |
||
566 | * Only module for controller, router, database and template only. |
||
567 | * |
||
568 | * @return void |
||
569 | */ |
||
570 | protected function runAllCoreModules() |
||
583 | |||
584 | /** |
||
585 | * Load all modules (except core). |
||
586 | * Get the load tree, read him and load all modules with the order |
||
587 | * declared into the tree. |
||
588 | * |
||
589 | * @return void |
||
590 | */ |
||
591 | protected function runAllAppModules() |
||
603 | |||
604 | /** |
||
605 | * Load a module |
||
606 | * |
||
607 | * @param string $moduleName The module's name to load |
||
608 | * |
||
609 | * @return void |
||
610 | */ |
||
611 | protected function runModule($moduleName) |
||
618 | |||
619 | /** |
||
620 | * Run the cli file if we're in cli mode |
||
621 | * |
||
622 | * @return void |
||
623 | * |
||
624 | * @throws Exception If no file is specified or if the file not exist. |
||
625 | */ |
||
626 | protected function runCliFile() |
||
638 | |||
639 | /** |
||
640 | * Create a new observer to controller and router module. |
||
641 | * |
||
642 | * @return void |
||
643 | */ |
||
644 | protected function initCtrlRouterLink() |
||
669 | |||
670 | /** |
||
671 | * Execute the ctrlRouter task to find the route and the controller. |
||
672 | * If nothing is found (context object), return an 404 error. |
||
673 | * Not executed in cli. |
||
674 | * |
||
675 | * @return void |
||
676 | */ |
||
677 | protected function runCtrlRouterLink() |
||
689 | } |
||
690 |
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.