Conditions | 11 |
Paths | 194 |
Total Lines | 49 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
130 | public function upsert(array $values, $uniqueBy, $update = null) |
||
131 | { |
||
132 | if (empty($values)) { |
||
133 | return 0; |
||
134 | } elseif ($update === []) { |
||
135 | return (int) $this->insert($values); |
||
136 | } |
||
137 | |||
138 | if (!is_array(reset($values))) { |
||
139 | $values = [$values]; |
||
140 | } |
||
141 | |||
142 | foreach($values as $key => $value) { |
||
143 | $values[$key] = Arr::undot($this->grammar->convertJsonFields($value)); |
||
144 | } |
||
145 | |||
146 | foreach($values as $key => $value) { |
||
147 | $values[$key] = $this->convertIdToKey($value); |
||
148 | } |
||
149 | |||
150 | foreach($values as $key => $value) { |
||
151 | foreach ($value as $dataKey => $data) { |
||
152 | $values[$key][$dataKey] = $this->bindValue($data, 'upsert'); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // unique id value should already be converted to _key |
||
157 | foreach ($uniqueBy as $key => $value) { |
||
158 | $uniqueBy[$key] = $this->convertIdToKey($value); |
||
159 | } |
||
160 | $uniqueBy = $this->grammar->convertJsonFields($uniqueBy); |
||
161 | |||
162 | if (is_null($update)) { |
||
163 | $update = array_keys(reset($values)); |
||
164 | } |
||
165 | foreach ($update as $key => $value) { |
||
166 | $update[$key] = $this->convertIdToKey($value); |
||
167 | } |
||
168 | $update = $this->grammar->convertJsonFields($update); |
||
169 | |||
170 | $this->applyBeforeQueryCallbacks(); |
||
171 | |||
172 | $bindings = $this->bindings['upsert']; |
||
173 | |||
174 | $aql = $this->grammar->compileUpsert($this, $values, (array) $uniqueBy, $update); |
||
175 | |||
176 | return $this->connection->affectingStatement( |
||
177 | $aql, |
||
178 | $bindings |
||
179 | ); |
||
182 |