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 |
||
| 11 | class Config{ |
||
|
|
|||
| 12 | |||
| 13 | /** |
||
| 14 | * Array of configurations |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | public static $config = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Array of javascript configurations |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | public static $jsConfig = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Gets a configuration value |
||
| 29 | * |
||
| 30 | * @param $key string |
||
| 31 | * @return string|null |
||
| 32 | * @throws Exception if configuration file doesn't exist |
||
| 33 | */ |
||
| 34 | public static function get($key){ |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Loads javascript configurations |
||
| 52 | * |
||
| 53 | * @param $key string |
||
| 54 | * @return string|null |
||
| 55 | * @throws Exception if configuration file doesn't exist |
||
| 56 | */ |
||
| 57 | private static function loadJsConfig(){ |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Gets javascript configuration value(s) |
||
| 73 | * |
||
| 74 | * @param $key string |
||
| 75 | * @return string|array|null |
||
| 76 | * @throws Exception if configuration file doesn't exist |
||
| 77 | */ |
||
| 78 | public static function getJsConfig($key = ""){ |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Adds a new variable to javascript configurations |
||
| 94 | * |
||
| 95 | * @param string $key |
||
| 96 | * @param mixed $value |
||
| 97 | */ |
||
| 98 | public static function addJsConfig($key, $value){ |
||
| 104 | } |
||
| 105 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.