| Conditions | 27 |
| Paths | 11 |
| Total Lines | 173 |
| 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 |
||
| 122 | public function table(string $table, bool $detectRelations = true) : Table |
||
| 123 | { |
||
| 124 | static $tables = []; |
||
| 125 | if (isset($tables[$table])) { |
||
| 126 | return $tables[$table]; |
||
| 127 | } |
||
| 128 | |||
| 129 | static $relationsT = null; |
||
| 130 | static $relationsR = null; |
||
| 131 | if (!isset($relationsT) || !isset($relationsR)) { |
||
| 132 | $relationsT = []; |
||
| 133 | $relationsR = []; |
||
| 134 | $col = Collection::from( |
||
| 135 | $this->query( |
||
| 136 | "SELECT |
||
| 137 | kc.table_name, |
||
| 138 | kc.column_name, |
||
| 139 | kc.constraint_name, |
||
| 140 | ct.table_name AS referenced_table_name, |
||
| 141 | (SELECT column_name |
||
| 142 | FROM information_schema.constraint_column_usage |
||
| 143 | WHERE constraint_name = kc.constraint_name AND table_name = ct.table_name |
||
| 144 | LIMIT 1 OFFSET kc.position_in_unique_constraint - 1 |
||
| 145 | ) AS referenced_column_name |
||
| 146 | FROM information_schema.key_column_usage kc |
||
| 147 | JOIN information_schema.constraint_table_usage ct ON kc.constraint_name = ct.constraint_name AND ct.table_schema = kc.table_schema |
||
| 148 | WHERE |
||
| 149 | kc.table_schema = ? AND kc.table_name IS NOT NULL AND kc.position_in_unique_constraint IS NOT NULL", |
||
| 150 | [ $this->connection['opts']['schema'] ?? $this->connection['name'] ] |
||
| 151 | ) |
||
| 152 | )->toArray(); |
||
| 153 | foreach ($col as $row) { |
||
| 154 | $relationsT[$row['table_name']][] = $row; |
||
| 155 | $relationsR[$row['referenced_table_name']][] = $row; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $columns = Collection::from($this |
||
| 160 | ->query( |
||
| 161 | "SELECT * FROM information_schema.columns WHERE table_name = ? AND table_schema = ?", |
||
| 162 | [ $table, $this->connection['opts']['schema'] ?? $this->connection['name'] ] |
||
| 163 | )) |
||
| 164 | ->mapKey(function ($v) { return $v['column_name']; }) |
||
| 165 | ->map(function ($v) { |
||
| 166 | $v['length'] = null; |
||
| 167 | if (!isset($v['data_type'])) { |
||
| 168 | return $v; |
||
| 169 | } |
||
| 170 | switch ($v['data_type']) { |
||
| 171 | case 'character': |
||
| 172 | case 'character varying': |
||
| 173 | $v['length'] = (int)$v['character_maximum_length']; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | return $v; |
||
| 177 | }) |
||
| 178 | ->toArray(); |
||
| 179 | if (!count($columns)) { |
||
| 180 | throw new DBException('Table not found by name'); |
||
| 181 | } |
||
| 182 | $pkname = Collection::from($this |
||
| 183 | ->query( |
||
| 184 | "SELECT constraint_name FROM information_schema.table_constraints |
||
| 185 | WHERE table_name = ? AND constraint_type = ? AND table_schema = ?", |
||
| 186 | [ $table, 'PRIMARY KEY', $this->connection['opts']['schema'] ?? $this->connection['name'] ] |
||
| 187 | )) |
||
| 188 | ->pluck('constraint_name') |
||
| 189 | ->value(); |
||
| 190 | $primary = []; |
||
| 191 | if ($pkname) { |
||
| 192 | $primary = Collection::from($this |
||
| 193 | ->query( |
||
| 194 | "SELECT column_name FROM information_schema.constraint_column_usage |
||
| 195 | WHERE table_name = ? AND constraint_name = ? AND table_schema = ?", |
||
| 196 | [ $table, $pkname, $this->connection['opts']['schema'] ?? $this->connection['name'] ] |
||
| 197 | )) |
||
| 198 | ->pluck('column_name') |
||
| 199 | ->toArray(); |
||
| 200 | } |
||
| 201 | $tables[$table] = $definition = (new Table($table)) |
||
| 202 | ->addColumns($columns) |
||
| 203 | ->setPrimaryKey($primary) |
||
| 204 | ->setComment(''); |
||
| 205 | |||
| 206 | if ($detectRelations) { |
||
| 207 | // relations where the current table is referenced |
||
| 208 | // assuming current table is on the "one" end having "many" records in the referencing table |
||
| 209 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
||
| 210 | $relations = []; |
||
| 211 | foreach ($relationsR[$table] ?? [] as $relation) { |
||
| 212 | $relations[$relation['constraint_name']]['table'] = $relation['table_name']; |
||
| 213 | $relations[$relation['constraint_name']]['keymap'][$relation['referenced_column_name']] = $relation['column_name']; |
||
| 214 | } |
||
| 215 | foreach ($relations as $data) { |
||
| 216 | $rtable = $this->table($data['table'], true); |
||
| 217 | $columns = []; |
||
| 218 | foreach ($rtable->getColumns() as $column) { |
||
| 219 | if (!in_array($column, $data['keymap'])) { |
||
| 220 | $columns[] = $column; |
||
| 221 | } |
||
| 222 | } |
||
| 223 | $foreign = []; |
||
| 224 | $usedcol = []; |
||
| 225 | if (count($columns)) { |
||
| 226 | foreach (Collection::from($relationsT[$data['table']] ?? []) |
||
| 227 | ->filter(function ($v) use ($columns) { |
||
| 228 | return in_array($v['column_name'], $columns); |
||
| 229 | }) as $relation |
||
| 230 | ) { |
||
| 231 | $foreign[$relation['constraint_name']]['table'] = $relation['referenced_table_name']; |
||
| 232 | $foreign[$relation['constraint_name']]['keymap'][$relation['column_name']] = $relation['referenced_column_name']; |
||
| 233 | $usedcol[] = $relation['column_name']; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | if (count($foreign) === 1 && !count(array_diff($columns, $usedcol))) { |
||
| 237 | $foreign = current($foreign); |
||
| 238 | $relname = $foreign['table']; |
||
| 239 | $cntr = 1; |
||
| 240 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 241 | $relname = $foreign['table'] . '_' . (++ $cntr); |
||
| 242 | } |
||
| 243 | $definition->addRelation( |
||
| 244 | new TableRelation( |
||
| 245 | $relname, |
||
| 246 | $this->table($foreign['table'], true), |
||
| 247 | $data['keymap'], |
||
| 248 | true, |
||
| 249 | $rtable, |
||
| 250 | $foreign['keymap'] |
||
| 251 | ) |
||
| 252 | ); |
||
| 253 | } else { |
||
| 254 | $relname = $data['table']; |
||
| 255 | $cntr = 1; |
||
| 256 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 257 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 258 | } |
||
| 259 | $definition->addRelation( |
||
| 260 | new TableRelation( |
||
| 261 | $relname, |
||
| 262 | $this->table($data['table'], true), |
||
| 263 | $data['keymap'], |
||
| 264 | true |
||
| 265 | ) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | // relations where the current table references another table |
||
| 270 | // assuming current table is linked to "one" record in the referenced table |
||
| 271 | // resulting in a "belongsTo" relationship |
||
| 272 | $relations = []; |
||
| 273 | foreach ($relationsT[$table] ?? [] as $relation) { |
||
| 274 | $relations[$relation['constraint_name']]['table'] = $relation['referenced_table_name']; |
||
| 275 | $relations[$relation['constraint_name']]['keymap'][$relation['column_name']] = $relation['referenced_column_name']; |
||
| 276 | } |
||
| 277 | foreach ($relations as $name => $data) { |
||
| 278 | $relname = $data['table']; |
||
| 279 | $cntr = 1; |
||
| 280 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 281 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 282 | } |
||
| 283 | $definition->addRelation( |
||
| 284 | new TableRelation( |
||
| 285 | $relname, |
||
| 286 | $this->table($data['table'], true), |
||
| 287 | $data['keymap'], |
||
| 288 | false |
||
| 289 | ) |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | return $definition->toLowerCase(); |
||
| 294 | } |
||
| 295 | public function tables() : array |
||
| 309 |