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 |
||
12 | Class Content extends \Anax\MVC\CDatabaseModel |
||
13 | { |
||
14 | /** |
||
15 | * Get "all" content from database (limit by per page) |
||
16 | * |
||
17 | * @param int $page Wich page paging are at |
||
18 | * @param int $perPage Contents per page |
||
19 | * @param boolean $publish If it needs to be publish |
||
20 | * @return object With "all" database content data |
||
21 | */ |
||
22 | 1 | View Code Duplication | public function getAllContent($page = null, $perPage = null, $publish = true){ |
52 | |||
53 | /** |
||
54 | * Get "all" content of one type from database (limit by per page) |
||
55 | * |
||
56 | * @param boolean $publish If it needs to be publish |
||
57 | * @param string $type Wich type of content ex. blog, page etc. |
||
58 | * @param int $page Wich page paging are at |
||
59 | * @param int $perPage Contents per page |
||
60 | * @return object With "all" database content data |
||
61 | */ |
||
62 | 2 | View Code Duplication | public function getAllContentOfType($type, $page = null, $perPage = null, $publish = true){ |
93 | |||
94 | /** |
||
95 | * Get "all" content of one tag and one type from database |
||
96 | * |
||
97 | * @param string $tag Wich tag content should have ex. page-news etc. |
||
98 | * @param string $type Wich type of content ex. blog, page etc. |
||
99 | * @param boolean $publish If it needs to be publish |
||
100 | * @param int $page Wich page paging are at |
||
101 | * @param int $perPage Content per page |
||
102 | * @return object With content data from database |
||
103 | */ |
||
104 | 1 | public function getAllContentOfTag($tag, $type, $page = 0, $perPage = null, $publish = true){ |
|
137 | |||
138 | /** |
||
139 | * Get content from database by url |
||
140 | * |
||
141 | * @param string $url Begin or whole url |
||
142 | * @param boolean $publish If it needs to be publish |
||
143 | * @param string $type Wich type of content ex. blog, page etc. |
||
144 | * @return object With content data from database |
||
145 | */ |
||
146 | 1 | View Code Duplication | public function getContentByUrl($url, $type, $publish = true){ |
169 | |||
170 | /** |
||
171 | * Get content from database by slug |
||
172 | * |
||
173 | * @param string $slug Unique query to content |
||
174 | * @param boolean $publish if it needs to be publish |
||
175 | * @param string $type Wich type of content ex. blog, page etc. |
||
176 | * @return object With content data from database |
||
177 | */ |
||
178 | 1 | View Code Duplication | public function getContentBySlug($slug, $type, $publish = true){ |
200 | |||
201 | /** |
||
202 | * Get content from database by index |
||
203 | * |
||
204 | * @param int $id Index to content |
||
205 | * @param boolean $publish If it needs to be publish |
||
206 | * @return object With content data. |
||
207 | */ |
||
208 | 2 | public function getContentById($id, $publish = true){ |
|
228 | |||
229 | /***** |
||
230 | *** |
||
231 | ** GET - Counting prepare for Paging (exemple on pagecontrollers blog.php, page.php, report.php and list_content.php) |
||
232 | *** |
||
233 | *****/ |
||
234 | |||
235 | /** |
||
236 | * Count all content from database |
||
237 | * |
||
238 | * @param boolean $publish If it needs to be publish |
||
239 | * @return int With count of content in database |
||
240 | */ |
||
241 | 1 | View Code Duplication | public function countAllContent($publish = true){ |
260 | |||
261 | /** |
||
262 | * Count all content of one type from database |
||
263 | * |
||
264 | * @param boolean $publish If it needs to be publish |
||
265 | * @param string $type Wich type of content ex. blog, page etc. |
||
266 | * @return int With count of content in database |
||
267 | */ |
||
268 | 1 | View Code Duplication | public function countAllContentOfType($type, $publish = true){ |
288 | |||
289 | /** |
||
290 | * Count all content of one tag and one type from database |
||
291 | * |
||
292 | * @param string $tag Wich tag of content ex. page-news etc. |
||
293 | * @param string $type Wich type of content ex. blog, page etc. |
||
294 | * @param boolean $publish Check if it needs to be publish |
||
295 | * @return int With count |
||
296 | */ |
||
297 | 1 | View Code Duplication | public function countAllContentOfTag($tag, $type, $publish = true){ |
320 | |||
321 | /***** |
||
322 | *** |
||
323 | ** GET - Help functions (exemple on pagecontrollers blog.php, page.php, report.php and list_content.php) |
||
324 | *** |
||
325 | *****/ |
||
326 | |||
327 | /** |
||
328 | * Get tags for a content |
||
329 | * |
||
330 | * @param int $id Index to content |
||
331 | * @return object $res Tags result |
||
332 | */ |
||
333 | 1 | public function getTagsForContent($id){ |
|
342 | |||
343 | /** |
||
344 | * Get a tag's title by slug |
||
345 | * |
||
346 | * @param string $slug URL for tag |
||
347 | * @return string Tag-title from database |
||
348 | */ |
||
349 | 1 | public function getTagBySlug($slug){ |
|
363 | |||
364 | /** |
||
365 | * Check if slug is available, or if not get one that is |
||
366 | * |
||
367 | * @param string $slug Slug to validate |
||
368 | * @param string $type Type of content |
||
369 | * @return string $newSlug A available slug |
||
370 | */ |
||
371 | 1 | public function makeSlugToContent($slug, $type){ |
|
394 | |||
395 | /** |
||
396 | * Get sql-query offset by per page and page |
||
397 | * |
||
398 | * @param int $perPage Per page |
||
399 | * @param int $page Page on paging |
||
400 | * @return int $offset Offset for sql-query |
||
401 | */ |
||
402 | public function getOffset($perPage, $page){ |
||
411 | } |