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 TMysqlMetaData 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 TMysqlMetaData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class TMysqlMetaData extends TDbMetaData |
||
| 33 | { |
||
| 34 | private $_serverVersion = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return string TDbTableInfo class name. |
||
| 38 | */ |
||
| 39 | protected function getTableInfoClass() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return string TDbTableColumn class name. |
||
| 46 | */ |
||
| 47 | protected function getTableColumnClass() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Quotes a table name for use in a query. |
||
| 54 | * @param string $name $name table name |
||
| 55 | * @return string the properly quoted table name |
||
| 56 | */ |
||
| 57 | public function quoteTableName($name) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Quotes a column name for use in a query. |
||
| 64 | * @param string $name $name column name |
||
| 65 | * @return string the properly quoted column name |
||
| 66 | */ |
||
| 67 | public function quoteColumnName($name) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Quotes a column alias for use in a query. |
||
| 74 | * @param string $name $name column alias |
||
| 75 | * @return string the properly quoted column alias |
||
| 76 | */ |
||
| 77 | public function quoteColumnAlias($name) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the column definitions for given table. |
||
| 84 | * @param string $table table name. |
||
| 85 | * @return TMysqlTableInfo table information. |
||
| 86 | */ |
||
| 87 | protected function createTableInfo($table) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return float server version. |
||
| 115 | */ |
||
| 116 | protected function getServerVersion() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param TMysqlTableInfo $tableInfo table information. |
||
| 129 | * @param array $col column information. |
||
| 130 | */ |
||
| 131 | protected function processColumn($tableInfo, $col) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param mixed $type |
||
| 186 | * @return bool true if column type if "numeric", "interval" or begins with "time". |
||
| 187 | */ |
||
| 188 | protected function isPrecisionType($type) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param mixed $type |
||
| 198 | * @return bool true if column type if "enum" or "set". |
||
| 199 | */ |
||
| 200 | protected function isEnumSetType($type) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $table table name, may be quoted with back-ticks and may contain database name. |
||
| 208 | * @throws TDbException when table name contains invalid identifier bytes. |
||
| 209 | * @return array tuple ($schema,$table), $schema may be null. |
||
| 210 | */ |
||
| 211 | protected function getSchemaTableName($table) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * http://dev.mysql.com/doc/refman/5.0/en/identifiers.html |
||
| 226 | * @param string $name identifier name |
||
| 227 | * @return bool true if valid identifier. |
||
| 228 | */ |
||
| 229 | protected function isValidIdentifier($name) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $table table schema name |
||
| 236 | * @return TMysqlTableInfo |
||
| 237 | */ |
||
| 238 | protected function createNewTableInfo($table) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * For MySQL version 5.0.1 or later we can use SHOW FULL TABLES |
||
| 253 | * http://dev.mysql.com/doc/refman/5.0/en/show-tables.html |
||
| 254 | * |
||
| 255 | * For MySQL version 5.0.1 or earlier, this always return false. |
||
| 256 | * @param string $schemaName database name, null to use default connection database. |
||
| 257 | * @param string $tableName table or view name. |
||
| 258 | * @throws TDbException if table or view does not exist. |
||
| 259 | * @return bool true if is view, false otherwise. |
||
| 260 | */ |
||
| 261 | protected function getIsView($schemaName, $tableName) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Gets the primary and foreign key column details for the given table. |
||
| 283 | * @param string $schemaName schema name |
||
| 284 | * @param string $tableName table name. |
||
| 285 | * @return array tuple ($primary, $foreign) |
||
| 286 | */ |
||
| 287 | protected function getConstraintKeys($schemaName, $tableName) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Gets foreign relationship constraint keys and table name |
||
| 310 | * @param string $schemaName database name |
||
| 311 | * @param string $tableName table name |
||
| 312 | * @return array foreign relationship table name and keys. |
||
| 313 | */ |
||
| 314 | protected function getForeignConstraints($schemaName, $tableName) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $schemaName database name |
||
| 346 | * @param string $tableName table name |
||
| 347 | * @throws TDbException if PHP version is less than 5.1.3 |
||
| 348 | * @return string SQL command to create the table. |
||
| 349 | */ |
||
| 350 | protected function getShowCreateTable($schemaName, $tableName) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Extract foreign key constraints by extracting the contraints from SHOW CREATE TABLE result. |
||
| 370 | * @param string $schemaName database name |
||
| 371 | * @param string $tableName table name |
||
| 372 | * @return array foreign relationship table name and keys. |
||
| 373 | */ |
||
| 374 | protected function findForeignConstraints($schemaName, $tableName) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $columnId column name. |
||
| 395 | * @param TMysqlTableInfo $tableInfo table information. |
||
| 396 | * @return bool true if column is a foreign key. |
||
| 397 | */ |
||
| 398 | View Code Duplication | protected function isForeignKeyColumn($columnId, $tableInfo) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Returns all table names in the database. |
||
| 410 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
| 411 | * If not empty, the returned table names will be prefixed with the schema name. |
||
| 412 | * @return array all table names in the database. |
||
| 413 | */ |
||
| 414 | public function findTableNames($schema = '') |
||
| 425 | } |
||
| 426 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.