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