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