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 | * Check if "mysqlnd"-driver is used. |
||
| 17 | * |
||
| 18 | * @return bool |
||
| 19 | */ |
||
| 20 | 51 | public static function isMysqlndIsUsed() |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Check if the current environment supports "utf8mb4". |
||
| 33 | * |
||
| 34 | * @param DB $dbConnection |
||
| 35 | * |
||
| 36 | * @return bool |
||
| 37 | */ |
||
| 38 | 8 | public static function isUtf8mb4Supported(DB $dbConnection = null) |
|
| 39 | { |
||
| 40 | /** |
||
| 41 | * https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
||
| 42 | * |
||
| 43 | * - You’re currently using the utf8 character set. |
||
| 44 | * - Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB). |
||
| 45 | * - Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher. |
||
| 46 | * |
||
| 47 | * INFO: utf8mb4 is 100% backwards compatible with utf8. |
||
| 48 | */ |
||
| 49 | |||
| 50 | 8 | if ($dbConnection === null) { |
|
| 51 | $dbConnection = DB::getInstance(); |
||
| 52 | } |
||
| 53 | |||
| 54 | 8 | $server_version = self::get_mysql_server_version($dbConnection); |
|
| 55 | 8 | $client_version = self::get_mysql_client_version($dbConnection); |
|
| 56 | |||
| 57 | if ( |
||
| 58 | $server_version >= 50503 |
||
| 59 | 8 | && |
|
| 60 | ( |
||
| 61 | ( |
||
| 62 | 8 | self::isMysqlndIsUsed() === true |
|
| 63 | 8 | && |
|
| 64 | $client_version >= 50009 |
||
| 65 | 8 | ) |
|
| 66 | || |
||
| 67 | ( |
||
| 68 | 8 | self::isMysqlndIsUsed() === false |
|
| 69 | 8 | && |
|
| 70 | $client_version >= 50503 |
||
| 71 | ) |
||
| 72 | 8 | ) |
|
| 73 | |||
| 74 | 8 | ) { |
|
| 75 | return true; |
||
| 76 | } |
||
| 77 | |||
| 78 | 8 | return false; |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * A phonetic search algorithms for different languages. |
||
| 83 | * |
||
| 84 | * INFO: if you need better performance, please save the "voku\helper\Phonetic"-output into the DB and search for it |
||
| 85 | * |
||
| 86 | * @param string $searchString |
||
| 87 | * @param string $searchFieldName |
||
| 88 | * @param string $idFieldName |
||
| 89 | * @param string $language <p>en, de, fr</p> |
||
| 90 | * @param string $table |
||
| 91 | * @param array $whereArray |
||
| 92 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
||
| 93 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 94 | * @param bool $useCache use cache? |
||
| 95 | * @param int $cacheTTL cache-ttl in seconds |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | 2 | public static function phoneticSearch($searchString, $searchFieldName, $idFieldName = null, $language = 'de', $table, array $whereArray = null, DB $dbConnection = null, $databaseName = null, $useCache = false, $cacheTTL = 3600) |
|
| 100 | { |
||
| 101 | // init |
||
| 102 | 2 | $cacheKey = null; |
|
| 103 | 2 | $searchString = (string)$searchString; |
|
| 104 | 2 | $searchFieldName = (string)$searchFieldName; |
|
| 105 | |||
| 106 | 2 | if ($dbConnection === null) { |
|
| 107 | 2 | $dbConnection = DB::getInstance(); |
|
| 108 | 2 | } |
|
| 109 | |||
| 110 | 2 | View Code Duplication | if ($table === '') { |
| 111 | $debug = new Debug($dbConnection); |
||
| 112 | $debug->displayError('Invalid table name, table name in empty.', false); |
||
| 113 | |||
| 114 | return array(); |
||
| 115 | } |
||
| 116 | |||
| 117 | 2 | if ($idFieldName === null) { |
|
| 118 | $idFieldName = 'id'; |
||
| 119 | } |
||
| 120 | |||
| 121 | 2 | $whereSQL = $dbConnection->_parseArrayPair($whereArray, 'AND'); |
|
|
|
|||
| 122 | 2 | if ($whereSQL) { |
|
| 123 | 2 | $whereSQL = 'AND ' . $whereSQL; |
|
| 124 | 2 | } |
|
| 125 | |||
| 126 | 2 | if ($databaseName) { |
|
| 127 | $databaseName = $dbConnection->quote_string(trim($databaseName)) . '.'; |
||
| 128 | } |
||
| 129 | |||
| 130 | // get the row |
||
| 131 | 2 | $query = 'SELECT ' . $dbConnection->quote_string($searchFieldName) . ', ' . $dbConnection->quote_string($idFieldName) . ' |
|
| 132 | 2 | FROM ' . $databaseName . $dbConnection->quote_string($table) . ' |
|
| 133 | WHERE 1 = 1 |
||
| 134 | 2 | ' . $whereSQL . ' |
|
| 135 | 2 | '; |
|
| 136 | |||
| 137 | 2 | View Code Duplication | if ($useCache === true) { |
| 138 | 1 | $cache = new Cache(null, null, false, $useCache); |
|
| 139 | 1 | $cacheKey = 'sql-phonetic-search-' . md5($query); |
|
| 140 | |||
| 141 | if ( |
||
| 142 | 1 | $cache->getCacheIsReady() === true |
|
| 143 | 1 | && |
|
| 144 | 1 | $cache->existsItem($cacheKey) |
|
| 145 | 1 | ) { |
|
| 146 | 1 | return $cache->getItem($cacheKey); |
|
| 147 | } |
||
| 148 | |||
| 149 | 1 | } else { |
|
| 150 | 1 | $cache = false; |
|
| 151 | } |
||
| 152 | |||
| 153 | 2 | $result = $dbConnection->query($query); |
|
| 154 | |||
| 155 | // make sure the row exists |
||
| 156 | 2 | if ($result->num_rows <= 0) { |
|
| 157 | return array(); |
||
| 158 | } |
||
| 159 | |||
| 160 | 2 | $dataToSearchIn = array(); |
|
| 161 | /** @noinspection LoopWhichDoesNotLoopInspection */ |
||
| 162 | /** @noinspection PhpAssignmentInConditionInspection */ |
||
| 163 | 2 | while ($tmpArray = $result->fetchArray()) { |
|
| 164 | 2 | $dataToSearchIn[$tmpArray[$idFieldName]] = $tmpArray[$searchFieldName]; |
|
| 165 | 2 | } |
|
| 166 | |||
| 167 | 2 | $phonetic = new Phonetic($language); |
|
| 168 | 2 | $return = $phonetic->phonetic_matches($searchString, $dataToSearchIn); |
|
| 169 | |||
| 170 | // save into the cache |
||
| 171 | View Code Duplication | if ( |
|
| 172 | $cacheKey !== null |
||
| 173 | 2 | && |
|
| 174 | $useCache === true |
||
| 175 | 2 | && |
|
| 176 | $cache instanceof Cache |
||
| 177 | 2 | && |
|
| 178 | 1 | $cache->getCacheIsReady() === true |
|
| 179 | 2 | ) { |
|
| 180 | 1 | $cache->setItem($cacheKey, $return, $cacheTTL); |
|
| 181 | 1 | } |
|
| 182 | |||
| 183 | 2 | return $return; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * A string that represents the MySQL client library version. |
||
| 188 | * |
||
| 189 | * @param DB $dbConnection |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 8 | View Code Duplication | public static function get_mysql_client_version(DB $dbConnection = null) |
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns a string representing the version of the MySQL server that the MySQLi extension is connected to. |
||
| 211 | * |
||
| 212 | * @param DB $dbConnection |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 8 | View Code Duplication | public static function get_mysql_server_version(DB $dbConnection = null) |
| 230 | |||
| 231 | /** |
||
| 232 | * Return all db-fields from a table. |
||
| 233 | * |
||
| 234 | * @param string $table |
||
| 235 | * @param bool $useStaticCache |
||
| 236 | * @param DB|null $dbConnection <p>use <strong>null</strong> if you will use the current database-connection</p> |
||
| 237 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 1 | public static function getDbFields($table, $useStaticCache = true, DB $dbConnection = null, $databaseName = null) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Copy row within a DB table and making updates to the columns. |
||
| 289 | * |
||
| 290 | * @param string $table |
||
| 291 | * @param array $whereArray |
||
| 292 | * @param array $updateArray |
||
| 293 | * @param array $ignoreArray |
||
| 294 | * @param DB|null $dbConnection <p>Use <strong>null</strong> to get your first singleton instance.</p> |
||
| 295 | * @param null|string $databaseName <p>use <strong>null</strong> if you will use the current database</p> |
||
| 296 | * |
||
| 297 | * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 298 | * "false" on error |
||
| 299 | */ |
||
| 300 | 1 | public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $dbConnection = null, $databaseName = null) |
|
| 374 | } |
||
| 375 |
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.