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 |
||
8 | class CPageContent |
||
9 | { |
||
10 | use \Anax\TConfigure, |
||
11 | \Anax\DI\TInjectionAware; |
||
12 | |||
13 | |||
14 | |||
15 | /** |
||
16 | * Properties. |
||
17 | */ |
||
18 | private $toc = null; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * Map url to page if such mapping can be done. |
||
24 | * |
||
25 | * @throws NotFoundException when mapping can not be done. |
||
26 | */ |
||
27 | public function getContentForRoute() |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * Map the route to the correct entry in the toc. |
||
53 | * |
||
54 | * @param string $route current route used to access page. |
||
55 | * @param array $toc the toc as array. |
||
56 | * |
||
57 | * @return string as the title for the content. |
||
58 | */ |
||
59 | public function mapRoute2Toc($route, $toc) |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Extract title from content. |
||
74 | * |
||
75 | * @param string $file filenam to load load content from. |
||
76 | * |
||
77 | * @return string as the title for the content. |
||
78 | */ |
||
79 | public function getTitleFromFirstLine($file) |
||
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * Get table of content for all pages. |
||
91 | * |
||
92 | * @param string $id to use to generate key for toc. |
||
93 | * |
||
94 | * @return array as table of content. |
||
95 | */ |
||
96 | public function getTableOfContent($id) |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Generate ToC from directory structure, containing url, title and filename |
||
117 | * of each page. |
||
118 | * |
||
119 | * @return array as table of content. |
||
120 | */ |
||
121 | public function createTableOfContent() |
||
165 | } |
||
166 |
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.