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 |
||
7 | |||
8 | use PHPUnit_Framework_TestCase; |
||
9 | |||
10 | /** |
||
11 | * Tests for the Page class. |
||
12 | */ |
||
13 | class PageTest extends PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * A page has a title and an HTML display title. |
||
18 | */ |
||
19 | public function testTitles() |
||
41 | |||
42 | /** |
||
43 | * A page either exists or doesn't. |
||
44 | */ |
||
45 | public function testExists() |
||
68 | |||
69 | /** |
||
70 | * A page has an integer ID on a given project. |
||
71 | */ |
||
72 | View Code Duplication | public function testId() |
|
83 | |||
84 | /** |
||
85 | * A page has a URL. |
||
86 | */ |
||
87 | View Code Duplication | public function testUrls() |
|
98 | |||
99 | /** |
||
100 | * A list of a single user's edits on this page can be retrieved, along with the count of |
||
101 | * these revisions, and the total bytes added and removed. |
||
102 | * @TODO this is not finished yet |
||
103 | */ |
||
104 | public function testUsersEdits() |
||
105 | { |
||
106 | $pageRepo = $this->getMock(PagesRepository::class, ['getRevisions']); |
||
107 | $pageRepo |
||
108 | ->method('getRevisions') |
||
109 | ->with() |
||
110 | ->willReturn([ |
||
111 | [ |
||
112 | 'id' => '1', |
||
113 | 'timestamp' => '20170505100000', |
||
114 | 'length_change' => '1', |
||
115 | 'comment' => 'One' |
||
116 | ], |
||
117 | ]); |
||
118 | |||
119 | $page = new Page(new Project('exampleWiki'), 'Page'); |
||
125 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.