| Conditions | 5 |
| Paths | 5 |
| Total Lines | 70 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 36 | public function save() |
||
| 37 | { |
||
| 38 | if ($this->isNew()) { |
||
| 39 | // insert |
||
| 40 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 41 | INSERT INTO domain ( |
||
| 42 | shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, |
||
| 43 | emailsender, notificationtarget |
||
| 44 | ) VALUES ( |
||
| 45 | :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage, |
||
| 46 | :emailsender, :notificationtarget |
||
| 47 | ); |
||
| 48 | SQL |
||
| 49 | ); |
||
| 50 | |||
| 51 | $statement->bindValue(":shortname", $this->shortname); |
||
| 52 | $statement->bindValue(":longname", $this->longname); |
||
| 53 | $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath); |
||
| 54 | $statement->bindValue(":wikiapipath", $this->wikiapipath); |
||
| 55 | $statement->bindValue(":enabled", $this->enabled); |
||
| 56 | $statement->bindValue(":defaultclose", $this->defaultclose); |
||
| 57 | $statement->bindValue(":defaultlanguage", $this->defaultlanguage); |
||
| 58 | $statement->bindValue(":emailsender", $this->emailsender); |
||
| 59 | $statement->bindValue(":notificationtarget", $this->notificationtarget); |
||
| 60 | |||
| 61 | if ($statement->execute()) { |
||
| 62 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 63 | } |
||
| 64 | else { |
||
| 65 | throw new Exception($statement->errorInfo()); |
||
|
|
|||
| 66 | } |
||
| 67 | } |
||
| 68 | else { |
||
| 69 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 70 | UPDATE domain SET |
||
| 71 | longname = :longname, |
||
| 72 | wikiarticlepath = :wikiarticlepath, |
||
| 73 | wikiapipath = :wikiapipath, |
||
| 74 | enabled = :enabled, |
||
| 75 | defaultclose = :defaultclose, |
||
| 76 | defaultlanguage = :defaultlanguage, |
||
| 77 | emailsender = :emailsender, |
||
| 78 | notificationtarget = :notificationtarget, |
||
| 79 | |||
| 80 | updateversion = updateversion + 1 |
||
| 81 | WHERE id = :id AND updateversion = :updateversion; |
||
| 82 | SQL |
||
| 83 | ); |
||
| 84 | |||
| 85 | $statement->bindValue(":longname", $this->longname); |
||
| 86 | $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath); |
||
| 87 | $statement->bindValue(":wikiapipath", $this->wikiapipath); |
||
| 88 | $statement->bindValue(":enabled", $this->enabled); |
||
| 89 | $statement->bindValue(":defaultclose", $this->defaultclose); |
||
| 90 | $statement->bindValue(":defaultlanguage", $this->defaultlanguage); |
||
| 91 | $statement->bindValue(":emailsender", $this->emailsender); |
||
| 92 | $statement->bindValue(":notificationtarget", $this->notificationtarget); |
||
| 93 | |||
| 94 | $statement->bindValue(':id', $this->id); |
||
| 95 | $statement->bindValue(':updateversion', $this->updateversion); |
||
| 96 | |||
| 97 | if (!$statement->execute()) { |
||
| 98 | throw new Exception($statement->errorInfo()); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($statement->rowCount() !== 1) { |
||
| 102 | throw new OptimisticLockFailedException(); |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->updateversion++; |
||
| 106 | } |
||
| 254 | } |