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:
| 1 | <?php |
||
| 34 | abstract class InformationSchemaDescriptor extends \ntentan\atiaa\Descriptor |
||
| 35 | { |
||
| 36 | 7 | protected function getColumns(&$table) |
|
| 37 | { |
||
| 38 | 7 | return $this->driver->quotedQuery( |
|
| 39 | 7 | 'select |
|
| 40 | "column_name" as "name", |
||
| 41 | "data_type" as "type", |
||
| 42 | "is_nullable" as "nulls", |
||
| 43 | "column_default" as "default", |
||
| 44 | "character_maximum_length" as "length" |
||
| 45 | from "information_schema"."columns" |
||
| 46 | where "table_name" = ? and "table_schema"=? |
||
| 47 | order by "column_name"', |
||
| 48 | array( |
||
| 49 | 7 | $table['name'], |
|
| 50 | 7 | $table['schema'] |
|
| 51 | ) |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | 11 | protected function getTables($schema, $tables, $includeViews) |
|
| 56 | { |
||
| 57 | 11 | View Code Duplication | if($includeViews) |
| 58 | { |
||
| 59 | 5 | $condition = "(table_type = ? or table_type = ?)"; |
|
| 60 | 5 | $bind = array('BASE TABLE', 'VIEW'); |
|
| 61 | } |
||
| 62 | else |
||
| 63 | { |
||
| 64 | 6 | $condition = "table_type = ?"; |
|
| 65 | 6 | $bind = array('BASE TABLE'); |
|
| 66 | } |
||
| 67 | |||
| 68 | 11 | if(count($tables) > 0) |
|
| 69 | { |
||
| 70 | 7 | return $this->driver->quotedQuery( |
|
| 71 | 'select "table_schema" as "schema", "table_name" as "name" |
||
| 72 | from "information_schema"."tables" |
||
| 73 | 7 | where ' . $condition . ' and table_schema = ? |
|
| 74 | 7 | and table_name in (?' . str_repeat(', ?', count($tables) - 1) . ') |
|
| 75 | order by "table_name"', |
||
| 76 | 7 | array_merge($bind, array($schema), $tables) |
|
| 77 | ); |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | 4 | return $this->driver->quotedQuery( |
|
| 82 | 'select "table_schema" as "schema", "table_name" as "name" |
||
| 83 | from "information_schema"."tables" |
||
| 84 | 4 | where ' . $condition . ' and table_schema = ? order by "table_name"', |
|
| 85 | 4 | array_merge($bind, array($schema)) |
|
| 86 | ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | 7 | protected function getPrimaryKey(&$table) |
|
| 94 | |||
| 95 | 7 | protected function getUniqueKeys(&$table) |
|
| 96 | { |
||
| 97 | 7 | return $this->getConstraints($table, 'UNIQUE'); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $type |
||
| 102 | */ |
||
| 103 | 7 | private function getConstraints($table, $type) |
|
| 117 | |||
| 118 | 4 | protected function getViews(&$schema) |
|
| 127 | } |
||
| 128 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.