Conditions | 12 |
Paths | 84 |
Total Lines | 55 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
18 | public function __construct(Table $model, CrossForeignKeys $cfk) { |
||
19 | $this->model = $model; |
||
20 | $this->cfk = $cfk; |
||
21 | |||
22 | $fkTables = new Map(); |
||
23 | foreach ($cfk->getMiddleTable()->getForeignKeys() as $fk) { |
||
24 | if (!$fkTables->has($fk->getForeignTableCommonName())) { |
||
25 | $fkTables->set($fk->getForeignTableCommonName(), 0); |
||
26 | } |
||
27 | $fkTables->set($fk->getForeignTableCommonName(), $fkTables->get($fk->getForeignTableCommonName()) + 1); |
||
28 | } |
||
29 | |||
30 | $idColumns = []; |
||
31 | if ($fkTables->get($model->getCommonName()) > 1) { |
||
32 | $name = ''; |
||
33 | $splits = explode('_', $cfk->getMiddleTable()->getOriginCommonName()); |
||
34 | foreach ($splits as $split) { |
||
35 | if (empty($name)) { |
||
36 | $name = $split; |
||
37 | } else { |
||
38 | $name .= '_' . $split; |
||
39 | $idColumns []= $split . '_id'; |
||
40 | } |
||
41 | |||
42 | $idColumns []= $name . '_id'; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | foreach ($cfk->getMiddleTable()->getForeignKeys() as $fk) { |
||
47 | // looks like a many-to-many parent + child relationship |
||
48 | if ($fkTables->get($model->getCommonName()) > 1) { |
||
49 | if (in_array($fk->getLocalColumnName(), $idColumns)) { |
||
50 | $this->lk = $fk; |
||
51 | } else { |
||
52 | $this->fk = $fk; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | // normal many-to-many relationship |
||
57 | else { |
||
58 | if ($fk->getForeignTable() != $model) { |
||
59 | $this->fk = $fk; |
||
60 | } else if ($fk->getForeignTable() == $model) { |
||
61 | $this->lk = $fk; |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if ($this->fk === null) { |
||
67 | echo $cfk->getMiddleTable()->getOriginCommonName() . "\n"; |
||
68 | echo $cfk->getTable()->getOriginCommonName() . "\n"; |
||
69 | } |
||
70 | |||
71 | $this->foreign = $this->fk->getForeignTable(); |
||
72 | } |
||
73 | |||
128 |