| Conditions | 9 |
| Paths | 192 |
| Total Lines | 65 |
| Code Lines | 39 |
| 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 |
||
| 165 | public function modifyPost() |
||
| 166 | { |
||
| 167 | //Security checks |
||
| 168 | $this->onlyAdmin(); |
||
| 169 | if (!$this->request->isPost()) { |
||
| 170 | $this->alertBox->setAlert('Only post messages allowed', 'error'); |
||
| 171 | $this->response->redirect('admin'); |
||
| 172 | } |
||
| 173 | |||
| 174 | $posts = $this->container->getRequest()->getDataFull(); |
||
| 175 | |||
| 176 | $postId = $posts["postId"]; |
||
| 177 | $title = trim($posts["postTitle"]); |
||
| 178 | $postImage = $posts["postImage"]; |
||
| 179 | $postSlug = trim($posts["postSlug"]); |
||
| 180 | $article = $posts["postTextArea"]; |
||
| 181 | $idCategory = $posts["categorySelector"]; |
||
| 182 | $published = $posts["isPublished"]; |
||
| 183 | $onFrontpage = $posts["isOnFrontPage"]; |
||
| 184 | |||
| 185 | //security and error checks |
||
| 186 | $originalPostSlug = $this->slugModel->getSlugFromId($postId, "posts", "idposts", |
||
| 187 | "posts_slug"); |
||
| 188 | $error = false; |
||
| 189 | if ($title == "") { |
||
| 190 | $error = true; |
||
| 191 | $this->alertBox->setAlert("empty title not allowed", "error"); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($postSlug == "") { |
||
| 195 | $error = true; |
||
| 196 | $this->alertBox->setAlert("empty slug not allowed", "error"); |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($postSlug != $originalPostSlug) //if the slug has been updated |
||
| 200 | { |
||
| 201 | if (!$this->slugModel->isUnique($postSlug, "posts", "posts_slug")) { |
||
| 202 | $error = true; |
||
| 203 | $originalPostSlug = $this->slugModel->getSlugFromId($postId, "posts", "idposts", "posts_slug"); |
||
| 204 | $this->alertBox->setAlert("Slug not unique", "error"); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | if ($error) { |
||
| 208 | $this->container->getResponse()->redirect("admin/post/modify/$originalPostSlug"); |
||
| 209 | } |
||
| 210 | |||
| 211 | //Update the post |
||
| 212 | $postUpdate = $this->postModel->modifyPost($postId, $title, $postImage, $idCategory, $article, $published, |
||
| 213 | $onFrontpage, $postSlug); |
||
| 214 | |||
| 215 | // Tags |
||
| 216 | //remove all tags |
||
| 217 | $this->tagModel->removeTagsOnPost($postId); |
||
| 218 | //set new tags |
||
| 219 | if (isset($posts["tags"])) { |
||
| 220 | $this->addTags($posts["tags"], $postId); |
||
| 221 | } |
||
| 222 | |||
| 223 | //checking result and redirecting |
||
| 224 | if ($postUpdate) { |
||
| 225 | $this->alertBox->setAlert("Post " . $title . " Updated"); |
||
| 226 | $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); |
||
| 227 | } |
||
| 228 | $this->alertBox->setAlert("Error updating " . $title, "error"); |
||
| 229 | $this->container->getResponse()->redirect("admin/post/modify/" . $originalPostSlug); |
||
| 230 | } |
||
| 231 | } |