Conditions | 5 |
Paths | 49 |
Total Lines | 74 |
Code Lines | 51 |
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 |
||
160 | public function testUpsert() |
||
161 | { |
||
162 | try { |
||
163 | $this->pdo->exec('BEGIN'); |
||
164 | |||
165 | // INSERT |
||
166 | $query = $this->sut->insert() |
||
167 | ->setInto(new Table('offices')) |
||
168 | ->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
||
169 | ->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
||
170 | ->addValues('bcd', 'Budapest', '+36 70 101 1234', '', 'Hungary', '1011', 'NA') |
||
171 | |||
172 | ->setReturning('*'); |
||
173 | $sql = (string)$query; |
||
174 | |||
175 | $statement = $this->pdo->prepare($sql); |
||
176 | |||
177 | $result = $statement->execute($query->getValues()); |
||
178 | $this->assertTrue($result); |
||
179 | |||
180 | $values = $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
181 | |||
182 | $expectedValues = [ |
||
183 | [ |
||
184 | 'officecode' => 'abc', |
||
185 | 'city' => 'Berlin', |
||
186 | 'phone' => '+49 101 123 4567', |
||
187 | 'addressline1' => '', |
||
188 | 'addressline2' => null, |
||
189 | 'state' => null, |
||
190 | 'country' => 'Germany', |
||
191 | 'postalcode' => '10111', |
||
192 | 'territory' => 'NA', |
||
193 | ], |
||
194 | [ |
||
195 | 'officecode' => 'bcd', |
||
196 | 'city' => 'Budapest', |
||
197 | 'phone' => '+36 70 101 1234', |
||
198 | 'addressline1' => '', |
||
199 | 'addressline2' => null, |
||
200 | 'state' => null, |
||
201 | 'country' => 'Hungary', |
||
202 | 'postalcode' => '1011', |
||
203 | 'territory' => 'NA', |
||
204 | ], |
||
205 | ]; |
||
206 | $this->assertSame($expectedValues, $values); |
||
207 | |||
208 | // DELETE |
||
209 | $query = $this->sut->delete() |
||
210 | ->addFrom(new Table('offices')) |
||
211 | ->addWhere(new SuperExpr('officeCode IN (??)', [['abc', 'bcd']], '??')); |
||
212 | |||
213 | $sql = (string)$query; |
||
214 | $params = $query->getParams(); |
||
215 | |||
216 | $statement = $this->pdo->prepare($sql); |
||
217 | foreach ($params as $k => $v) { |
||
218 | if (is_numeric($k)) { |
||
219 | $statement->bindParam($k + 1, $v[0], $v[1]); |
||
220 | } else { |
||
221 | $statement->bindParam($k, $v[0], $v[1]); |
||
222 | } |
||
223 | } |
||
224 | $result = $statement->execute(); |
||
225 | $this->assertTrue($result); |
||
226 | |||
227 | // COMMIT |
||
228 | $this->pdo->exec('COMMIT'); |
||
229 | } catch (\Exception $e) { |
||
230 | if ($this->pdo->inTransaction()) { |
||
231 | $this->pdo->exec('ROLLBACK'); |
||
232 | } |
||
233 | $this->fail($e->getMessage() . PHP_EOL . $e->getTraceAsString()); |
||
234 | } |
||
237 |