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 MarkdownRendererTest extends PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | public function testIntegrity() |
||
11 | { |
||
12 | $renderer = Factory::createRenderer('markdown'); |
||
13 | $output = $renderer->render('# Hello World!'); |
||
14 | |||
15 | $this->assertEquals("<h1>Hello World!</h1>\n", $output); |
||
16 | } |
||
17 | |||
18 | public function basicParserProvider() |
||
19 | { |
||
20 | $sets = array(); |
||
21 | |||
22 | foreach (glob(__DIR__ . '/examples/markdown/*.text') as $file) { |
||
23 | $sets[] = array( |
||
24 | realpath($file), |
||
25 | realpath(substr($file, 0, -5) . '.xhtml') |
||
26 | ); |
||
27 | } |
||
28 | |||
29 | return $sets; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @dataProvider basicParserProvider |
||
34 | */ |
||
35 | public function testBasicParser($fileInput, $fileExpected) |
||
36 | { |
||
37 | $renderer = Factory::createRenderer('markdown'); |
||
38 | |||
39 | $output = $renderer->render(file_get_contents($fileInput)); |
||
40 | |||
41 | $expected = file_get_contents($fileExpected); |
||
42 | |||
43 | $output = str_replace(array("\r\n", "\r"), "\n", $output); |
||
44 | $expected = str_replace(array("\r\n", "\r"), "\n", $expected); |
||
45 | |||
46 | $this->assertEquals($expected, $output); |
||
47 | } |
||
48 | } |
||
49 |