Conditions | 5 |
Paths | 53 |
Total Lines | 71 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 | |||
137 | ->setReturning('*'); |
||
138 | $sql = (string)$query; |
||
139 | |||
140 | $statement = $this->pdo->prepare($sql); |
||
141 | |||
142 | $result = $statement->execute($query->getValues()); |
||
143 | $this->assertTrue($result); |
||
144 | |||
145 | $values = $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
146 | |||
147 | $expectedValues = [ |
||
148 | [ |
||
149 | 'officecode' => 'abc', |
||
150 | 'city' => 'Berlin', |
||
151 | 'phone' => '+49 101 123 4567', |
||
152 | 'addressline1' => '', |
||
153 | 'addressline2' => null, |
||
154 | 'state' => null, |
||
155 | 'country' => 'Germany', |
||
156 | 'postalcode' => '10111', |
||
157 | 'territory' => 'NA', |
||
158 | ], |
||
159 | [ |
||
160 | 'officecode' => 'bcd', |
||
161 | 'city' => 'Budapest', |
||
162 | 'phone' => '+36 70 101 1234', |
||
163 | 'addressline1' => '', |
||
164 | 'addressline2' => null, |
||
165 | 'state' => null, |
||
166 | 'country' => 'Hungary', |
||
167 | 'postalcode' => '1011', |
||
168 | 'territory' => 'NA', |
||
169 | ], |
||
170 | ]; |
||
171 | $this->assertSame($expectedValues, $values); |
||
172 | |||
173 | // DELETE |
||
174 | $query = $this->sut->delete() |
||
175 | ->addFrom(new Table('offices')) |
||
176 | ->addWhere(new Expr('officeCode IN (?)', [['abc', 'bcd']])); |
||
177 | |||
178 | $statement = $this->pdo->prepare((string)$query); |
||
179 | foreach ($query->getParams() as $param => $var) { |
||
180 | if (is_numeric($param)) { |
||
181 | $statement->bindParam($param + 1, $var[0], $var[1]); |
||
182 | } else { |
||
183 | $statement->bindParam($param, $var[0], $var[1]); |
||
184 | } |
||
185 | } |
||
186 | $result = $statement->execute(); |
||
187 | $this->assertTrue($result); |
||
188 | |||
189 | // COMMIT |
||
190 | $this->pdo->exec('COMMIT'); |
||
191 | } catch (\Exception $e) { |
||
192 | if ($this->pdo->inTransaction()) { |
||
193 | $this->pdo->exec('ROLLBACK'); |
||
194 | } |
||
195 | $this->fail($e->getMessage() . PHP_EOL . $e->getTraceAsString()); |
||
196 | } |
||
199 |