| Conditions | 5 |
| Paths | 5 |
| Total Lines | 59 |
| Code Lines | 42 |
| 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 |
||
| 172 | public function save() |
||
| 173 | { |
||
| 174 | if ($this->isNew()) { |
||
| 175 | // insert |
||
| 176 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 177 | INSERT INTO credential ( updateversion, user, factor, type, data, version, timeout, disabled, priority ) |
||
| 178 | VALUES ( 0, :user, :factor, :type, :data, :version, :timeout, :disabled, :priority ); |
||
| 179 | SQL |
||
| 180 | ); |
||
| 181 | $statement->bindValue(":user", $this->user); |
||
| 182 | $statement->bindValue(":factor", $this->factor); |
||
| 183 | $statement->bindValue(":type", $this->type); |
||
| 184 | $statement->bindValue(":data", $this->data); |
||
| 185 | $statement->bindValue(":version", $this->version); |
||
| 186 | $statement->bindValue(":timeout", $this->timeout); |
||
| 187 | $statement->bindValue(":disabled", $this->disabled); |
||
| 188 | $statement->bindValue(":priority", $this->priority); |
||
| 189 | |||
| 190 | if ($statement->execute()) { |
||
| 191 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 192 | } |
||
| 193 | else { |
||
| 194 | throw new Exception($statement->errorInfo()); |
||
|
|
|||
| 195 | } |
||
| 196 | } |
||
| 197 | else { |
||
| 198 | // update |
||
| 199 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 200 | UPDATE credential |
||
| 201 | SET factor = :factor |
||
| 202 | , data = :data |
||
| 203 | , version = :version |
||
| 204 | , timeout = :timeout |
||
| 205 | , disabled = :disabled |
||
| 206 | , priority = :priority |
||
| 207 | , updateversion = updateversion + 1 |
||
| 208 | WHERE id = :id AND updateversion = :updateversion; |
||
| 209 | SQL |
||
| 210 | ); |
||
| 211 | |||
| 212 | $statement->bindValue(':id', $this->id); |
||
| 213 | $statement->bindValue(':updateversion', $this->updateversion); |
||
| 214 | |||
| 215 | $statement->bindValue(":factor", $this->factor); |
||
| 216 | $statement->bindValue(":data", $this->data); |
||
| 217 | $statement->bindValue(":version", $this->version); |
||
| 218 | $statement->bindValue(":timeout", $this->timeout); |
||
| 219 | $statement->bindValue(":disabled", $this->disabled); |
||
| 220 | $statement->bindValue(":priority", $this->priority); |
||
| 221 | |||
| 222 | if (!$statement->execute()) { |
||
| 223 | throw new Exception($statement->errorInfo()); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($statement->rowCount() !== 1) { |
||
| 227 | throw new OptimisticLockFailedException(); |
||
| 228 | } |
||
| 229 | |||
| 230 | $this->updateversion++; |
||
| 231 | } |
||
| 233 | } |