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 |
||
| 5 | class SqliteDescriptor extends \ntentan\atiaa\Descriptor |
||
| 6 | { |
||
| 7 | |||
| 8 | 3 | protected function getColumns(&$table) |
|
| 9 | { |
||
| 10 | 3 | $pragmaColumns = $this->driver->query("PRAGMA table_info({$table['name']})"); |
|
| 11 | 3 | foreach ($pragmaColumns as $column) { |
|
| 12 | 3 | preg_match("/(?<type>[a-zA-Z]*)(\((?<length>[0-9]+)\))*/", $column['type'], $matches); |
|
| 13 | 3 | $columns[] = [ |
|
|
|
|||
| 14 | 3 | 'name' => $column['name'], |
|
| 15 | 3 | 'type' => $matches['type'], |
|
| 16 | 3 | 'nulls' => $column['notnull'] == '0', |
|
| 17 | 3 | 'default' => $column['dflt_value'], |
|
| 18 | 3 | 'length' => isset($matches['length']) ? $matches['length'] : null |
|
| 19 | ]; |
||
| 20 | } |
||
| 21 | |||
| 22 | 3 | return $columns; |
|
| 23 | } |
||
| 24 | |||
| 25 | 1 | protected function cleanDefaultValue($default) |
|
| 26 | { |
||
| 27 | 1 | if(preg_match("/(')?(?<value>.*)/", $default, $matches)) { |
|
| 28 | 1 | return substr($matches['value'], 0, strlen($matches['value']) - 1); |
|
| 29 | } else { |
||
| 30 | return null; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | 3 | protected function getForeignKeys(&$table) |
|
| 35 | { |
||
| 36 | 3 | $foreignKeys = []; |
|
| 37 | 3 | $pragmaColumns = $this->driver->query("pragma foreign_key_list({$table['name']})"); |
|
| 38 | 3 | foreach ($pragmaColumns as $i => $foreignKey) { |
|
| 39 | 2 | $foreignKeys[] = [ |
|
| 40 | 2 | 'name' => "{$table['name']}_{$foreignKey['table']}_{$i}_fk", |
|
| 41 | 2 | 'schema' => $table['schema'], |
|
| 42 | 2 | 'table' => $table['name'], |
|
| 43 | 2 | 'column' => $foreignKey['from'], |
|
| 44 | 2 | 'foreign_table' => $foreignKey['table'], |
|
| 45 | 2 | 'foreign_schema' => 'main', |
|
| 46 | 2 | 'foreign_column' => $foreignKey['to'], |
|
| 47 | 2 | 'on_update' => $foreignKey['on_update'], |
|
| 48 | 2 | 'on_delete' => $foreignKey['on_delete'] |
|
| 49 | ]; |
||
| 50 | } |
||
| 51 | |||
| 52 | 3 | return $foreignKeys; |
|
| 53 | } |
||
| 54 | |||
| 55 | 2 | private function extractIndexDetails($details, $index, &$indexDetails) |
|
| 56 | { |
||
| 57 | 2 | foreach ($details as $detail) { |
|
| 58 | 2 | if ($detail['name'] != '') { |
|
| 59 | 2 | $indexDetails[] = [ |
|
| 60 | 2 | 'column' => $detail['name'], |
|
| 61 | 2 | 'name' => $index['name'], |
|
| 62 | 2 | 'schema' => $index['schema'] |
|
| 63 | ]; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | 2 | } |
|
| 67 | |||
| 68 | 3 | private function getIndexDetails($table, $unique) |
|
| 69 | { |
||
| 70 | 3 | $indices = $this->driver->query("pragma index_list({$table['name']})"); |
|
| 71 | 3 | $indexDetails = []; |
|
| 72 | |||
| 73 | 3 | foreach ($indices as $index) { |
|
| 74 | 2 | if ($index['unique'] == $unique) { |
|
| 75 | 2 | $index['schema'] = $table['schema']; |
|
| 76 | 2 | $detail = $this->driver->query("pragma index_info({$index['name']})"); |
|
| 77 | 2 | $this->extractIndexDetails($detail, $index, $indexDetails); |
|
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | 3 | return $indexDetails; |
|
| 82 | } |
||
| 83 | |||
| 84 | 3 | protected function getIndices(&$table) |
|
| 88 | |||
| 89 | 3 | protected function getPrimaryKey(&$table) |
|
| 90 | { |
||
| 91 | 3 | $keyColumns = []; |
|
| 92 | 3 | $pragmaColumns = $this->driver->query("PRAGMA table_info({$table['name']})"); |
|
| 93 | 3 | foreach ($pragmaColumns as $column) { |
|
| 94 | 3 | if ($column['pk'] > 0) { |
|
| 95 | 2 | $keyColumns[] = [ |
|
| 96 | 2 | 'order' => $column['pk'], |
|
| 97 | 2 | 'column' => $column['name'], |
|
| 98 | 3 | 'name' => "{$table['name']}_pk" |
|
| 99 | ]; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | 3 | usort($keyColumns, function ($a, $b) { |
|
| 104 | return $a['order'] - $b['order']; |
||
| 105 | 3 | }); |
|
| 106 | 3 | return $keyColumns; |
|
| 107 | } |
||
| 108 | |||
| 109 | 2 | protected function getSchemata() |
|
| 113 | |||
| 114 | 5 | protected function getTables($schema, $tables, $includeViews) |
|
| 115 | { |
||
| 116 | 5 | View Code Duplication | if ($includeViews) { |
| 117 | 2 | $condition = "(type = ? or type = ?)"; |
|
| 118 | 2 | $bind = array('table', 'view'); |
|
| 119 | } else { |
||
| 120 | 3 | $condition = "type = ?"; |
|
| 121 | 3 | $bind = array('table'); |
|
| 122 | } |
||
| 123 | |||
| 124 | 5 | if (count($tables) > 0) { |
|
| 125 | 3 | return $this->driver->quotedQuery( |
|
| 126 | 'select name as "name", \'main\' as "schema" from sqlite_master |
||
| 127 | 3 | where ' . $condition . ' and name not in (\'sqlite_master\', \'sqlite_sequence\') and name in (?' . str_repeat(', ?', count($tables) - 1) . ') |
|
| 128 | 3 | order by name', array_merge($bind, $tables) |
|
| 129 | ); |
||
| 130 | } else { |
||
| 131 | 2 | return $this->driver->quotedQuery( |
|
| 132 | 'select name as "name", \'main\' as "schema" from sqlite_master |
||
| 133 | 2 | where name not in (\'sqlite_master\', \'sqlite_sequence\') and ' . $condition, array_merge($bind) |
|
| 134 | ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | 3 | protected function getUniqueKeys(&$table) |
|
| 142 | |||
| 143 | 2 | protected function getViews(&$schema) |
|
| 147 | |||
| 148 | 3 | protected function hasAutoIncrementingKey(&$table) |
|
| 157 | |||
| 158 | } |
||
| 159 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.