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 |
||
| 13 | class Options extends Dictionary { |
||
| 14 | protected static $fields = null; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Load a PHP configuration file (script must return array) |
||
| 18 | * @param string $filepath The path of the PHP config file |
||
| 19 | * @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
||
| 20 | */ |
||
| 21 | View Code Duplication | public static function loadPHP($filepath,$prefix_path=null){ |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Load an INI configuration file |
||
| 33 | * @param string $filepath The path of the INI config file |
||
| 34 | * @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
||
| 35 | */ |
||
| 36 | View Code Duplication | public static function loadINI($filepath,$prefix_path=null){ |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Load a JSON configuration file |
||
| 46 | * @param string $filepath The path of the JSON config file |
||
| 47 | * @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
||
| 48 | */ |
||
| 49 | public static function loadJSON($filepath,$prefix_path=null){ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Load an array to the configuration |
||
| 60 | * @param array $array The array to load |
||
| 61 | * @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
||
| 62 | */ |
||
| 63 | public static function loadArray(array $array,$prefix_path=null){ |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Load an ENV file |
||
| 75 | * @param string $dir The directory of the ENV file |
||
| 76 | * @param string $envname The filename for the ENV file (Default to `.env`) |
||
| 77 | * @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
||
| 78 | */ |
||
| 79 | public static function loadENV($dir,$envname='.env',$prefix_path=null){ |
||
| 99 | |||
| 100 | } |
||
| 101 |
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.