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 |
||
19 | final class VersionTest extends PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | //////////////////////////////////////////////////////////////////////////// |
||
22 | // __construct() |
||
23 | //////////////////////////////////////////////////////////////////////////// |
||
24 | |||
25 | /** |
||
26 | * @dataProvider provideValidVersion |
||
27 | * |
||
28 | * @param string $unused |
||
29 | * @param int $major |
||
30 | * @param int $minor |
||
31 | * @param int $patch |
||
32 | * @param string $preRelease |
||
33 | * @param string $build |
||
34 | */ |
||
35 | public function testConstructor( |
||
70 | |||
71 | /** |
||
72 | * @dataProvider provideWrongPreReleaseVersion |
||
73 | * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException |
||
74 | * @expectedExceptionMessage The pre-release version is not compatible with rule 9 of the specifications. |
||
75 | * |
||
76 | * @param string $preRelease |
||
77 | */ |
||
78 | public function testConstructorFailsIfPreReleaseNotValid(string $preRelease) |
||
82 | |||
83 | /** |
||
84 | * @dataProvider provideWrongPreReleaseVersion |
||
85 | * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException |
||
86 | * @expectedExceptionMessage The build version is not compatible with rule 10 of the specifications. |
||
87 | * |
||
88 | * @param string $build |
||
89 | */ |
||
90 | public function testConstructorFailsIfBuildNotValid(string $build) |
||
94 | |||
95 | /** @return array */ |
||
96 | public function provideWrongPreReleaseVersion() : array |
||
110 | |||
111 | //////////////////////////////////////////////////////////////////////////// |
||
112 | // fromString() |
||
113 | //////////////////////////////////////////////////////////////////////////// |
||
114 | |||
115 | /** |
||
116 | * @dataProvider provideValidVersion |
||
117 | * |
||
118 | * @param string $string |
||
119 | * @param int $expectedMajor |
||
120 | * @param int $expectedMinor |
||
121 | * @param int $expectedPatch |
||
122 | * @param string $expectedPreRelease |
||
123 | * @param string $exceptedBuild |
||
124 | */ |
||
125 | public function testFromString( |
||
160 | |||
161 | /** @return array */ |
||
162 | public function provideValidVersion() : array |
||
175 | |||
176 | /** |
||
177 | * @dataProvider provideWrongStringVersion |
||
178 | * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException |
||
179 | * @expectedExceptionMessageRegExp /The string ".+" does not look like a version\./ |
||
180 | * |
||
181 | * @param string $string |
||
182 | */ |
||
183 | public function testFromStringFailsWithInvalidString(string $string) |
||
187 | |||
188 | /** |
||
189 | * @return array |
||
190 | */ |
||
191 | public function provideWrongStringVersion() : array |
||
201 | |||
202 | //////////////////////////////////////////////////////////////////////////// |
||
203 | // __toString() |
||
204 | //////////////////////////////////////////////////////////////////////////// |
||
205 | public function testToString() |
||
218 | |||
219 | //////////////////////////////////////////////////////////////////////////// |
||
220 | // equals() |
||
221 | //////////////////////////////////////////////////////////////////////////// |
||
222 | public function testEquals() |
||
231 | |||
232 | //////////////////////////////////////////////////////////////////////////// |
||
233 | // greaterThan() |
||
234 | //////////////////////////////////////////////////////////////////////////// |
||
235 | View Code Duplication | public function testGreaterThan() |
|
242 | |||
243 | //////////////////////////////////////////////////////////////////////////// |
||
244 | // greaterThanOrEqual() |
||
245 | //////////////////////////////////////////////////////////////////////////// |
||
246 | View Code Duplication | public function testGreaterThanOrEqual() |
|
253 | |||
254 | //////////////////////////////////////////////////////////////////////////// |
||
255 | // lessThan() |
||
256 | //////////////////////////////////////////////////////////////////////////// |
||
257 | View Code Duplication | public function testLessThan() |
|
264 | |||
265 | //////////////////////////////////////////////////////////////////////////// |
||
266 | // lessThanOrEqual() |
||
267 | //////////////////////////////////////////////////////////////////////////// |
||
268 | View Code Duplication | public function testLessThanOrEqual() |
|
275 | } |
||
276 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.