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 |
||
| 8 | class Test extends \PHPUnit\Framework\TestCase |
||
|
|
|||
| 9 | {
|
||
| 10 | /** @test */ |
||
| 11 | public function emptyPassword() |
||
| 19 | |||
| 20 | /** @test */ |
||
| 21 | View Code Duplication | public function letterPasswordOnly() |
|
|
1 ignored issue
–
show
|
|||
| 22 | {
|
||
| 23 | $pw = new Password(); |
||
| 24 | |||
| 25 | $pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(10)); |
||
| 26 | |||
| 27 | $str = $pw->generate(); |
||
| 28 | |||
| 29 | $this->assertNotEmpty($str); |
||
| 30 | |||
| 31 | $this->assertRegExp('/[a-z]/', $str);
|
||
| 32 | } |
||
| 33 | |||
| 34 | /** @test */ |
||
| 35 | public function alphanumericPasswordOnly() |
||
| 51 | |||
| 52 | /** @test */ |
||
| 53 | View Code Duplication | public function accentedLetterOnly() |
|
| 69 | |||
| 70 | /** @test */ |
||
| 71 | View Code Duplication | public function accentedUppercaseLetterOnly() |
|
| 87 | |||
| 88 | /** @test */ |
||
| 89 | public function specialCharacterOnly() |
||
| 109 | |||
| 110 | /** @test */ |
||
| 111 | public function longDigitFormatOnly() |
||
| 123 | |||
| 124 | /** @test */ |
||
| 125 | View Code Duplication | public function smallAlphanumericFormatOnly() |
|
| 139 | |||
| 140 | /** @test */ |
||
| 141 | View Code Duplication | public function strongAlphanumericFormatOnly() |
|
| 155 | |||
| 156 | /** @test */ |
||
| 157 | public function mixedStrongFormatOnly() |
||
| 171 | |||
| 172 | /** @test */ |
||
| 173 | public function mixedComplexFormatOnly() |
||
| 187 | |||
| 188 | |||
| 189 | /** @test */ |
||
| 190 | View Code Duplication | public function digitPasswordOnly() |
|
|
1 ignored issue
–
show
|
|||
| 191 | {
|
||
| 192 | $pw = new Password(); |
||
| 193 | |||
| 194 | $pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(10)); |
||
| 195 | |||
| 196 | $str = $pw->generate(); |
||
| 197 | |||
| 198 | $this->assertNotEmpty($str); |
||
| 199 | |||
| 200 | $this->assertRegExp('/\d{10}/', $str);
|
||
| 201 | } |
||
| 202 | |||
| 203 | /** @test */ |
||
| 204 | public function strictOccurrences() |
||
| 218 | |||
| 219 | /** @test */ |
||
| 220 | public function betweenOccurrences() |
||
| 233 | |||
| 234 | /** @test */ |
||
| 235 | public function perfectScore() |
||
| 248 | } |
||
| 249 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.