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 | // sort() |
||
204 | //////////////////////////////////////////////////////////////////////////// |
||
205 | public function testSort() |
||
243 | |||
244 | //////////////////////////////////////////////////////////////////////////// |
||
245 | // __toString() |
||
246 | //////////////////////////////////////////////////////////////////////////// |
||
247 | public function testToString() |
||
260 | |||
261 | //////////////////////////////////////////////////////////////////////////// |
||
262 | // greaterThan() |
||
263 | //////////////////////////////////////////////////////////////////////////// |
||
264 | View Code Duplication | public function testGreaterThan() |
|
271 | |||
272 | //////////////////////////////////////////////////////////////////////////// |
||
273 | // greaterThanOrEqual() |
||
274 | //////////////////////////////////////////////////////////////////////////// |
||
275 | View Code Duplication | public function testGreaterThanOrEqual() |
|
282 | |||
283 | //////////////////////////////////////////////////////////////////////////// |
||
284 | // lessThan() |
||
285 | //////////////////////////////////////////////////////////////////////////// |
||
286 | View Code Duplication | public function testLessThan() |
|
293 | |||
294 | //////////////////////////////////////////////////////////////////////////// |
||
295 | // lessThanOrEqual() |
||
296 | //////////////////////////////////////////////////////////////////////////// |
||
297 | View Code Duplication | public function testLessThanOrEqual() |
|
304 | } |
||
305 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.