| Conditions | 22 |
| Paths | 4 |
| Total Lines | 160 |
| Code Lines | 108 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 89 | public function table(string $table, bool $detectRelations = true) : Table |
||
| 90 | { |
||
| 91 | static $tables = []; |
||
| 92 | if (isset($tables[$table])) { |
||
| 93 | return $tables[$table]; |
||
| 94 | } |
||
| 95 | |||
| 96 | $columns = Collection::from($this->query("SHOW FULL COLUMNS FROM {$table}")); |
||
| 97 | if (!count($columns)) { |
||
| 98 | throw new DBException('Table not found by name'); |
||
| 99 | } |
||
| 100 | $tables[$table] = $definition = (new Table($table)) |
||
| 101 | ->addColumns( |
||
| 102 | $columns |
||
| 103 | ->clone() |
||
| 104 | ->mapKey(function ($v) { return $v['Field']; }) |
||
| 105 | ->toArray() |
||
| 106 | ) |
||
| 107 | ->setPrimaryKey( |
||
| 108 | $columns |
||
| 109 | ->clone() |
||
| 110 | ->filter(function ($v) { return $v['Key'] === 'PRI'; }) |
||
| 111 | ->pluck('Field') |
||
| 112 | ->toArray() |
||
| 113 | ) |
||
| 114 | ->setComment( |
||
| 115 | (string)Collection::from($this |
||
| 116 | ->query( |
||
| 117 | "SELECT table_comment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?", |
||
| 118 | [ $this->connection['name'], $table ] |
||
| 119 | )) |
||
| 120 | ->pluck('table_comment') |
||
| 121 | ->value() |
||
| 122 | ); |
||
| 123 | |||
| 124 | if ($detectRelations) { |
||
| 125 | // relations where the current table is referenced |
||
| 126 | // assuming current table is on the "one" end having "many" records in the referencing table |
||
| 127 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
||
| 128 | $relations = []; |
||
| 129 | foreach ( |
||
| 130 | $this |
||
| 131 | ->query( |
||
| 132 | "SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME |
||
| 133 | FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
||
| 134 | WHERE TABLE_SCHEMA = ? AND REFERENCED_TABLE_SCHEMA = ? AND REFERENCED_TABLE_NAME = ?", |
||
| 135 | [ $this->connection['name'], $this->connection['name'], $table ] |
||
| 136 | ) as $relation |
||
| 137 | ) { |
||
| 138 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_NAME']; |
||
| 139 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$relation['REFERENCED_COLUMN_NAME']] = $relation['COLUMN_NAME']; |
||
| 140 | } |
||
| 141 | foreach ($relations as $data) { |
||
| 142 | $rtable = $this->table($data['table'], true); |
||
| 143 | $columns = []; |
||
| 144 | foreach ($rtable->getColumns() as $column) { |
||
| 145 | if (!in_array($column, $data['keymap'])) { |
||
| 146 | $columns[] = $column; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | $foreign = []; |
||
| 150 | $usedcol = []; |
||
| 151 | if (count($columns)) { |
||
| 152 | foreach (Collection::from($this |
||
| 153 | ->query( |
||
| 154 | "SELECT |
||
| 155 | TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, |
||
| 156 | REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME |
||
| 157 | FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
||
| 158 | WHERE |
||
| 159 | TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME IN (??) AND |
||
| 160 | REFERENCED_TABLE_NAME IS NOT NULL", |
||
| 161 | [ $this->connection['name'], $data['table'], $columns ] |
||
| 162 | )) |
||
| 163 | ->map(function ($v) { |
||
| 164 | $new = []; |
||
| 165 | foreach ($v as $kk => $vv) { |
||
| 166 | $new[strtoupper($kk)] = $vv; |
||
| 167 | } |
||
| 168 | return $new; |
||
| 169 | }) as $relation |
||
| 170 | ) { |
||
| 171 | $foreign[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_NAME']; |
||
| 172 | $foreign[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = $relation['REFERENCED_COLUMN_NAME']; |
||
| 173 | $usedcol[] = $relation['COLUMN_NAME']; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | if (count($foreign) === 1 && !count(array_diff($columns, $usedcol))) { |
||
| 177 | $foreign = current($foreign); |
||
| 178 | $relname = $foreign['table']; |
||
| 179 | $cntr = 1; |
||
| 180 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 181 | $relname = $foreign['table'] . '_' . (++ $cntr); |
||
| 182 | } |
||
| 183 | $definition->addRelation( |
||
| 184 | new TableRelation( |
||
| 185 | $relname, |
||
| 186 | $this->table($foreign['table'], true), |
||
| 187 | $data['keymap'], |
||
| 188 | true, |
||
| 189 | $rtable, |
||
| 190 | $foreign['keymap'] |
||
| 191 | ) |
||
| 192 | ); |
||
| 193 | } else { |
||
| 194 | $relname = $data['table']; |
||
| 195 | $cntr = 1; |
||
| 196 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 197 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 198 | } |
||
| 199 | $definition->addRelation( |
||
| 200 | new TableRelation( |
||
| 201 | $relname, |
||
| 202 | $this->table($data['table'], true), |
||
| 203 | $data['keymap'], |
||
| 204 | true |
||
| 205 | ) |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | // relations where the current table references another table |
||
| 210 | // assuming current table is linked to "one" record in the referenced table |
||
| 211 | // resulting in a "belongsTo" relationship |
||
| 212 | $relations = []; |
||
| 213 | foreach (Collection::from($this |
||
| 214 | ->query( |
||
| 215 | "SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME |
||
| 216 | FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
||
| 217 | WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND REFERENCED_TABLE_NAME IS NOT NULL", |
||
| 218 | [ $this->connection['name'], $table ] |
||
| 219 | )) |
||
| 220 | ->map(function ($v) { |
||
| 221 | $new = []; |
||
| 222 | foreach ($v as $kk => $vv) { |
||
| 223 | $new[strtoupper($kk)] = $vv; |
||
| 224 | } |
||
| 225 | return $new; |
||
| 226 | }) as $relation |
||
| 227 | ) { |
||
| 228 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_NAME']; |
||
| 229 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = $relation['REFERENCED_COLUMN_NAME']; |
||
| 230 | } |
||
| 231 | foreach ($relations as $name => $data) { |
||
| 232 | $relname = $data['table']; |
||
| 233 | $cntr = 1; |
||
| 234 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 235 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 236 | } |
||
| 237 | $definition->addRelation( |
||
| 238 | new TableRelation( |
||
| 239 | $relname, |
||
| 240 | $this->table($data['table'], true), |
||
| 241 | $data['keymap'], |
||
| 242 | false |
||
| 243 | ) |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | return $definition->toLowerCase(); |
||
| 248 | } |
||
| 249 | public function tables() : array |
||
| 269 | } |
If you suppress an error, we recommend checking for the error condition explicitly: