| Total Complexity | 174 |
| Total Lines | 419 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MySqlDataTypeHelper 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.
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 MySqlDataTypeHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class MySqlDataTypeHelper implements CommonDataTypeHelper |
||
| 13 | { |
||
| 14 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 15 | /** |
||
| 16 | * Returns the widths of a field based on a MySQL data type. |
||
| 17 | * |
||
| 18 | * @param array $dataTypeInfo Metadata of the column on which the field is based. |
||
| 19 | */ |
||
| 20 | public static function deriveFieldLength(array $dataTypeInfo): ?int |
||
| 96 | } |
||
| 97 | |||
| 98 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 99 | /** |
||
| 100 | * Returns the type of bind variable. |
||
| 101 | * |
||
| 102 | * @see http://php.net/manual/en/mysqli-stmt.bind-param.php |
||
| 103 | * |
||
| 104 | * @param array $dataTypeInfo Metadata of the column on which the field is based. |
||
| 105 | */ |
||
| 106 | public static function getBindVariableType(array $dataTypeInfo): string |
||
| 107 | { |
||
| 108 | $ret = ''; |
||
| 109 | switch ($dataTypeInfo['data_type']) |
||
| 110 | { |
||
| 111 | case 'tinyint': |
||
| 112 | case 'smallint': |
||
| 113 | case 'mediumint': |
||
| 114 | case 'int': |
||
| 115 | case 'bigint': |
||
| 116 | case 'year': |
||
| 117 | $ret = 'i'; |
||
| 118 | break; |
||
| 119 | |||
| 120 | case 'float': |
||
| 121 | case 'double': |
||
| 122 | $ret = 'd'; |
||
| 123 | break; |
||
| 124 | |||
| 125 | case 'time': |
||
| 126 | case 'timestamp': |
||
| 127 | case 'binary': |
||
| 128 | case 'enum': |
||
| 129 | case 'bit': |
||
| 130 | case 'set': |
||
| 131 | case 'char': |
||
| 132 | case 'varchar': |
||
| 133 | case 'date': |
||
| 134 | case 'datetime': |
||
| 135 | case 'varbinary': |
||
| 136 | case 'decimal': |
||
| 137 | case 'inet4': |
||
| 138 | case 'inet6': |
||
| 139 | case 'list_of_int': |
||
| 140 | $ret = 's'; |
||
| 141 | break; |
||
| 142 | |||
| 143 | case 'tinytext': |
||
| 144 | case 'text': |
||
| 145 | case 'mediumtext': |
||
| 146 | case 'longtext': |
||
| 147 | case 'tinyblob': |
||
| 148 | case 'blob': |
||
| 149 | case 'mediumblob': |
||
| 150 | case 'longblob': |
||
| 151 | $ret .= 'b'; |
||
| 152 | break; |
||
| 153 | |||
| 154 | default: |
||
| 155 | throw new FallenException('parameter type', $dataTypeInfo['data_type']); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $ret; |
||
| 159 | } |
||
| 160 | |||
| 161 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 162 | /** |
||
| 163 | * Returns whether MySQL column type is a BLOB or a CLOB. |
||
| 164 | * |
||
| 165 | * @param string $dataType Metadata of the MySQL data type. |
||
| 166 | */ |
||
| 167 | public static function isBlobParameter(string $dataType): bool |
||
| 168 | { |
||
| 169 | switch ($dataType) |
||
| 170 | { |
||
| 171 | case 'tinytext': |
||
| 172 | case 'text': |
||
| 173 | case 'mediumtext': |
||
| 174 | case 'longtext': |
||
| 175 | case 'tinyblob': |
||
| 176 | case 'blob': |
||
| 177 | case 'mediumblob': |
||
| 178 | case 'longblob': |
||
| 179 | $isBlob = true; |
||
| 180 | break; |
||
| 181 | |||
| 182 | case 'tinyint': |
||
| 183 | case 'smallint': |
||
| 184 | case 'mediumint': |
||
| 185 | case 'int': |
||
| 186 | case 'bigint': |
||
| 187 | case 'year': |
||
| 188 | case 'decimal': |
||
| 189 | case 'float': |
||
| 190 | case 'double': |
||
| 191 | case 'time': |
||
| 192 | case 'timestamp': |
||
| 193 | case 'binary': |
||
| 194 | case 'enum': |
||
| 195 | case 'inet4': |
||
| 196 | case 'inet6': |
||
| 197 | case 'bit': |
||
| 198 | case 'set': |
||
| 199 | case 'char': |
||
| 200 | case 'varchar': |
||
| 201 | case 'date': |
||
| 202 | case 'datetime': |
||
| 203 | case 'varbinary': |
||
| 204 | case 'list_of_int': |
||
| 205 | $isBlob = false; |
||
| 206 | break; |
||
| 207 | |||
| 208 | default: |
||
| 209 | throw new FallenException('data type', $dataType); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $isBlob; |
||
| 213 | } |
||
| 214 | |||
| 215 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 216 | /** |
||
| 217 | * Returns the corresponding PHP type declaration of a MySQL column type. |
||
| 218 | * |
||
| 219 | * @param string $phpTypeHint The PHP type hinting. |
||
| 220 | */ |
||
| 221 | public static function phpTypeHintingToPhpTypeDeclaration(string $phpTypeHint): string |
||
| 257 | } |
||
| 258 | |||
| 259 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 260 | /** |
||
| 261 | * @inheritdoc |
||
| 262 | */ |
||
| 263 | public function allColumnTypes(): array |
||
| 264 | { |
||
| 265 | return ['int', |
||
| 266 | 'smallint', |
||
| 267 | 'tinyint', |
||
| 268 | 'mediumint', |
||
| 269 | 'bigint', |
||
| 270 | 'decimal', |
||
| 271 | 'float', |
||
| 272 | 'double', |
||
| 273 | 'bit', |
||
| 274 | 'date', |
||
| 275 | 'datetime', |
||
| 276 | 'timestamp', |
||
| 277 | 'time', |
||
| 278 | 'year', |
||
| 279 | 'char', |
||
| 280 | 'varchar', |
||
| 281 | 'binary', |
||
| 282 | 'varbinary', |
||
| 283 | 'enum', |
||
| 284 | 'set', |
||
| 285 | 'inet4', |
||
| 286 | 'inet6', |
||
| 287 | 'tinyblob', |
||
| 288 | 'blob', |
||
| 289 | 'mediumblob', |
||
| 290 | 'longblob', |
||
| 291 | 'tinytext', |
||
| 292 | 'text', |
||
| 293 | 'mediumtext', |
||
| 294 | 'longtext']; |
||
| 295 | } |
||
| 296 | |||
| 297 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 298 | /** |
||
| 299 | * Returns the corresponding PHP type hinting of a MySQL column type. |
||
| 300 | * |
||
| 301 | * @param string[] $dataTypeInfo Metadata of the MySQL data type. |
||
| 302 | */ |
||
| 303 | public function columnTypeToPhpType(array $dataTypeInfo): string |
||
| 304 | { |
||
| 305 | switch ($dataTypeInfo['data_type']) |
||
| 306 | { |
||
| 307 | case 'tinyint': |
||
| 308 | case 'smallint': |
||
| 309 | case 'mediumint': |
||
| 310 | case 'int': |
||
| 311 | case 'bigint': |
||
| 312 | case 'year': |
||
| 313 | $phpType = 'int'; |
||
| 314 | break; |
||
| 315 | |||
| 316 | case 'decimal': |
||
| 317 | $phpType = 'int|float|string'; |
||
| 318 | break; |
||
| 319 | |||
| 320 | case 'float': |
||
| 321 | case 'double': |
||
| 322 | $phpType = 'float'; |
||
| 323 | break; |
||
| 324 | |||
| 325 | case 'bit': |
||
| 326 | case 'varbinary': |
||
| 327 | case 'binary': |
||
| 328 | case 'char': |
||
| 329 | case 'varchar': |
||
| 330 | case 'time': |
||
| 331 | case 'timestamp': |
||
| 332 | case 'date': |
||
| 333 | case 'datetime': |
||
| 334 | case 'enum': |
||
| 335 | case 'inet4': |
||
| 336 | case 'inet6': |
||
| 337 | case 'set': |
||
| 338 | case 'tinytext': |
||
| 339 | case 'text': |
||
| 340 | case 'mediumtext': |
||
| 341 | case 'longtext': |
||
| 342 | case 'tinyblob': |
||
| 343 | case 'blob': |
||
| 344 | case 'mediumblob': |
||
| 345 | case 'longblob': |
||
| 346 | $phpType = 'string'; |
||
| 347 | break; |
||
| 348 | |||
| 349 | case 'list_of_int': |
||
| 350 | $phpType = 'array|string'; |
||
| 351 | break; |
||
| 352 | |||
| 353 | default: |
||
| 354 | throw new FallenException('data type', $dataTypeInfo['data_type']); |
||
| 355 | } |
||
| 356 | |||
| 357 | return $phpType; |
||
| 358 | } |
||
| 359 | |||
| 360 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 361 | /** |
||
| 362 | * Returns PHP code escaping the value of a PHP expression that can be safely used when concatenating a SQL statement. |
||
| 363 | * |
||
| 364 | * @param array $dataTypeInfo Metadata of the column on which the field is based. |
||
| 365 | * @param string $expression The PHP expression. |
||
| 366 | */ |
||
| 367 | public function escapePhpExpression(array $dataTypeInfo, string $expression): string |
||
| 431 | } |
||
| 432 | |||
| 433 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 434 | } |
||
| 435 | |||
| 437 |