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 |
||
19 | class Renderer |
||
20 | { |
||
21 | /** |
||
22 | * Set the paginator to be rendered. |
||
23 | * |
||
24 | * @var Paginator |
||
25 | */ |
||
26 | protected $xPaginator = null; |
||
27 | |||
28 | /** |
||
29 | * The template manager. |
||
30 | * |
||
31 | * Will be used to render HTML code for links. |
||
32 | * |
||
33 | * @var Template |
||
34 | */ |
||
35 | protected $xTemplate = null; |
||
36 | |||
37 | /** |
||
38 | * The class contructor |
||
39 | * |
||
40 | * @param Template $xTemplate |
||
41 | */ |
||
42 | public function __construct(Template $xTemplate) |
||
43 | { |
||
44 | $this->xTemplate = $xTemplate; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Set the paginator to be rendered. |
||
49 | * |
||
50 | * @param Paginator $xPaginator The paginator to be rendered |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function setPaginator(\Jaxon\Utils\Pagination\Paginator $xPaginator) |
||
58 | |||
59 | /** |
||
60 | * Render the previous link. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | View Code Duplication | protected function getPrevLink() |
|
73 | |||
74 | /** |
||
75 | * Render the next link. |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | View Code Duplication | protected function getNextLink() |
|
88 | |||
89 | /** |
||
90 | * Render the pagination links. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | protected function getLinks() |
||
111 | |||
112 | /** |
||
113 | * Render an HTML pagination control. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function render() |
||
125 | } |
||
126 |
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.