| 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 |
||
| 89 | |||
| 90 | public function testInsertUpdateDelete() |
||
| 91 | { |
||
| 92 | try { |
||
| 93 | $this->pdo->exec('BEGIN'); |
||
| 94 | |||
| 95 | // INSERT |
||
| 96 | $query = $this->sut->insert() |
||
| 97 | ->setInto(new Table('offices')) |
||
| 98 | ->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
||
| 99 | ->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA'); |
||
| 100 | |||
| 101 | $statement = $this->pdo->prepare((string)$query); |
||
| 102 | |||
| 103 | $result = $statement->execute($query->getValues()); |
||
| 104 | $this->assertTrue($result); |
||
| 105 | |||
| 106 | // UPDATE |
||
| 107 | $query = $this->sut->update() |
||
| 108 | ->addFrom(new Table('offices')) |
||
| 109 | ->setValues(['territory' => 'Berlin']) |
||
| 110 | ->addWhere('officeCode = \'oc\''); |
||
| 111 | |||
| 112 | $num = 1; |
||
| 113 | $sql = (string)$query; |
||
| 114 | $values = $query->getValues(); |
||
| 115 | $params = $query->getParams(); |
||
| 116 | |||
| 117 | $statement = $this->pdo->prepare($sql); |
||
| 118 | foreach ($values as $value) { |
||
| 119 | $statement->bindParam($num++, $value); |
||
| 120 | } |
||
| 121 | foreach ($params as $k => $v) { |
||
| 122 | if (is_numeric($k)) { |
||
| 123 | $statement->bindParam($num++, $v[0], $v[1]); |
||
| 124 | } else { |
||
| 125 | $statement->bindParam($k, $v[0], $v[1]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | $result = $statement->execute($values); |
||
| 129 | $this->assertTrue($result); |
||
| 130 | |||
| 131 | // DELETE |
||
| 132 | $query = $this->sut->delete() |
||
| 133 | ->addFrom(new Table('offices')) |
||
| 134 | ->addWhere(new Expr('officeCode = ?', ['abc'])); |
||
| 135 | |||
| 136 | $sql = (string)$query; |
||
| 137 | $params = $query->getParams(); |
||
| 138 | |||
| 139 | $statement = $this->pdo->prepare($sql); |
||
| 140 | foreach ($params as $k => $v) { |
||
| 141 | if (is_numeric($k)) { |
||
| 142 | $statement->bindParam($k + 1, $v[0], $v[1]); |
||
| 143 | } else { |
||
| 144 | $statement->bindParam($k, $v[0], $v[1]); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | $result = $statement->execute(); |
||
| 148 | $this->assertTrue($result); |
||
| 149 | |||
| 150 | // COMMIT |
||
| 151 | $this->pdo->exec('COMMIT'); |
||
| 152 | } catch (\Exception $e) { |
||
| 153 | if ($this->pdo->inTransaction()) { |
||
| 154 | $this->pdo->exec('ROLLBACK'); |
||
| 155 | } |
||
| 156 | $this->fail($e->getMessage() . PHP_EOL . $e->getTraceAsString()); |
||
| 237 |