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 |
||
33 | class PMF_Helper_Faq extends PMF_Helper |
||
34 | { |
||
35 | /** |
||
36 | * SSL enabled. |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $ssl = false; |
||
41 | |||
42 | /** |
||
43 | * Constructor. |
||
44 | * |
||
45 | * @param PMF_Configuration $config |
||
46 | * |
||
47 | * @return PMF_Helper_Faq |
||
48 | */ |
||
49 | public function __construct(PMF_Configuration $config) |
||
53 | |||
54 | /** |
||
55 | * Sets SSL mode. |
||
56 | * |
||
57 | * @param bool $ssl |
||
58 | */ |
||
59 | public function setSsl($ssl) |
||
63 | |||
64 | /** |
||
65 | * Returns current SSL mode. |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function getSsl() |
||
73 | |||
74 | /** |
||
75 | * Renders a Facebook Like button. |
||
76 | * |
||
77 | * @param string $url |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function renderFacebookLikeButton($url) |
||
99 | |||
100 | /** |
||
101 | * Renders a Share on Facebook link. |
||
102 | * |
||
103 | * @param string $url |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | View Code Duplication | public function renderFacebookShareLink($url) |
|
124 | |||
125 | /** |
||
126 | * Renders a Share on Twitter link. |
||
127 | * |
||
128 | * @param string $url |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | View Code Duplication | public function renderTwitterShareLink($url) |
|
149 | |||
150 | /** |
||
151 | * Renders a select box with all translations of a FAQ. |
||
152 | * |
||
153 | * @param PMF_Faq $faq |
||
154 | * @param int $categoryId |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function renderChangeLanguageSelector(PMF_Faq $faq, $categoryId) |
||
188 | |||
189 | /** |
||
190 | * Renders a preview of the answer. |
||
191 | * |
||
192 | * @param string $answer |
||
193 | * @param integer $numWords |
||
194 | * @return string |
||
195 | */ |
||
196 | public function renderAnswerPreview($answer, $numWords) |
||
205 | |||
206 | /** |
||
207 | * Creates an overview with all categories with their FAQs. |
||
208 | * |
||
209 | * @param PMF_Category $category |
||
210 | * @param PMF_Faq $faq |
||
211 | * @param string $language |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | public function createOverview(PMF_Category $category, PMF_Faq $faq, $language = '') |
||
249 | } |
||
250 |
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.