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 | class DateTest extends \PHPUnit_Framework_TestCase |
||
8 | { |
||
9 | public function testSetExcelCalendar() |
||
21 | |||
22 | public function testSetExcelCalendarWithInvalidValue() |
||
28 | |||
29 | /** |
||
30 | * @dataProvider providerDateTimeExcelToTimestamp1900 |
||
31 | */ |
||
32 | public function testDateTimeExcelToTimestamp1900() |
||
44 | |||
45 | public function providerDateTimeExcelToTimestamp1900() |
||
49 | |||
50 | /** |
||
51 | * @dataProvider providerDateTimePHPToExcel1900 |
||
52 | */ |
||
53 | View Code Duplication | public function testDateTimePHPToExcel1900() |
|
65 | |||
66 | public function providerDateTimePHPToExcel1900() |
||
70 | |||
71 | /** |
||
72 | * @dataProvider providerDateTimeFormattedPHPToExcel1900 |
||
73 | */ |
||
74 | View Code Duplication | public function testDateTimeFormattedPHPToExcel1900() |
|
86 | |||
87 | public function providerDateTimeFormattedPHPToExcel1900() |
||
91 | |||
92 | /** |
||
93 | * @dataProvider providerDateTimeExcelToTimestamp1904 |
||
94 | */ |
||
95 | View Code Duplication | public function testDateTimeExcelToTimestamp1904() |
|
107 | |||
108 | public function providerDateTimeExcelToTimestamp1904() |
||
112 | |||
113 | /** |
||
114 | * @dataProvider providerDateTimePHPToExcel1904 |
||
115 | */ |
||
116 | View Code Duplication | public function testDateTimePHPToExcel1904() |
|
128 | |||
129 | public function providerDateTimePHPToExcel1904() |
||
133 | |||
134 | /** |
||
135 | * @dataProvider providerIsDateTimeFormatCode |
||
136 | */ |
||
137 | public function testIsDateTimeFormatCode() |
||
144 | |||
145 | public function providerIsDateTimeFormatCode() |
||
149 | |||
150 | /** |
||
151 | * @dataProvider providerDateTimeExcelToTimestamp1900Timezone |
||
152 | */ |
||
153 | View Code Duplication | public function testDateTimeExcelToTimestamp1900Timezone() |
|
165 | |||
166 | public function providerDateTimeExcelToTimestamp1900Timezone() |
||
170 | } |
||
171 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.