| Conditions | 9 |
| Paths | 73 |
| Total Lines | 51 |
| Code Lines | 33 |
| 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 |
||
| 69 | protected function maybeUpdatePivotRow() |
||
| 70 | { |
||
| 71 | if (! $this->relation instanceof ManyToMany) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | $conn = $this->relation->getNativeMapper()->getWriteConnection(); |
||
| 76 | $throughTable = (string)$this->relation->getOption(RelationConfig::THROUGH_TABLE); |
||
| 77 | |||
| 78 | $throughNativeColumns = (array)$this->relation->getOption(RelationConfig::THROUGH_NATIVE_COLUMN); |
||
| 79 | $throughForeignColumns = (array)$this->relation->getOption(RelationConfig::THROUGH_FOREIGN_COLUMN); |
||
| 80 | $nativeKey = (array)$this->getNativeEntityHydrator()->getPk($this->nativeEntity); |
||
| 81 | $foreignKey = (array)$this->getForeignEntityHydrator()->getPk($this->foreignEntity); |
||
| 82 | |||
| 83 | // first delete existing pivot row |
||
| 84 | $delete = new \Sirius\Sql\Delete($conn); |
||
| 85 | $delete->from($throughTable); |
||
| 86 | foreach ($throughNativeColumns as $k => $col) { |
||
| 87 | $delete->where($col, $nativeKey[$k]); |
||
| 88 | $delete->where($throughForeignColumns[$k], $foreignKey[$k]); |
||
| 89 | } |
||
| 90 | foreach ((array)$this->relation->getOption(RelationConfig::THROUGH_GUARDS) as $col => $value) { |
||
| 91 | if (! is_int($col)) { |
||
| 92 | $delete->where($col, $value); |
||
| 93 | } else { |
||
| 94 | $delete->where($value); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | $delete->perform(); |
||
| 98 | |||
| 99 | $insertColumns = []; |
||
| 100 | foreach ($throughNativeColumns as $k => $col) { |
||
| 101 | $insertColumns[$col] = $nativeKey[$k]; |
||
| 102 | $insertColumns[$throughForeignColumns[$k]] = $foreignKey[$k]; |
||
| 103 | } |
||
| 104 | |||
| 105 | foreach ((array)$this->relation->getOption(RelationConfig::THROUGH_COLUMNS) as $col => $alias) { |
||
| 106 | $insertColumns[$col] = $this->getForeignEntityHydrator() |
||
| 107 | ->get($this->foreignEntity, $alias); |
||
| 108 | } |
||
| 109 | |||
| 110 | foreach ((array)$this->relation->getOption(RelationConfig::THROUGH_GUARDS) as $col => $value) { |
||
| 111 | if (! is_int($col)) { |
||
| 112 | $insertColumns[$col] = $value; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $insert = new \Sirius\Sql\Insert($conn); |
||
| 117 | $insert->into($throughTable) |
||
| 118 | ->columns($insertColumns) |
||
| 119 | ->perform(); |
||
| 120 | } |
||
| 132 |