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 |
||
| 13 | class Helper |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Optimize tables |
||
| 17 | * |
||
| 18 | * @param array $tables database table names |
||
| 19 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 20 | * |
||
| 21 | * @return int |
||
| 22 | */ |
||
| 23 | 1 | View Code Duplication | public static function optimizeTables(array $tables = array(), DB $dbConnection = null) |
| 42 | |||
| 43 | /** |
||
| 44 | * Repair tables |
||
| 45 | * |
||
| 46 | * @param array $tables database table names |
||
| 47 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 48 | * |
||
| 49 | * @return int |
||
| 50 | */ |
||
| 51 | 1 | View Code Duplication | public static function repairTables(array $tables = array(), DB $dbConnection = null) |
| 70 | |||
| 71 | /** |
||
| 72 | * Check if "mysqlnd"-driver is used. |
||
| 73 | * |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | 57 | public static function isMysqlndIsUsed() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Check if the current environment supports "utf8mb4". |
||
| 89 | * |
||
| 90 | * @param DB $dbConnection |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | 8 | public static function isUtf8mb4Supported(DB $dbConnection = null) |
|
| 95 | { |
||
| 96 | /** |
||
| 97 | * https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
||
| 98 | * |
||
| 99 | * - You’re currently using the utf8 character set. |
||
| 100 | * - Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB). |
||
| 101 | * - Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher. |
||
| 102 | * |
||
| 103 | * INFO: utf8mb4 is 100% backwards compatible with utf8. |
||
| 104 | */ |
||
| 105 | |||
| 106 | 8 | if ($dbConnection === null) { |
|
| 107 | $dbConnection = DB::getInstance(); |
||
| 108 | } |
||
| 109 | |||
| 110 | 8 | $server_version = self::get_mysql_server_version($dbConnection); |
|
| 111 | 8 | $client_version = self::get_mysql_client_version($dbConnection); |
|
| 112 | |||
| 113 | if ( |
||
| 114 | $server_version >= 50503 |
||
| 115 | 8 | && |
|
| 116 | ( |
||
| 117 | ( |
||
| 118 | 8 | self::isMysqlndIsUsed() === true |
|
| 119 | 8 | && |
|
| 120 | $client_version >= 50009 |
||
| 121 | 8 | ) |
|
| 122 | || |
||
| 123 | ( |
||
| 124 | 8 | self::isMysqlndIsUsed() === false |
|
| 125 | 8 | && |
|
| 126 | $client_version >= 50503 |
||
| 127 | ) |
||
| 128 | 8 | ) |
|
| 129 | |||
| 130 | 8 | ) { |
|
| 131 | return true; |
||
| 132 | } |
||
| 133 | |||
| 134 | 8 | return false; |
|
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * A phonetic search algorithms for different languages. |
||
| 139 | * |
||
| 140 | * INFO: if you need better performance, please save the "voku\helper\Phonetic"-output into the DB and search for it |
||
| 141 | * |
||
| 142 | * @param string $searchString |
||
| 143 | * @param string $searchFieldName |
||
| 144 | * @param string $idFieldName |
||
| 145 | * @param string $language <p>en, de, fr</p> |
||
| 146 | * @param string $table |
||
| 147 | * @param array $whereArray |
||
| 148 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
||
| 149 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 150 | * @param bool $useCache use cache? |
||
| 151 | * @param int $cacheTTL cache-ttl in seconds |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | 2 | public static function phoneticSearch($searchString, $searchFieldName, $idFieldName = null, $language = 'de', $table, array $whereArray = null, DB $dbConnection = null, $databaseName = null, $useCache = false, $cacheTTL = 3600) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * A string that represents the MySQL client library version. |
||
| 244 | * |
||
| 245 | * @param DB $dbConnection |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | 8 | View Code Duplication | public static function get_mysql_client_version(DB $dbConnection = null) |
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns a string representing the version of the MySQL server that the MySQLi extension is connected to. |
||
| 267 | * |
||
| 268 | * @param DB $dbConnection |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 8 | View Code Duplication | public static function get_mysql_server_version(DB $dbConnection = null) |
| 286 | |||
| 287 | /** |
||
| 288 | * Return all db-fields from a table. |
||
| 289 | * |
||
| 290 | * @param string $table |
||
| 291 | * @param bool $useStaticCache |
||
| 292 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
||
| 293 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | 1 | public static function getDbFields($table, $useStaticCache = true, DB $dbConnection = null, $databaseName = null) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Copy row within a DB table and making updates to the columns. |
||
| 345 | * |
||
| 346 | * @param string $table |
||
| 347 | * @param array $whereArray |
||
| 348 | * @param array $updateArray |
||
| 349 | * @param array $ignoreArray |
||
| 350 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 351 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 352 | * |
||
| 353 | * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 354 | * "false" on error |
||
| 355 | */ |
||
| 356 | 1 | public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $dbConnection = null, $databaseName = null) |
|
| 430 | } |
||
| 431 |
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.