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 |
||
9 | class CTextFilterTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * Supported filters. |
||
13 | */ |
||
14 | private $standardFilters = [ |
||
15 | 'yamlfrontmatter', |
||
16 | 'bbcode', |
||
17 | 'clickable', |
||
18 | 'markdown', |
||
19 | // 'geshi', |
||
20 | 'nl2br', |
||
21 | 'shortcode', |
||
22 | 'purify', |
||
23 | 'titlefromh1', |
||
24 | ]; |
||
25 | |||
26 | |||
27 | |||
28 | /** |
||
29 | * Test. |
||
30 | * |
||
31 | * @return void |
||
32 | */ |
||
33 | public function testMore() |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * Test. |
||
60 | * |
||
61 | * @return void |
||
62 | */ |
||
63 | public function testStop() |
||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * Test. |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | /* |
||
88 | public function testSyntaxHighlightGeshiMethod() |
||
89 | { |
||
90 | $filter = new CTextFilter(); |
||
91 | |||
92 | $text = ""; |
||
93 | $exp = '<pre class="text geshi"></pre>'; |
||
94 | $res = $filter->parse($text, ["geshi"]); |
||
95 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
96 | |||
97 | $text = <<<'EOD' |
||
98 | $php = "hi"; |
||
99 | EOD; |
||
100 | $exp = '<pre class="text geshi">$php = "hi";</pre>'; |
||
101 | $res = $filter->parse($text, ["geshi"]); |
||
102 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
103 | |||
104 | $text = <<<'EOD' |
||
105 | $php = "hi"; |
||
106 | EOD; |
||
107 | |||
108 | // @codingStandardsIgnoreStart |
||
109 | $exp = <<<'EOD' |
||
110 | <pre class="php geshi"><span class="re0">$php</span> <span class="sy0">=</span> <span class="st0">"hi"</span><span class="sy0">;</span></pre> |
||
111 | EOD; |
||
112 | // @codingStandardsIgnoreEnd |
||
113 | $res = $filter->syntaxHighlightGeSHi($text, "php"); |
||
114 | $this->assertEquals($exp, $res, "Geshi did not match"); |
||
115 | } |
||
116 | */ |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Test. |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | /* |
||
125 | public function testSyntaxHighlightGeshiShortCode() |
||
126 | { |
||
127 | $filter = new CTextFilter(); |
||
128 | |||
129 | $text = <<<'EOD' |
||
130 | ```text |
||
131 | ``` |
||
132 | |||
133 | EOD; |
||
134 | $exp = <<<'EOD' |
||
135 | <pre class="text geshi"></pre> |
||
136 | EOD; |
||
137 | $res = $filter->parse($text, ["shortcode"]); |
||
138 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
139 | |||
140 | $text = <<<'EOD' |
||
141 | ``` |
||
142 | ``` |
||
143 | |||
144 | EOD; |
||
145 | $exp = <<<'EOD' |
||
146 | <pre class="text geshi"></pre> |
||
147 | EOD; |
||
148 | $res = $filter->parse($text, ["shortcode"]); |
||
149 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
150 | |||
151 | $text = <<<'EOD' |
||
152 | ```text |
||
153 | $php = "hi"; |
||
154 | ``` |
||
155 | |||
156 | EOD; |
||
157 | $exp = <<<'EOD' |
||
158 | <pre class="text geshi">$php = "hi"; |
||
159 | </pre> |
||
160 | EOD; |
||
161 | $res = $filter->parse($text, ["shortcode"]); |
||
162 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
163 | |||
164 | $text = <<<'EOD' |
||
165 | ```php |
||
166 | $php = "hi"; |
||
167 | ``` |
||
168 | |||
169 | EOD; |
||
170 | // @codingStandardsIgnoreStart |
||
171 | $exp = <<<'EOD' |
||
172 | <pre class="php geshi"><span class="re0">$php</span> <span class="sy0">=</span> <span class="st0">"hi"</span><span class="sy0">;</span> |
||
173 | </pre> |
||
174 | EOD; |
||
175 | // @codingStandardsIgnoreEnd |
||
176 | $res = $filter->parse($text, ["shortcode"]); |
||
177 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
178 | } |
||
179 | */ |
||
180 | |||
181 | |||
182 | |||
183 | /** |
||
184 | * Test. |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | /* |
||
189 | public function testSyntaxHighlightHlJsiShortCode() |
||
190 | { |
||
191 | $filter = new CTextFilter(); |
||
192 | |||
193 | $text = <<<'EOD' |
||
194 | ```text |
||
195 | ``` |
||
196 | |||
197 | EOD; |
||
198 | $exp = <<<'EOD' |
||
199 | <pre></pre> |
||
200 | EOD; |
||
201 | $res = $filter->parse($text, ["shortcode"]); |
||
202 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
203 | |||
204 | $text = <<<'EOD' |
||
205 | ``` |
||
206 | ``` |
||
207 | |||
208 | EOD; |
||
209 | $exp = <<<'EOD' |
||
210 | <pre></pre> |
||
211 | EOD; |
||
212 | $res = $filter->parse($text, ["shortcode"]); |
||
213 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
214 | |||
215 | $text = <<<'EOD' |
||
216 | ```text |
||
217 | $php = "hi"; |
||
218 | ``` |
||
219 | |||
220 | EOD; |
||
221 | $exp = <<<'EOD' |
||
222 | <pre>$php = "hi"; |
||
223 | </pre> |
||
224 | EOD; |
||
225 | $res = $filter->parse($text, ["shortcode"]); |
||
226 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
227 | |||
228 | $text = <<<'EOD' |
||
229 | ```php |
||
230 | $php = "hi"; |
||
231 | ``` |
||
232 | |||
233 | EOD; |
||
234 | // @codingStandardsIgnoreStart |
||
235 | $exp = <<<'EOD' |
||
236 | <pre class="hljs">$php = <span class="hljs-string">"hi"</span>; |
||
237 | </pre> |
||
238 | EOD; |
||
239 | // @codingStandardsIgnoreEnd |
||
240 | $res = $filter->parse($text, ["shortcode"]); |
||
241 | $this->assertEquals($exp, $res->text, "Geshi did not match"); |
||
242 | } |
||
243 | */ |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Test. |
||
248 | * |
||
249 | * @return void |
||
250 | */ |
||
251 | public function testTitleFromFirstH1() |
||
329 | |||
330 | |||
331 | |||
332 | /** |
||
333 | * Test. |
||
334 | * |
||
335 | * @expectedException /Mos/TextFilter/Exception |
||
336 | * |
||
337 | * @return void |
||
338 | */ |
||
339 | public function testJsonFrontMatterException() |
||
350 | |||
351 | |||
352 | |||
353 | /** |
||
354 | * Test. |
||
355 | * |
||
356 | * @return void |
||
357 | */ |
||
358 | public function testJsonFrontMatter() |
||
395 | |||
396 | |||
397 | |||
398 | /** |
||
399 | * Test. |
||
400 | * |
||
401 | * @expectedException /Mos/TextFilter/Exception |
||
402 | * |
||
403 | * @return void |
||
404 | */ |
||
405 | public function testYamlFrontMatterException() |
||
420 | |||
421 | |||
422 | |||
423 | /** |
||
424 | * Test. |
||
425 | * |
||
426 | * @return void |
||
427 | */ |
||
428 | public function testYamlFrontMatter() |
||
508 | |||
509 | |||
510 | |||
511 | /** |
||
512 | * Test. |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | public function testGetFilters() |
||
524 | |||
525 | |||
526 | |||
527 | |||
528 | /** |
||
529 | * Test. |
||
530 | * |
||
531 | * @return void |
||
532 | */ |
||
533 | public function testHasFilter() |
||
540 | |||
541 | |||
542 | |||
543 | |||
544 | /** |
||
545 | * Test. |
||
546 | * |
||
547 | * @expectedException /Mos/TextFilter/Exception |
||
548 | * |
||
549 | * @return void |
||
550 | */ |
||
551 | public function testHasFilterException() |
||
557 | |||
558 | |||
559 | |||
560 | |||
561 | /** |
||
562 | * Test. |
||
563 | * |
||
564 | * @return void |
||
565 | */ |
||
566 | public function testPurifier() |
||
575 | |||
576 | |||
577 | |||
578 | /** |
||
579 | * Test. |
||
580 | * |
||
581 | * @return void |
||
582 | */ |
||
583 | View Code Duplication | public function testMarkdown() |
|
592 | |||
593 | |||
594 | |||
595 | /** |
||
596 | * Test. |
||
597 | * |
||
598 | * @return void |
||
599 | */ |
||
600 | View Code Duplication | public function testSmartyPants() |
|
609 | |||
610 | |||
611 | |||
612 | /** |
||
613 | * Test. |
||
614 | * |
||
615 | * @return void |
||
616 | */ |
||
617 | View Code Duplication | public function testMarkdownAndBBCode() |
|
626 | |||
627 | |||
628 | |||
629 | /** |
||
630 | * Test. |
||
631 | * |
||
632 | * @return void |
||
633 | */ |
||
634 | View Code Duplication | public function testMarkdownAndBBCodeAsArray() |
|
643 | |||
644 | |||
645 | |||
646 | /** |
||
647 | * Test. |
||
648 | * |
||
649 | * @return void |
||
650 | */ |
||
651 | View Code Duplication | public function testMarkdownArray() |
|
660 | |||
661 | |||
662 | |||
663 | /** |
||
664 | * Test. |
||
665 | * |
||
666 | * @return void |
||
667 | */ |
||
668 | View Code Duplication | public function testUppercase() |
|
677 | |||
678 | |||
679 | |||
680 | /** |
||
681 | * Test. |
||
682 | * |
||
683 | * @return void |
||
684 | */ |
||
685 | View Code Duplication | public function testBBCode() |
|
694 | |||
695 | |||
696 | |||
697 | /** |
||
698 | * Test. |
||
699 | * |
||
700 | * @return void |
||
701 | */ |
||
702 | View Code Duplication | public function testClickable() |
|
711 | |||
712 | |||
713 | |||
714 | /** |
||
715 | * Test. |
||
716 | * |
||
717 | * @return void |
||
718 | */ |
||
719 | View Code Duplication | public function testNl2Br() |
|
728 | |||
729 | |||
730 | |||
731 | /** |
||
732 | * Test. |
||
733 | * |
||
734 | * @return void |
||
735 | */ |
||
736 | public function testShortCodeFigure() |
||
756 | |||
757 | |||
758 | |||
759 | /** |
||
760 | * Test. |
||
761 | * |
||
762 | * @expectedException Exception |
||
763 | * |
||
764 | * @return void |
||
765 | */ |
||
766 | public function testDoItException() |
||
771 | } |
||
772 |
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.