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 |
||
| 14 | class DatabaseAdapterRegistry { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Default database connector registration fields |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private static $default_fields = array( |
||
| 22 | 'server' => array( |
||
| 23 | 'title' => 'Database server', |
||
| 24 | 'envVar' => 'SS_DATABASE_SERVER', |
||
| 25 | 'default' => 'localhost' |
||
| 26 | ), |
||
| 27 | 'username' => array( |
||
| 28 | 'title' => 'Database username', |
||
| 29 | 'envVar' => 'SS_DATABASE_USERNAME', |
||
| 30 | 'default' => 'root' |
||
| 31 | ), |
||
| 32 | 'password' => array( |
||
| 33 | 'title' => 'Database password', |
||
| 34 | 'envVar' => 'SS_DATABASE_PASSWORD', |
||
| 35 | 'default' => 'password' |
||
| 36 | ), |
||
| 37 | 'database' => array( |
||
| 38 | 'title' => 'Database name', |
||
| 39 | 'default' => 'SS_mysite', |
||
| 40 | 'attributes' => array( |
||
| 41 | "onchange" => "this.value = this.value.replace(/[\/\\:*?"<>|. \t]+/g,'');" |
||
| 42 | ) |
||
| 43 | ), |
||
| 44 | ); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Internal array of registered database adapters |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private static $adapters = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add new adapter to the registry |
||
| 55 | * |
||
| 56 | * @param array $config Associative array of configuration details. This must include: |
||
| 57 | * - title |
||
| 58 | * - class |
||
| 59 | * - helperClass |
||
| 60 | * - supported |
||
| 61 | * This SHOULD include: |
||
| 62 | * - fields |
||
| 63 | * - helperPath (if helperClass can't be autoloaded via psr-4/-0) |
||
| 64 | * - missingExtensionText |
||
| 65 | * - module OR missingModuleText |
||
| 66 | */ |
||
| 67 | public static function register($config) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Unregisters a database connector by classname |
||
| 102 | * |
||
| 103 | * @param string $class |
||
| 104 | */ |
||
| 105 | public static function unregister($class) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Detects all _register_database.php files and invokes them |
||
| 111 | */ |
||
| 112 | View Code Duplication | public static function autodiscover() { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Detects all _configure_database.php files and invokes them |
||
| 122 | * Called by ConfigureFromEnv.php |
||
| 123 | */ |
||
| 124 | View Code Duplication | public static function autoconfigure() { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Return all registered adapters |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public static function get_adapters() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns registry data for a class |
||
| 143 | * |
||
| 144 | * @param string $class |
||
| 145 | * @return array List of adapter properties |
||
| 146 | */ |
||
| 147 | public static function get_adapter($class) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retrieves default field configuration |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public static function get_default_fields() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Build configuration helper for a given class |
||
| 165 | * |
||
| 166 | * @param string $databaseClass Name of class |
||
| 167 | * @return DatabaseConfigurationHelper|null Instance of helper, or null if cannot be loaded |
||
| 168 | */ |
||
| 169 | public static function getDatabaseConfigurationHelper($databaseClass) { |
||
| 184 | |||
| 185 | } |
||
| 186 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.