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 | * @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 | * Constructor |
||
93 | * Init output buffering |
||
94 | * Declare run steps |
||
95 | * Set UTF-8 header |
||
96 | * |
||
97 | * protected for Singleton pattern |
||
98 | */ |
||
99 | protected function __construct() |
||
109 | |||
110 | /** |
||
111 | * Get the Application instance (Singleton pattern) |
||
112 | * |
||
113 | * @param array $options Options passed to application |
||
114 | * |
||
115 | * @return \BFW\Application The current instance of this class |
||
116 | */ |
||
117 | public static function getInstance($options = []) |
||
127 | |||
128 | /** |
||
129 | * Like getInstance. This is to have a keyword easier for users who want |
||
130 | * initialize the application |
||
131 | * |
||
132 | * @param array $options Options passed to application |
||
133 | * |
||
134 | * @return \BFW\Application The current instance of this class |
||
135 | */ |
||
136 | public static function init($options = []) |
||
141 | |||
142 | /** |
||
143 | * Getter to access to composerLoader property |
||
144 | * |
||
145 | * @return \Composer\Autoload\ClassLoader The composer class loader |
||
146 | */ |
||
147 | public function getComposerLoader() |
||
151 | |||
152 | /** |
||
153 | * Getter to access to the config instance |
||
154 | * |
||
155 | * @return \BFW\Config |
||
156 | */ |
||
157 | public function getConfig() |
||
161 | |||
162 | /** |
||
163 | * Getter to access to memcache instance |
||
164 | * |
||
165 | * @return Object|null |
||
166 | */ |
||
167 | public function getMemcached() |
||
171 | |||
172 | /** |
||
173 | * Getter to access to a module |
||
174 | * |
||
175 | * @param string $moduleName The module name to access |
||
176 | * |
||
177 | * @return \BFW\Module |
||
178 | */ |
||
179 | public function getModule($moduleName) |
||
183 | |||
184 | /** |
||
185 | * Getter to access to an option's value |
||
186 | * |
||
187 | * @param string $optionKey The key for the option |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function getOption($optionKey) |
||
195 | |||
196 | /** |
||
197 | * Getter to access to the Request instance |
||
198 | * |
||
199 | * @return \BFW\Request |
||
200 | */ |
||
201 | public function getRequest() |
||
205 | |||
206 | /** |
||
207 | * Initialize all components |
||
208 | * |
||
209 | * @param array $options Options passed to application |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | protected function initSystem($options) |
||
224 | |||
225 | /** |
||
226 | * Initialize options with the class \BFW\Core\Options |
||
227 | * |
||
228 | * @param array $options The option passed when initialize this class |
||
229 | */ |
||
230 | protected function initOptions($options) |
||
240 | |||
241 | /** |
||
242 | * Initialize all constants used by framework |
||
243 | * Use helper Constants::create to allow override of constants |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | protected function initConstants() |
||
263 | |||
264 | /** |
||
265 | * Initialize composer loader |
||
266 | * Obtain the composerLoader instance |
||
267 | * Call addComposerNamespaces method to add Application namespaces |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | protected function initComposerLoader() |
||
278 | |||
279 | /** |
||
280 | * Initialize the property config with \BFW\Config instance |
||
281 | * The config class will search all file in "bfw" directory and load files |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | protected function initConfig() |
||
290 | |||
291 | /** |
||
292 | * Initialize request property with the \BFW\Request class |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | protected function initRequest() |
||
300 | |||
301 | /** |
||
302 | * Initiliaze php session if option "runSession" is not (bool) false |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | protected function initSession() |
||
318 | |||
319 | /** |
||
320 | * Initialize errors property with the \BFW\Core\Errors class |
||
321 | * |
||
322 | * @return void |
||
323 | */ |
||
324 | protected function initErrors() |
||
328 | |||
329 | /** |
||
330 | * Initialize modules property with the \BFW\Modules class |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | protected function initModules() |
||
338 | |||
339 | /** |
||
340 | * Add namespaces used by a BFW Application to composer |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | protected function addComposerNamespaces() |
||
350 | |||
351 | /** |
||
352 | * Declare all steps to run the application |
||
353 | * |
||
354 | * @return void |
||
355 | */ |
||
356 | protected function declareRunSteps() |
||
366 | |||
367 | /** |
||
368 | * Run the application |
||
369 | * |
||
370 | * @return void |
||
371 | */ |
||
372 | View Code Duplication | public function run() |
|
387 | |||
388 | /** |
||
389 | * Connect to memcache(d) server with the class declared in config file |
||
390 | * |
||
391 | * @return Object |
||
392 | * |
||
393 | * @throws Exception If memcached is enabled but no class is define. Or if |
||
394 | * The class declared into the config is not found. |
||
395 | */ |
||
396 | protected function loadMemcached() |
||
421 | |||
422 | /** |
||
423 | * Read all directories in modules directory and add each module to Modules |
||
424 | * class. |
||
425 | * Generate the load tree. |
||
426 | * Not initialize modules ! |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | protected function readAllModules() |
||
447 | |||
448 | /** |
||
449 | * Load core modules defined into config bfw file. |
||
450 | * Only module for controller, router, database and template only. |
||
451 | * |
||
452 | * @return void |
||
453 | */ |
||
454 | protected function loadAllCoreModules() |
||
467 | |||
468 | /** |
||
469 | * Load all modules (except core). |
||
470 | * Get the load tree, read him and load all modules with the order |
||
471 | * declared into the tree. |
||
472 | * |
||
473 | * @return void |
||
474 | */ |
||
475 | protected function loadAllAppModules() |
||
487 | |||
488 | /** |
||
489 | * Load a module |
||
490 | * |
||
491 | * @param string $moduleName The module's name to load |
||
492 | * |
||
493 | * @return void |
||
494 | */ |
||
495 | protected function loadModule($moduleName) |
||
500 | |||
501 | /** |
||
502 | * Run the cli file if we're in cli mode |
||
503 | * |
||
504 | * @return void |
||
505 | * |
||
506 | * @throws Exception If no file is specified or if the file not exist. |
||
507 | */ |
||
508 | protected function runCliFile() |
||
537 | } |
||
538 |
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.