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 |
||
24 | class syntax_plugin_backlinks_test extends DokuWikiTest { |
||
25 | |||
26 | protected $pluginsEnabled = array('backlinks'); |
||
27 | |||
28 | /** |
||
29 | * copy data and add pages to the index. |
||
30 | */ |
||
31 | View Code Duplication | public static function setUpBeforeClass(){ |
|
1 ignored issue
–
show
|
|||
32 | parent::setUpBeforeClass(); |
||
33 | global $conf; |
||
34 | $conf['allowdebug'] = 1; |
||
35 | |||
36 | TestUtils::rcopy(TMP_DIR, dirname(__FILE__) . '/data/'); |
||
37 | |||
38 | dbglog("\nset up class syntax_plugin_backlinks_test"); |
||
39 | } |
||
40 | |||
41 | View Code Duplication | function setUp() { |
|
1 ignored issue
–
show
|
|||
42 | parent::setUp(); |
||
43 | |||
44 | global $conf; |
||
45 | $conf['allowdebug'] = 1; |
||
46 | $conf['cachetime'] = -1; |
||
47 | |||
48 | $data = array(); |
||
49 | search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); |
||
50 | |||
51 | //dbglog($data, "pages for indexing"); |
||
52 | |||
53 | $verbose = false; |
||
54 | $force = false; |
||
55 | foreach($data as $val) { |
||
56 | idx_addPage($val['id'], $verbose, $force); |
||
57 | } |
||
58 | //idx_addPage('bob_ross_says', $verbose, $force); |
||
59 | //idx_addPage('link', $verbose, $force); |
||
60 | //idx_addPage('backlinks_syntax', $verbose, $force); |
||
61 | if($conf['allowdebug']) { |
||
62 | touch(DOKU_TMP_DATA.'cache/debug.log'); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | View Code Duplication | public function tearDown() { |
|
1 ignored issue
–
show
|
|||
67 | parent::tearDown(); |
||
68 | |||
69 | global $conf; |
||
70 | // try to get the debug log after running the test, print and clear |
||
71 | if($conf['allowdebug']) { |
||
72 | print "\n"; |
||
73 | readfile(DOKU_TMP_DATA.'cache/debug.log'); |
||
74 | unlink(DOKU_TMP_DATA.'cache/debug.log'); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | public function testIndex() { |
||
79 | $query = array('ross'); |
||
80 | $this->assertEquals( |
||
81 | array('ross' => array( |
||
82 | 'link' => '3', |
||
83 | 'bob_ross_says' => '1', |
||
84 | 'backlinks_syntax' => '2', |
||
85 | 'backlinks_include_syntax' => '2', |
||
86 | 'backlinks_exclude_syntax' => '2', |
||
87 | 'backlink_test_pages' => '8', |
||
88 | 'include:link' => '3', |
||
89 | 'exclude:link' => '3' |
||
90 | )), |
||
91 | idx_lookup($query) |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | View Code Duplication | public function testLinksPage() { |
|
104 | |||
105 | View Code Duplication | public function testStoryPage() { |
|
1 ignored issue
–
show
|
|||
106 | $request = new TestRequest(); |
||
107 | $response = $request->get(array('id'=>'bob_ross_says'), '/doku.php'); |
||
108 | |||
114 | |||
115 | View Code Duplication | public function testBacklinks() { |
|
148 | } |
||
149 |
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.