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 |
||
18 | class NewsCategory extends AliasModel |
||
19 | { |
||
20 | /** @var bool Whether or not the category is protected from being deleted from the UI */ |
||
21 | protected $is_protected; |
||
22 | |||
23 | /** @var bool When set to true, no new articles can be assigned this category */ |
||
24 | protected $is_read_only; |
||
25 | |||
26 | const DEFAULT_STATUS = 'enabled'; |
||
27 | |||
28 | const DELETED_COLUMN = 'is_deleted'; |
||
29 | const TABLE = "news_categories"; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | protected function assignResult($category) |
||
41 | 11 | ||
42 | 11 | /** |
|
43 | 11 | * Delete a category. Only delete a category if it is not protected |
|
44 | 11 | * |
|
45 | 11 | * @throws DeletionDeniedException |
|
46 | * @throws Exception |
||
47 | */ |
||
48 | public function delete() |
||
66 | 3 | ||
67 | /** |
||
68 | 3 | * Get all the news entries in the category that aren't disabled or deleted |
|
69 | 3 | * |
|
70 | 3 | * @param int $start The offset used when fetching matches, i.e. the starting point |
|
71 | * @param int $limit The amount of matches to be retrieved |
||
72 | 3 | * @param bool $getDrafts Whether or not to fetch drafts |
|
73 | * |
||
74 | * @throws \Pixie\Exception |
||
75 | * @throws Exception |
||
76 | * |
||
77 | * @return News[] An array of news objects |
||
78 | */ |
||
79 | 1 | View Code Duplication | public function getNews($start = 0, $limit = 5, $getDrafts = false) |
94 | 7 | ||
95 | /** |
||
96 | * Check if the category is protected from being deleted. |
||
97 | * |
||
98 | * @return bool Whether or not the category is protected |
||
99 | */ |
||
100 | public function isProtected() |
||
104 | |||
105 | /** |
||
106 | 1 | * Check if new News article can be assigned this category. |
|
107 | * |
||
108 | 1 | * @return bool |
|
109 | */ |
||
110 | 1 | public function isReadOnly() |
|
114 | 1 | ||
115 | /** |
||
116 | 1 | * Create a new category |
|
117 | 1 | * |
|
118 | * @param string $name The name of the category |
||
119 | 1 | * |
|
120 | * @return NewsCategory An object representing the category that was just created |
||
121 | */ |
||
122 | public static function addCategory($name) |
||
129 | 4 | ||
130 | /** |
||
131 | * Get all of the categories for the news |
||
132 | * |
||
133 | * @throws Exception |
||
134 | * |
||
135 | * @return NewsCategory[] An array of categories |
||
136 | */ |
||
137 | public static function getCategories() |
||
145 | 11 | ||
146 | /** |
||
147 | * Get a query builder for news categories. |
||
148 | * |
||
149 | * @throws Exception |
||
150 | * |
||
151 | * @return QueryBuilderFlex |
||
152 | */ |
||
153 | public static function getQueryBuilder() |
||
159 | 4 | ||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public static function getParamName() |
||
167 | 1 | ||
168 | /** |
||
169 | 1 | * {@inheritdoc} |
|
170 | */ |
||
171 | public static function getTypeForHumans() |
||
175 | } |
||
176 |
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.