Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 20 | class CategoryService |
||
| 21 | { |
||
| 22 | private $categoryMapper; |
||
| 23 | private $categoryFilter; |
||
| 24 | private $upload; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * CategoryService constructor. |
||
| 28 | * |
||
| 29 | * @param CategoryMapper $categoryMapper |
||
| 30 | * @param CategoryFilter $categoryFilter |
||
| 31 | * @param Upload $upload |
||
| 32 | */ |
||
| 33 | public function __construct(CategoryMapper $categoryMapper, CategoryFilter $categoryFilter, Upload $upload) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Return pagination object to paginate results on view. |
||
| 42 | * |
||
| 43 | * @param int $page Current page set to pagination to display |
||
| 44 | * @param int $limit Limit set to pagination |
||
| 45 | * |
||
| 46 | * @return Paginator |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function getPagination($page, $limit) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Return one category for given UUID. |
||
| 62 | * |
||
| 63 | * @param string $categoryId UUID from DB |
||
| 64 | * |
||
| 65 | * @return array|\ArrayObject|null |
||
| 66 | */ |
||
| 67 | public function getCategory($categoryId) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return one category for given URL Slug. |
||
| 74 | * |
||
| 75 | * @param string $urlSlug |
||
| 76 | * |
||
| 77 | * @return array|\ArrayObject|null |
||
| 78 | */ |
||
| 79 | public function getCategoryBySlug($urlSlug) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Create new category. |
||
| 86 | * |
||
| 87 | * @param $data |
||
| 88 | * |
||
| 89 | * @throws FilterException |
||
| 90 | */ |
||
| 91 | public function createCategory($data) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Update existing category. |
||
| 109 | * |
||
| 110 | * @param $data |
||
| 111 | * @param $categoryId |
||
| 112 | * |
||
| 113 | * @throws FilterException |
||
| 114 | * @throws \Exception |
||
| 115 | */ |
||
| 116 | public function updateCategory($data, $categoryId) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Delete category by given UUID. |
||
| 144 | * |
||
| 145 | * @param string $categoryId UUID from DB |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public function delete($categoryId) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returnall categoriess typically to populate select box. |
||
| 160 | * |
||
| 161 | * @return \Zend\Db\ResultSet\ResultSet |
||
| 162 | */ |
||
| 163 | public function getAll($type = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Return categories with posts(articles). |
||
| 174 | * |
||
| 175 | * @param null $inHomepage |
||
| 176 | * @param null $inCategoryList |
||
| 177 | * |
||
| 178 | * @return mixed |
||
| 179 | */ |
||
| 180 | public function getCategoriesWithPosts($inHomepage = null, $inCategoryList = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Return categories posts/articles. |
||
| 195 | * |
||
| 196 | * @param null $inCategoryList |
||
| 197 | * |
||
| 198 | * @return null|\Zend\Db\ResultSet\ResultSetInterface |
||
| 199 | */ |
||
| 200 | public function getCategories($inCategoryList = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get posts - articles with type == Posts. |
||
| 207 | * |
||
| 208 | * @param $category |
||
| 209 | * @param int $page |
||
| 210 | * |
||
| 211 | * @return Paginator |
||
| 212 | */ |
||
| 213 | public function paginateCategoryPosts($category, $page = 1): Paginator |
||
| 225 | } |
||
| 226 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.