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 SlugTest extends PHPUnit_Framework_TestCase |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * If the given Slug is valid validateSlug should not throw an exception |
||
11 | * |
||
12 | * @test |
||
13 | * @dataProvider getValidSlugs |
||
14 | * |
||
15 | * @param string $sku A Slug |
||
16 | */ |
||
17 | View Code Duplication | public function validateSlug_validSlugs_NoExceptionIsThrown($sku) |
|
33 | |||
34 | /** |
||
35 | * If the given Slug is invalid validateSlug should throw an exception |
||
36 | * |
||
37 | * @test |
||
38 | * @dataProvider getInvalidSlugs |
||
39 | * |
||
40 | * @param string $sku A Slug |
||
41 | * @param string $expectedExceptionMessage The expected exception message |
||
42 | */ |
||
43 | View Code Duplication | public function validateSlug_invalidSlugs_ExceptionIsThrown($sku, $expectedExceptionMessage) |
|
64 | |||
65 | /** |
||
66 | * Get a list of valid Slugs |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public static function getValidSlugs() |
||
97 | |||
98 | /** |
||
99 | * Get a list of invalid Slugs |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public static function getInvalidSlugs() |
||
132 | } |
||
133 |
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.