| Conditions | 31 |
| Paths | 6 |
| Total Lines | 240 |
| Code Lines | 161 |
| 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 |
||
| 115 | public function table( |
||
| 116 | string $table, |
||
| 117 | bool $detectRelations = true |
||
| 118 | ) : Table |
||
| 119 | { |
||
| 120 | static $tables = []; |
||
| 121 | if (isset($tables[$table])) { |
||
| 122 | return $tables[$table]; |
||
| 123 | } |
||
| 124 | |||
| 125 | $columns = Collection::from($this |
||
| 126 | ->query( |
||
| 127 | "SELECT * FROM all_tab_cols WHERE table_name = ? AND owner = ?", |
||
| 128 | [ strtoupper($table), $this->name() ] |
||
| 129 | )) |
||
| 130 | ->map(function ($v) { |
||
| 131 | $new = []; |
||
| 132 | foreach ($v as $kk => $vv) { |
||
| 133 | $new[strtoupper($kk)] = $vv; |
||
| 134 | } |
||
| 135 | return $new; |
||
| 136 | }) |
||
| 137 | ->mapKey(function ($v) { return $v['COLUMN_NAME']; }) |
||
| 138 | ->toArray(); |
||
| 139 | if (!count($columns)) { |
||
| 140 | throw new DBException('Table not found by name'); |
||
| 141 | } |
||
| 142 | $owner = $this->name(); // current($columns)['OWNER']; |
||
| 143 | $pkname = Collection::from($this |
||
| 144 | ->query( |
||
| 145 | "SELECT constraint_name FROM all_constraints |
||
| 146 | WHERE table_name = ? AND constraint_type = ? AND owner = ?", |
||
| 147 | [ strtoupper($table), 'P', $owner ] |
||
| 148 | )) |
||
| 149 | ->map(function ($v) { |
||
| 150 | $new = []; |
||
| 151 | foreach ($v as $kk => $vv) { |
||
| 152 | $new[strtoupper($kk)] = $vv; |
||
| 153 | } |
||
| 154 | return $new; |
||
| 155 | }) |
||
| 156 | ->pluck('CONSTRAINT_NAME') |
||
| 157 | ->value(); |
||
| 158 | $primary = []; |
||
| 159 | if ($pkname) { |
||
| 160 | $primary = Collection::from($this |
||
| 161 | ->query( |
||
| 162 | "SELECT column_name FROM all_cons_columns |
||
| 163 | WHERE table_name = ? AND constraint_name = ? AND owner = ?", |
||
| 164 | [ strtoupper($table), $pkname, $owner ] |
||
| 165 | )) |
||
| 166 | ->map(function ($v) { |
||
| 167 | $new = []; |
||
| 168 | foreach ($v as $kk => $vv) { |
||
| 169 | $new[strtoupper($kk)] = $vv; |
||
| 170 | } |
||
| 171 | return $new; |
||
| 172 | }) |
||
| 173 | ->pluck('COLUMN_NAME') |
||
| 174 | ->toArray(); |
||
| 175 | } |
||
| 176 | $tables[$table] = $definition = (new Table($table)) |
||
| 177 | ->addColumns($columns) |
||
| 178 | ->setPrimaryKey($primary) |
||
| 179 | ->setComment(''); |
||
| 180 | |||
| 181 | if ($detectRelations) { |
||
| 182 | // relations where the current table is referenced |
||
| 183 | // assuming current table is on the "one" end having "many" records in the referencing table |
||
| 184 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
||
| 185 | $relations = []; |
||
| 186 | foreach (Collection::from($this |
||
| 187 | ->query( |
||
| 188 | "SELECT ac.TABLE_NAME, ac.CONSTRAINT_NAME, cc.COLUMN_NAME, cc.POSITION |
||
| 189 | FROM all_constraints ac |
||
| 190 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
||
| 191 | WHERE ac.OWNER = ? AND ac.R_OWNER = ? AND ac.R_CONSTRAINT_NAME = ? AND ac.CONSTRAINT_TYPE = ? |
||
| 192 | ORDER BY cc.POSITION", |
||
| 193 | [ $owner, $owner, $pkname, 'R' ] |
||
| 194 | )) |
||
| 195 | ->map(function ($v) { |
||
| 196 | $new = []; |
||
| 197 | foreach ($v as $kk => $vv) { |
||
| 198 | $new[strtoupper($kk)] = $vv; |
||
| 199 | } |
||
| 200 | return $new; |
||
| 201 | }) |
||
| 202 | as $relation |
||
| 203 | ) { |
||
| 204 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_NAME']; |
||
| 205 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$primary[(int)$relation['POSITION']-1]] = $relation['COLUMN_NAME']; |
||
| 206 | } |
||
| 207 | foreach ($relations as $data) { |
||
| 208 | $rtable = $this->table($data['table'], true); // ?? $this->addTableByName($data['table'], false); |
||
| 209 | $columns = []; |
||
| 210 | foreach ($rtable->getColumns() as $column) { |
||
| 211 | if (!in_array($column, $data['keymap'])) { |
||
| 212 | $columns[] = $column; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | $foreign = []; |
||
| 216 | $usedcol = []; |
||
| 217 | if (count($columns)) { |
||
| 218 | foreach (Collection::from($this |
||
| 219 | ->query( |
||
| 220 | "SELECT |
||
| 221 | cc.COLUMN_NAME, ac.CONSTRAINT_NAME, rc.TABLE_NAME AS REFERENCED_TABLE_NAME, ac.R_CONSTRAINT_NAME |
||
| 222 | FROM all_constraints ac |
||
| 223 | JOIN all_constraints rc ON rc.CONSTRAINT_NAME = ac.R_CONSTRAINT_NAME AND rc.OWNER = ac.OWNER |
||
| 224 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
||
| 225 | WHERE |
||
| 226 | ac.OWNER = ? AND ac.R_OWNER = ? AND ac.TABLE_NAME = ? AND ac.CONSTRAINT_TYPE = ? AND |
||
| 227 | cc.COLUMN_NAME IN (??) |
||
| 228 | ORDER BY POSITION", |
||
| 229 | [ $owner, $owner, $data['table'], 'R', $columns ] |
||
| 230 | )) |
||
| 231 | ->map(function ($v) { |
||
| 232 | $new = []; |
||
| 233 | foreach ($v as $kk => $vv) { |
||
| 234 | $new[strtoupper($kk)] = $vv; |
||
| 235 | } |
||
| 236 | return $new; |
||
| 237 | }) as $relation |
||
| 238 | ) { |
||
| 239 | $foreign[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_NAME']; |
||
| 240 | $foreign[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = $relation['R_CONSTRAINT_NAME']; |
||
| 241 | $usedcol[] = $relation['COLUMN_NAME']; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | if (count($foreign) === 1 && !count(array_diff($columns, $usedcol))) { |
||
| 245 | $foreign = current($foreign); |
||
| 246 | $rcolumns = Collection::from($this |
||
| 247 | ->query( |
||
| 248 | "SELECT COLUMN_NAME FROM all_cons_columns WHERE OWNER = ? AND CONSTRAINT_NAME = ? ORDER BY POSITION", |
||
| 249 | [ $owner, current($foreign['keymap']) ] |
||
| 250 | )) |
||
| 251 | ->map(function ($v) { |
||
| 252 | $new = []; |
||
| 253 | foreach ($v as $kk => $vv) { |
||
| 254 | $new[strtoupper($kk)] = $vv; |
||
| 255 | } |
||
| 256 | return $new; |
||
| 257 | }) |
||
| 258 | ->pluck('COLUMN_NAME') |
||
| 259 | ->toArray(); |
||
| 260 | foreach ($foreign['keymap'] as $column => $related) { |
||
| 261 | $foreign['keymap'][$column] = array_shift($rcolumns); |
||
| 262 | } |
||
| 263 | $relname = $foreign['table']; |
||
| 264 | $cntr = 1; |
||
| 265 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 266 | $relname = $foreign['table'] . '_' . (++ $cntr); |
||
| 267 | } |
||
| 268 | $definition->addRelation( |
||
| 269 | new TableRelation( |
||
| 270 | $relname, |
||
| 271 | $this->table($foreign['table'], true), |
||
| 272 | $data['keymap'], |
||
| 273 | true, |
||
| 274 | $rtable, |
||
| 275 | $foreign['keymap'] |
||
| 276 | ) |
||
| 277 | ); |
||
| 278 | } else { |
||
| 279 | $relname = $data['table']; |
||
| 280 | $cntr = 1; |
||
| 281 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 282 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 283 | } |
||
| 284 | $definition->addRelation( |
||
| 285 | new TableRelation( |
||
| 286 | $relname, |
||
| 287 | $this->table($data['table'], true), |
||
| 288 | $data['keymap'], |
||
| 289 | true |
||
| 290 | ) |
||
| 291 | ); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | // relations where the current table references another table |
||
| 295 | // assuming current table is linked to "one" record in the referenced table |
||
| 296 | // resulting in a "belongsTo" relationship |
||
| 297 | $relations = []; |
||
| 298 | foreach (Collection::from($this |
||
| 299 | ->query( |
||
| 300 | "SELECT ac.CONSTRAINT_NAME, cc.COLUMN_NAME, rc.TABLE_NAME AS REFERENCED_TABLE_NAME, ac.R_CONSTRAINT_NAME |
||
| 301 | FROM all_constraints ac |
||
| 302 | JOIN all_constraints rc ON rc.CONSTRAINT_NAME = ac.R_CONSTRAINT_NAME AND rc.OWNER = ac.OWNER |
||
| 303 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
||
| 304 | WHERE ac.OWNER = ? AND ac.R_OWNER = ? AND ac.TABLE_NAME = ? AND ac.CONSTRAINT_TYPE = ? |
||
| 305 | ORDER BY cc.POSITION", |
||
| 306 | [ $owner, $owner, strtoupper($table), 'R' ] |
||
| 307 | )) |
||
| 308 | ->map(function ($v) { |
||
| 309 | $new = []; |
||
| 310 | foreach ($v as $kk => $vv) { |
||
| 311 | $new[strtoupper($kk)] = $vv; |
||
| 312 | } |
||
| 313 | return $new; |
||
| 314 | }) |
||
| 315 | as $relation |
||
| 316 | ) { |
||
| 317 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_NAME']; |
||
| 318 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = $relation['R_CONSTRAINT_NAME']; |
||
| 319 | } |
||
| 320 | foreach ($relations as $name => $data) { |
||
| 321 | $rcolumns = Collection::from($this |
||
| 322 | ->query( |
||
| 323 | "SELECT COLUMN_NAME FROM all_cons_columns WHERE OWNER = ? AND CONSTRAINT_NAME = ? ORDER BY POSITION", |
||
| 324 | [ $owner, current($data['keymap']) ] |
||
| 325 | )) |
||
| 326 | ->map(function ($v) { |
||
| 327 | $new = []; |
||
| 328 | foreach ($v as $kk => $vv) { |
||
| 329 | $new[strtoupper($kk)] = $vv; |
||
| 330 | } |
||
| 331 | return $new; |
||
| 332 | }) |
||
| 333 | ->pluck('COLUMN_NAME') |
||
| 334 | ->toArray(); |
||
| 335 | foreach ($data['keymap'] as $column => $related) { |
||
| 336 | $data['keymap'][$column] = array_shift($rcolumns); |
||
| 337 | } |
||
| 338 | $relname = $data['table']; |
||
| 339 | $cntr = 1; |
||
| 340 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 341 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 342 | } |
||
| 343 | $definition->addRelation( |
||
| 344 | new TableRelation( |
||
| 345 | $relname, |
||
| 346 | $this->table($data['table'], true), |
||
| 347 | $data['keymap'], |
||
| 348 | false |
||
| 349 | ) |
||
| 350 | ); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | return $definition->toLowerCase(); |
||
| 354 | } |
||
| 355 | public function tables() : array |
||
| 376 |
If you suppress an error, we recommend checking for the error condition explicitly: