| Conditions | 5 |
| Paths | 5 |
| Total Lines | 52 |
| Code Lines | 35 |
| 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 |
||
| 29 | public function save() |
||
| 30 | { |
||
| 31 | if ($this->isNew()) { |
||
| 32 | // insert |
||
| 33 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 34 | INSERT INTO oauthtoken ( user, token, secret, type, expiry ) |
||
| 35 | VALUES ( :user, :token, :secret, :type, :expiry ); |
||
| 36 | SQL |
||
| 37 | ); |
||
| 38 | $statement->bindValue(":user", $this->user); |
||
| 39 | $statement->bindValue(":token", $this->token); |
||
| 40 | $statement->bindValue(":secret", $this->secret); |
||
| 41 | $statement->bindValue(":type", $this->type); |
||
| 42 | $statement->bindValue(":expiry", $this->expiry); |
||
| 43 | |||
| 44 | if ($statement->execute()) { |
||
| 45 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 46 | } |
||
| 47 | else { |
||
| 48 | throw new Exception($statement->errorInfo()); |
||
|
|
|||
| 49 | } |
||
| 50 | } |
||
| 51 | else { |
||
| 52 | // update |
||
| 53 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 54 | UPDATE oauthtoken |
||
| 55 | SET token = :token |
||
| 56 | , secret = :secret |
||
| 57 | , type = :type |
||
| 58 | , expiry = :expiry |
||
| 59 | , updateversion = updateversion + 1 |
||
| 60 | WHERE id = :id AND updateversion = :updateversion; |
||
| 61 | SQL |
||
| 62 | ); |
||
| 63 | |||
| 64 | $statement->bindValue(':id', $this->id); |
||
| 65 | $statement->bindValue(':updateversion', $this->updateversion); |
||
| 66 | |||
| 67 | $statement->bindValue(":token", $this->token); |
||
| 68 | $statement->bindValue(":secret", $this->secret); |
||
| 69 | $statement->bindValue(":type", $this->type); |
||
| 70 | $statement->bindValue(":expiry", $this->expiry); |
||
| 71 | |||
| 72 | if (!$statement->execute()) { |
||
| 73 | throw new Exception($statement->errorInfo()); |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($statement->rowCount() !== 1) { |
||
| 77 | throw new OptimisticLockFailedException(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->updateversion++; |
||
| 81 | } |
||
| 167 | } |