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 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 | * Init output buffering |
||
71 | * Declare run steps |
||
72 | * Set UTF-8 header |
||
73 | * |
||
74 | * protected for Singleton pattern |
||
75 | */ |
||
76 | protected function __construct() |
||
86 | |||
87 | /** |
||
88 | * get the Application instance (Singleton pattern) |
||
89 | * |
||
90 | * @param array $options Options passed to application |
||
91 | * |
||
92 | * @return \BFW\Application The current instance of this class |
||
93 | */ |
||
94 | public static function getInstance($options = []) |
||
104 | |||
105 | /** |
||
106 | * Like getInstance. This is to have a keyword easier for users who want |
||
107 | * initialize the application |
||
108 | * |
||
109 | * @param array $options Options passed to application |
||
110 | * |
||
111 | * @return \BFW\Application The current instance of this class |
||
112 | */ |
||
113 | public static function init($options = []) |
||
118 | |||
119 | /** |
||
120 | * Getter to access to composerLoader attribut |
||
121 | * |
||
122 | * @return \Composer\Autoload\ClassLoader The composer class loader |
||
123 | */ |
||
124 | public function getComposerLoader() |
||
128 | |||
129 | /** |
||
130 | * Getter to access to a BFW config value |
||
131 | * |
||
132 | * @param string $configKey The key in the config file |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function getConfig($configKey) |
||
140 | |||
141 | /** |
||
142 | * Getter to access to memcache instance |
||
143 | * |
||
144 | * @return Object|null |
||
145 | */ |
||
146 | public function getMemcached() |
||
150 | |||
151 | /** |
||
152 | * Getter to access to a module |
||
153 | * |
||
154 | * @param string $moduleName The module name to access |
||
155 | * |
||
156 | * @return \BFW\Module |
||
157 | */ |
||
158 | public function getModule($moduleName) |
||
162 | |||
163 | /** |
||
164 | * Getter to access to a BFW option value |
||
165 | * |
||
166 | * @param string $optionKey The key for the option |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function getOption($optionKey) |
||
174 | |||
175 | /** |
||
176 | * Getter to access to Request instance |
||
177 | * |
||
178 | * @return \BFW\Request |
||
179 | */ |
||
180 | public function getRequest() |
||
184 | |||
185 | /** |
||
186 | * Initialize all components |
||
187 | * |
||
188 | * @param array $options Options passed to application |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | protected function initSystem($options) |
||
203 | |||
204 | /** |
||
205 | * Initialize attribute options with the class \BFW\Core\Options |
||
206 | * |
||
207 | * @param array $options The option passed when initialize this class |
||
208 | */ |
||
209 | protected function initOptions($options) |
||
219 | |||
220 | /** |
||
221 | * Initialize all constants used by framework |
||
222 | * Use helper Constants::create to allow override of constants |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | protected function initConstants() |
||
242 | |||
243 | /** |
||
244 | * Initialize composer loader |
||
245 | * Get composerLoader instance |
||
246 | * Call addComposerNamespaces method to add Application namespace |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | protected function initComposerLoader() |
||
255 | |||
256 | /** |
||
257 | * Initialize attribute config with \BFW\Config instance |
||
258 | * The config class will search all file in "bfw" directory and load files |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | protected function initConfig() |
||
267 | |||
268 | /** |
||
269 | * Initialize request attribute with \BFW\Request class |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function initRequest() |
||
277 | |||
278 | /** |
||
279 | * Initiliaze php session if option "runSession" is not (bool) false |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | protected function initSession() |
||
295 | |||
296 | /** |
||
297 | * Initialize errors attribute with \BFW\Core\Errors class |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | protected function initErrors() |
||
305 | |||
306 | /** |
||
307 | * Initialize modules attribut with \BFW\Modules class |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | protected function initModules() |
||
315 | |||
316 | /** |
||
317 | * Add namespace used by a BFW Application to composer |
||
318 | * |
||
319 | * @return void |
||
320 | */ |
||
321 | protected function addComposerNamespaces() |
||
327 | |||
328 | /** |
||
329 | * Declare all step to run application |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | protected function declareRunPhases() |
||
343 | |||
344 | /** |
||
345 | * Run the application |
||
346 | * |
||
347 | * @return void |
||
348 | */ |
||
349 | View Code Duplication | public function run() |
|
364 | |||
365 | /** |
||
366 | * Connect to memcache(d) server with the class declared in config file |
||
367 | * |
||
368 | * @return Object |
||
369 | * |
||
370 | * @throws Exception If memcached is enabled but no class is define. Or if |
||
371 | * The class declared in config is not found. |
||
372 | */ |
||
373 | protected function loadMemcached() |
||
392 | |||
393 | /** |
||
394 | * Read all directory in modules directory and add the module to Modules |
||
395 | * class. |
||
396 | * Generate the load tree. |
||
397 | * Not initialize modules ! |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | protected function readAllModules() |
||
418 | |||
419 | /** |
||
420 | * Load modules define in config bfw file. |
||
421 | * It's module for controller, router, database and template only. |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | protected function loadAllCoreModules() |
||
438 | |||
439 | /** |
||
440 | * Load all modules (except core). |
||
441 | * Get the load tree, read this and load modules in order declared in tree. |
||
442 | * |
||
443 | * @return void |
||
444 | */ |
||
445 | protected function loadAllAppModules() |
||
457 | |||
458 | /** |
||
459 | * Load a module |
||
460 | * |
||
461 | * @param string $moduleName The module's name to load |
||
462 | * |
||
463 | * @return void |
||
464 | */ |
||
465 | protected function loadModule($moduleName) |
||
470 | |||
471 | /** |
||
472 | * Run the cli file if we're in cli mode |
||
473 | * |
||
474 | * @return void |
||
475 | * |
||
476 | * @throws Exception If no file is specified or if the file not exist. |
||
477 | */ |
||
478 | protected function runCliFile() |
||
501 | } |
||
502 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.