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:
1 | <?php |
||
44 | class Core |
||
45 | { |
||
46 | /** |
||
47 | * The current version of Cervo. |
||
48 | */ |
||
49 | const VERSION = '4.0.0'; |
||
50 | |||
51 | /** |
||
52 | * All the libraries instances that have been initialized through getLibrary(). |
||
53 | * @var array |
||
54 | */ |
||
55 | protected static $libraries = []; |
||
56 | |||
57 | /** |
||
58 | * All the controller instances that have been initialized through getController(). |
||
59 | * @var array |
||
60 | */ |
||
61 | protected static $controllers = []; |
||
62 | |||
63 | /** |
||
64 | * Additinal autoloading functions. |
||
65 | * @var callable[] |
||
66 | */ |
||
67 | protected static $autoloads = []; |
||
68 | |||
69 | /** |
||
70 | * If Cervo have been initialized. |
||
71 | * @var bool |
||
72 | */ |
||
73 | private static $is_init = false; |
||
74 | |||
75 | /** |
||
76 | * If the configuration/autoloaders have been initialized. |
||
77 | * @var bool |
||
78 | */ |
||
79 | private static $is_init_config = false; |
||
80 | |||
81 | /** |
||
82 | * Initialize Cervo. |
||
83 | * |
||
84 | * @param string|null $json_config_file The path to the JSON configuration file to use. |
||
85 | */ |
||
86 | public static function init($json_config_file = null) |
||
140 | |||
141 | /** |
||
142 | * Initialize the configuration for Cervo with default configs. |
||
143 | * |
||
144 | * @param string|null $json_config_file |
||
145 | */ |
||
146 | public static function initConfig($json_config_file = null) |
||
188 | |||
189 | /** |
||
190 | * Return a library. It will be stored in an internal cache and reused if called again. |
||
191 | * $name format: [Module]/[Name] |
||
192 | * Name MAY contain slashes (/) to go deeper in the tree. |
||
193 | * The module name Cervo may be used to access the Cervo standard libraries. |
||
194 | * |
||
195 | * If you do not want your library to be re-used, please access the library directly without |
||
196 | * using any functions or methods. Ex: |
||
197 | * new \Application\[Module]Module\Libraries\[Name](); |
||
198 | * |
||
199 | * @param string $name The path name |
||
200 | * |
||
201 | * @return object |
||
202 | */ |
||
203 | public static function getLibrary($name) |
||
224 | |||
225 | /** |
||
226 | * Return a controller. It will be stored in an internal cache and reused if called again. |
||
227 | * $name format: [Module]/[Name] |
||
228 | * $name MAY contain slashes (/) to go deeper in the tree. |
||
229 | * |
||
230 | * @param string $name The path name |
||
231 | * |
||
232 | * @return object |
||
233 | */ |
||
234 | public static function getController($name) |
||
243 | |||
244 | /** |
||
245 | * Return a model. |
||
246 | * $name format: [Module]/[Name] |
||
247 | * $name MAY contain slashes (/) to go deeper in the tree. |
||
248 | * |
||
249 | * @param string $name The path name |
||
250 | * |
||
251 | * @return object |
||
252 | */ |
||
253 | public static function getModel($name) |
||
257 | |||
258 | /** |
||
259 | * Return a view. |
||
260 | * $name format: [Module]/[Name] |
||
261 | * $name MAY contain slashes (/) to go deeper in the tree. |
||
262 | * |
||
263 | * @param string $name The path name |
||
264 | * |
||
265 | * @return object |
||
266 | */ |
||
267 | public static function getView($name) |
||
271 | |||
272 | /** |
||
273 | * Return an instanciated object depending on the module sub-folder. |
||
274 | * $class_path format: [Module]/[Name] |
||
275 | * $class_path MAY contain slashes (/) to go deeper in the tree. |
||
276 | * $application_path is the module sub-folder to look in for. |
||
277 | * |
||
278 | * @param string $class_path The path name |
||
279 | * @param string $application_path The sub-folder within the module |
||
280 | * |
||
281 | * @return object |
||
282 | */ |
||
283 | public static function getPath($class_path, $application_path) |
||
295 | |||
296 | /** |
||
297 | * Return a template. |
||
298 | * $name format: [Module]/[Name] |
||
299 | * Name MAY contain slashes (/) to go deeper in the tree. |
||
300 | * |
||
301 | * @param string $name The path name |
||
302 | * |
||
303 | * @return Template |
||
304 | */ |
||
305 | public static function getTemplate($name) |
||
309 | |||
310 | /** |
||
311 | * The default class autoloader. |
||
312 | * Also run any additional autoloaders added with register_autoload(). |
||
313 | * |
||
314 | * @param string $name The class full name (Include the namespace(s)) |
||
315 | */ |
||
316 | public static function autoload($name) |
||
330 | } |
||
331 |
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.