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