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 |
||
107 | public function save() |
||
108 | { |
||
109 | if ($this->isNew()) { |
||
110 | // insert |
||
111 | $statement = $this->dbObject->prepare(<<<SQL |
||
112 | INSERT INTO domain ( |
||
113 | shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, |
||
114 | emailsender, notificationtarget |
||
115 | ) VALUES ( |
||
116 | :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage, |
||
117 | :emailsender, :notificationtarget |
||
118 | ); |
||
119 | SQL |
||
120 | ); |
||
121 | |||
122 | $statement->bindValue(":shortname", $this->shortname); |
||
123 | $statement->bindValue(":longname", $this->longname); |
||
124 | $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath); |
||
125 | $statement->bindValue(":wikiapipath", $this->wikiapipath); |
||
126 | $statement->bindValue(":enabled", $this->enabled); |
||
127 | $statement->bindValue(":defaultclose", $this->defaultclose); |
||
128 | $statement->bindValue(":defaultlanguage", $this->defaultlanguage); |
||
129 | $statement->bindValue(":emailsender", $this->emailsender); |
||
130 | $statement->bindValue(":notificationtarget", $this->notificationtarget); |
||
131 | |||
132 | if ($statement->execute()) { |
||
133 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
134 | } |
||
135 | else { |
||
136 | throw new Exception($statement->errorInfo()); |
||
137 | } |
||
138 | } |
||
139 | else { |
||
140 | $statement = $this->dbObject->prepare(<<<SQL |
||
141 | UPDATE domain SET |
||
142 | longname = :longname, |
||
143 | wikiarticlepath = :wikiarticlepath, |
||
144 | wikiapipath = :wikiapipath, |
||
145 | enabled = :enabled, |
||
146 | defaultclose = :defaultclose, |
||
147 | defaultlanguage = :defaultlanguage, |
||
148 | emailsender = :emailsender, |
||
149 | notificationtarget = :notificationtarget, |
||
150 | |||
151 | updateversion = updateversion + 1 |
||
152 | WHERE id = :id AND updateversion = :updateversion; |
||
153 | SQL |
||
154 | ); |
||
155 | |||
156 | $statement->bindValue(":longname", $this->longname); |
||
157 | $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath); |
||
158 | $statement->bindValue(":wikiapipath", $this->wikiapipath); |
||
159 | $statement->bindValue(":enabled", $this->enabled); |
||
160 | $statement->bindValue(":defaultclose", $this->defaultclose); |
||
161 | $statement->bindValue(":defaultlanguage", $this->defaultlanguage); |
||
162 | $statement->bindValue(":emailsender", $this->emailsender); |
||
163 | $statement->bindValue(":notificationtarget", $this->notificationtarget); |
||
164 | |||
165 | $statement->bindValue(':id', $this->id); |
||
166 | $statement->bindValue(':updateversion', $this->updateversion); |
||
167 | |||
168 | if (!$statement->execute()) { |
||
169 | throw new Exception($statement->errorInfo()); |
||
170 | } |
||
171 | |||
172 | if ($statement->rowCount() !== 1) { |
||
173 | throw new OptimisticLockFailedException(); |
||
174 | } |
||
175 | |||
176 | $this->updateversion++; |
||
177 | } |
||
325 | } |