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 | * @param DB $dbConnection |
||
| 17 | * |
||
| 18 | * @return string |
||
| 19 | */ |
||
| 20 | 8 | private static function generateCacheKey(DB $dbConnection): string |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Optimize tables |
||
| 39 | * |
||
| 40 | * @param array $tables database table names |
||
| 41 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 42 | * |
||
| 43 | * @return int |
||
| 44 | */ |
||
| 45 | 1 | View Code Duplication | public static function optimizeTables(array $tables = [], DB $dbConnection = null): int |
| 64 | |||
| 65 | /** |
||
| 66 | * Repair tables |
||
| 67 | * |
||
| 68 | * @param array $tables database table names |
||
| 69 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 70 | * |
||
| 71 | * @return int |
||
| 72 | */ |
||
| 73 | 1 | View Code Duplication | public static function repairTables(array $tables = [], DB $dbConnection = null): int |
| 92 | |||
| 93 | /** |
||
| 94 | * Check if "mysqlnd"-driver is used. |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | 91 | public static function isMysqlndIsUsed(): bool |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Check if the current environment supports "utf8mb4". |
||
| 115 | * |
||
| 116 | * @param DB $dbConnection |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | 8 | public static function isUtf8mb4Supported(DB $dbConnection = null): bool |
|
| 161 | |||
| 162 | /** |
||
| 163 | * A phonetic search algorithms for different languages. |
||
| 164 | * |
||
| 165 | * INFO: if you need better performance, please save the "voku\helper\Phonetic"-output into the DB and search for it |
||
| 166 | * |
||
| 167 | * @param string $searchString |
||
| 168 | * @param string $searchFieldName |
||
| 169 | * @param string $idFieldName |
||
| 170 | * @param string $language <p>en, de, fr</p> |
||
| 171 | * @param string $table |
||
| 172 | * @param array $whereArray |
||
| 173 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
||
| 174 | * @param string|null $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 175 | * @param bool $useCache use cache? |
||
| 176 | * @param int $cacheTTL cache-ttl in seconds |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | 2 | public static function phoneticSearch(string $searchString, string $searchFieldName, string $idFieldName = null, string $language = 'de', string $table = '', array $whereArray = null, DB $dbConnection = null, string $databaseName = null, bool $useCache = false, int $cacheTTL = 3600): array |
|
| 181 | { |
||
| 182 | // init |
||
| 183 | 2 | $cacheKey = null; |
|
| 184 | |||
| 185 | 2 | if ($dbConnection === null) { |
|
| 186 | 2 | $dbConnection = DB::getInstance(); |
|
| 187 | } |
||
| 188 | |||
| 189 | 2 | View Code Duplication | if ($table === '') { |
| 190 | $debug = new Debug($dbConnection); |
||
| 191 | $debug->displayError('Invalid table name, table name in empty.', false); |
||
| 192 | |||
| 193 | return []; |
||
| 194 | } |
||
| 195 | |||
| 196 | 2 | if ($idFieldName === null) { |
|
| 197 | $idFieldName = 'id'; |
||
| 198 | } |
||
| 199 | |||
| 200 | 2 | $whereSQL = $dbConnection->_parseArrayPair($whereArray, 'AND'); |
|
| 201 | 2 | if ($whereSQL) { |
|
| 202 | 2 | $whereSQL = 'AND ' . $whereSQL; |
|
| 203 | } |
||
| 204 | |||
| 205 | 2 | if ($databaseName) { |
|
| 206 | $databaseName = $dbConnection->quote_string(\trim($databaseName)) . '.'; |
||
| 207 | } |
||
| 208 | |||
| 209 | // get the row |
||
| 210 | 2 | $query = 'SELECT ' . $dbConnection->quote_string($searchFieldName) . ', ' . $dbConnection->quote_string($idFieldName) . ' |
|
| 211 | 2 | FROM ' . $databaseName . $dbConnection->quote_string($table) . ' |
|
| 212 | WHERE 1 = 1 |
||
| 213 | 2 | ' . $whereSQL . ' |
|
| 214 | '; |
||
| 215 | |||
| 216 | 2 | View Code Duplication | if ($useCache) { |
| 217 | 1 | $cache = new Cache(null, null, false, $useCache); |
|
| 218 | 1 | $cacheKey = 'sql-phonetic-search-' . \md5($query); |
|
| 219 | |||
| 220 | if ( |
||
| 221 | 1 | $cache->getCacheIsReady() |
|
| 222 | && |
||
| 223 | 1 | $cache->existsItem($cacheKey) |
|
| 224 | ) { |
||
| 225 | 1 | return $cache->getItem($cacheKey); |
|
| 226 | } |
||
| 227 | } else { |
||
| 228 | 1 | $cache = false; |
|
| 229 | } |
||
| 230 | |||
| 231 | 2 | $result = $dbConnection->query($query); |
|
| 232 | |||
| 233 | // make sure the row exists |
||
| 234 | 2 | if ($result->num_rows <= 0) { |
|
| 235 | return []; |
||
| 236 | } |
||
| 237 | |||
| 238 | 2 | $dataToSearchIn = []; |
|
| 239 | /** @noinspection LoopWhichDoesNotLoopInspection */ |
||
| 240 | /** @noinspection PhpAssignmentInConditionInspection */ |
||
| 241 | 2 | while ($tmpArray = $result->fetchArray()) { |
|
| 242 | 2 | $dataToSearchIn[$tmpArray[$idFieldName]] = $tmpArray[$searchFieldName]; |
|
| 243 | } |
||
| 244 | |||
| 245 | 2 | $phonetic = new Phonetic($language); |
|
| 246 | 2 | $return = $phonetic->phonetic_matches($searchString, $dataToSearchIn); |
|
| 247 | |||
| 248 | // save into the cache |
||
| 249 | View Code Duplication | if ( |
|
| 250 | 2 | $cacheKey !== null |
|
| 251 | && |
||
| 252 | 2 | $useCache |
|
| 253 | && |
||
| 254 | 2 | $cache instanceof Cache |
|
| 255 | && |
||
| 256 | 2 | $cache->getCacheIsReady() |
|
| 257 | ) { |
||
| 258 | 1 | $cache->setItem($cacheKey, $return, $cacheTTL); |
|
| 259 | } |
||
| 260 | |||
| 261 | 2 | return $return; |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * A string that represents the MySQL client library version. |
||
| 266 | * |
||
| 267 | * @param DB $dbConnection |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 8 | View Code Duplication | public static function get_mysql_client_version(DB $dbConnection = null): string |
| 295 | |||
| 296 | /** |
||
| 297 | * Returns a string representing the version of the MySQL server that the MySQLi extension is connected to. |
||
| 298 | * |
||
| 299 | * @param DB $dbConnection |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 8 | View Code Duplication | public static function get_mysql_server_version(DB $dbConnection = null): string |
| 327 | |||
| 328 | /** |
||
| 329 | * Return all db-fields from a table. |
||
| 330 | * |
||
| 331 | * @param string $table |
||
| 332 | * @param bool $useStaticCache |
||
| 333 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
||
| 334 | * @param string|null $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | 1 | public static function getDbFields(string $table, bool $useStaticCache = true, DB $dbConnection = null, string $databaseName = null): array |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Copy row within a DB table and making updates to the columns. |
||
| 386 | * |
||
| 387 | * @param string $table |
||
| 388 | * @param array $whereArray |
||
| 389 | * @param array $updateArray |
||
| 390 | * @param array $ignoreArray |
||
| 391 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 392 | * @param string|null $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 393 | * |
||
| 394 | * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 395 | * "false" on error |
||
| 396 | */ |
||
| 397 | 1 | public static function copyTableRow(string $table, array $whereArray, array $updateArray = [], array $ignoreArray = [], DB $dbConnection = null, string $databaseName = null) |
|
| 471 | } |
||
| 472 |
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.