Conditions | 8 |
Paths | 278 |
Total Lines | 67 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
122 | public function testInsertUpdateDelete() |
||
123 | { |
||
124 | try { |
||
125 | $this->pdo->exec('BEGIN'); |
||
126 | |||
127 | // INSERT |
||
128 | $query = $this->sut->insert() |
||
129 | ->addFrom(new Table('offices')) |
||
130 | ->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
||
131 | ->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA'); |
||
132 | |||
133 | $statement = $this->pdo->prepare((string)$query); |
||
134 | |||
135 | $result = $statement->execute($query->getValues()[0]); |
||
136 | $this->assertTrue($result); |
||
137 | |||
138 | // UPDATE |
||
139 | $query = $this->sut->update() |
||
140 | ->addFrom(new Table('offices')) |
||
141 | ->setValues(['territory' => 'Berlin']) |
||
142 | ->addWhere('officeCode = \'oc\''); |
||
143 | |||
144 | $num = 1; |
||
145 | $sql = (string)$query; |
||
146 | $values = $query->getValues(); |
||
147 | $params = $query->getParams(); |
||
148 | |||
149 | $statement = $this->pdo->prepare($sql); |
||
150 | foreach ($values as $value) { |
||
151 | $statement->bindParam($num++, $value); |
||
152 | } |
||
153 | foreach ($params as $k => $v) { |
||
154 | if (is_numeric($k)) { |
||
155 | $statement->bindParam($num++, $v[0], $v[1]); |
||
156 | } else { |
||
157 | $statement->bindParam($k, $v[0], $v[1]); |
||
158 | } |
||
159 | } |
||
160 | $result = $statement->execute($values); |
||
161 | $this->assertTrue($result); |
||
162 | |||
163 | // DELETE |
||
164 | $query = $this->sut->delete() |
||
165 | ->addFrom(new Table('offices')) |
||
166 | ->addWhere(new Expr('officeCode = ?', ['abc'])); |
||
167 | |||
168 | $sql = (string)$query; |
||
169 | $params = $query->getParams(); |
||
170 | |||
171 | $statement = $this->pdo->prepare($sql); |
||
172 | foreach ($params as $k => $v) { |
||
173 | if (is_numeric($k)) { |
||
174 | $statement->bindParam($k + 1, $v[0], $v[1]); |
||
175 | } else { |
||
176 | $statement->bindParam($k, $v[0], $v[1]); |
||
177 | } |
||
178 | } |
||
179 | $result = $statement->execute(); |
||
180 | $this->assertTrue($result); |
||
181 | |||
182 | // COMMIT |
||
183 | $this->pdo->exec('COMMIT'); |
||
184 | } catch (\Exception $e) { |
||
185 | if ($this->pdo->inTransaction()) { |
||
186 | $this->pdo->exec('ROLLBACK'); |
||
187 | } |
||
188 | $this->fail($e->getMessage()); |
||
189 | } |
||
192 |