| Conditions | 3 |
| Paths | 15 |
| Total Lines | 55 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 |
||
| 129 | public function testUpsert() |
||
| 130 | { |
||
| 131 | try { |
||
| 132 | $this->pdo->exec('BEGIN'); |
||
| 133 | |||
| 134 | // INSERT |
||
| 135 | $query = $this->sut->insert() |
||
| 136 | ->into(new Table('offices')) |
||
| 137 | ->columns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
||
| 138 | ->values("'abc'", "'Berlin'", "'+49 101 123 4567'", "''", "'Germany'", "'10111'", "'NA'") |
||
| 139 | ->values("'bcd'", "'Budapest'", "'+36 70 101 1234'", "''", "'Hungary'", "'1011'", "'NA'") |
||
| 140 | ->returning('*'); |
||
| 141 | |||
| 142 | $values = $this->pdoWrapper->fetchAll($query, \PDO::FETCH_ASSOC); |
||
| 143 | |||
| 144 | $expectedValues = [ |
||
| 145 | [ |
||
| 146 | 'officecode' => 'abc', |
||
| 147 | 'city' => 'Berlin', |
||
| 148 | 'phone' => '+49 101 123 4567', |
||
| 149 | 'addressline1' => '', |
||
| 150 | 'addressline2' => null, |
||
| 151 | 'state' => null, |
||
| 152 | 'country' => 'Germany', |
||
| 153 | 'postalcode' => '10111', |
||
| 154 | 'territory' => 'NA', |
||
| 155 | ], |
||
| 156 | [ |
||
| 157 | 'officecode' => 'bcd', |
||
| 158 | 'city' => 'Budapest', |
||
| 159 | 'phone' => '+36 70 101 1234', |
||
| 160 | 'addressline1' => '', |
||
| 161 | 'addressline2' => null, |
||
| 162 | 'state' => null, |
||
| 163 | 'country' => 'Hungary', |
||
| 164 | 'postalcode' => '1011', |
||
| 165 | 'territory' => 'NA', |
||
| 166 | ], |
||
| 167 | ]; |
||
| 168 | $this->assertSame($expectedValues, $values); |
||
| 169 | |||
| 170 | // DELETE |
||
| 171 | $query = $this->sut->delete() |
||
| 172 | ->from(new Table('offices')) |
||
| 173 | ->where(new Expr('officeCode IN (?)', [['abc', 'bcd']])); |
||
| 174 | |||
| 175 | $this->assertTrue($this->pdoWrapper->execute($query)); |
||
| 176 | |||
| 177 | // COMMIT |
||
| 178 | $this->pdo->exec('COMMIT'); |
||
| 179 | } catch (\Exception $e) { |
||
| 180 | if ($this->pdo->inTransaction()) { |
||
| 181 | $this->pdo->exec('ROLLBACK'); |
||
| 182 | } |
||
| 183 | $this->fail($e->getMessage() . PHP_EOL . $e->getTraceAsString()); |
||
| 184 | } |
||
| 187 |