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 Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Helper |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Check if "mysqlnd"-driver is used. |
||
| 16 | * |
||
| 17 | 34 | * @return bool |
|
| 18 | */ |
||
| 19 | 34 | public static function isMysqlndIsUsed() |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Check if the current environment supports "utf8mb4". |
||
| 32 | * |
||
| 33 | * @param DB $dbConnection |
||
| 34 | * |
||
| 35 | 7 | * @return bool |
|
| 36 | */ |
||
| 37 | public static function isUtf8mb4Supported(DB $dbConnection = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | 7 | * A phonetic search algorithms for different languages. |
|
| 82 | * |
||
| 83 | 7 | * @param string $searchString |
|
| 84 | * @param string $searchFieldName |
||
| 85 | 7 | * @param string $idFieldName |
|
| 86 | 1 | * @param string $language <p>en, de, fr</p> |
|
| 87 | 1 | * @param string $table |
|
| 88 | * @param array $whereArray |
||
| 89 | 7 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
|
| 90 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public static function phoneticSearch($searchString, $searchFieldName, $idFieldName = null, $language = 'de', $table, array $whereArray = null, DB $dbConnection = null, $databaseName = null) |
||
| 147 | |||
| 148 | 1 | /** |
|
| 149 | * A string that represents the MySQL client library version. |
||
| 150 | * |
||
| 151 | * @param DB $dbConnection |
||
| 152 | 1 | * |
|
| 153 | 1 | * @return string |
|
| 154 | */ |
||
| 155 | 1 | View Code Duplication | public static function get_mysql_client_version(DB $dbConnection = null) |
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns a string representing the version of the MySQL server that the MySQLi extension is connected to. |
||
| 173 | * |
||
| 174 | * @param DB $dbConnection |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | View Code Duplication | public static function get_mysql_server_version(DB $dbConnection = null) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Return all db-fields from a table. |
||
| 195 | * |
||
| 196 | 1 | * @param string $table |
|
| 197 | 1 | * @param bool $useStaticCache |
|
| 198 | 1 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
|
| 199 | 1 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
|
| 200 | * |
||
| 201 | 1 | * @return array |
|
| 202 | */ |
||
| 203 | public static function getDbFields($table, $useStaticCache = true, DB $dbConnection = null, $databaseName = null) |
||
| 248 | 1 | ||
| 249 | /** |
||
| 250 | * Copy row within a DB table and making updates to the columns. |
||
| 251 | * |
||
| 252 | * @param string $table |
||
| 253 | * @param array $whereArray |
||
| 254 | * @param array $updateArray |
||
| 255 | * @param array $ignoreArray |
||
| 256 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 257 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 258 | * |
||
| 259 | * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 260 | * "false" on error |
||
| 261 | */ |
||
| 262 | public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $dbConnection = null, $databaseName = null) |
||
| 336 | } |
||
| 337 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.