| Conditions | 9 |
| Paths | 66 |
| Total Lines | 52 |
| Code Lines | 29 |
| 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 |
||
| 64 | public function update() |
||
| 65 | { |
||
| 66 | $this->onlyAdmin(); |
||
| 67 | if (!$this->request->isPost()) { |
||
| 68 | $this->alertBox->setAlert('Only post messages allowed', 'error'); |
||
| 69 | $this->response->redirect('admin'); |
||
| 70 | } |
||
| 71 | |||
| 72 | $category = $this->container->getRequest()->getDataFull(); |
||
| 73 | |||
| 74 | $categoryId = $category["idcategories"]; |
||
| 75 | $categoryName = $category["category_name"]; |
||
| 76 | $categorySlug = $category["categories_slug"]; |
||
| 77 | |||
| 78 | //Sanity check on ID |
||
| 79 | if($categoryId == null ) |
||
| 80 | { |
||
| 81 | throw new \ErrorException("invalid category ID"); |
||
| 82 | } |
||
| 83 | |||
| 84 | $originalCategorySlug = $this->slugModel->getSlugFromId($categoryId, "categories", "idcategories","categories_slug"); |
||
| 85 | |||
| 86 | //Error checking |
||
| 87 | $error = false; |
||
| 88 | if($categoryName == "") |
||
| 89 | { |
||
| 90 | $error = true; |
||
| 91 | $this->alertBox->setAlert("empty name not allowed", "error"); |
||
| 92 | } |
||
| 93 | if($categorySlug == "") |
||
| 94 | { |
||
| 95 | $error = true; |
||
| 96 | $this->alertBox->setAlert("empty slug not allowed", "error"); |
||
| 97 | } |
||
| 98 | if (!$this->slugModel->isUnique($categorySlug, "categories", "categories_slug") && $categorySlug !== $originalCategorySlug) { |
||
| 99 | $error = true; |
||
| 100 | $this->alertBox->setAlert("Slug not unique", "error"); |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($error) { |
||
| 104 | $this->container->getResponse()->redirect("/admin/category/list"); |
||
| 105 | } |
||
| 106 | |||
| 107 | $categoryUpdate = $this->categoryModel->update($categoryId, $categoryName, $categorySlug); |
||
| 108 | |||
| 109 | //checking result and redirecting |
||
| 110 | if ($categoryUpdate) { |
||
| 111 | $this->alertBox->setAlert("Category " . $categoryName . " updated"); |
||
| 112 | $this->container->getResponse()->redirect("/admin/category/list/"); |
||
| 113 | } |
||
| 114 | $this->alertBox->setAlert("Error updating " . $categoryName, "error"); |
||
| 115 | $this->container->getResponse()->redirect("/admin/category/list/"); |
||
| 116 | } |
||
| 185 | } |