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 ContentMapperTest extends PHPUnit_Framework_TestCase |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * If the given summary and description is valid a Content model with the given summary and description should be |
||
11 | * returned |
||
12 | * |
||
13 | * @test |
||
14 | * @dataProvider getValidContentData |
||
15 | * |
||
16 | * @param array $contentData Product content data |
||
17 | */ |
||
18 | public function getContent_ValidSummaryAndDescriptionGiven_ContentWithSummaryAndDescriptionIsReturned($contentData) |
||
30 | |||
31 | /** |
||
32 | * If the given summary and description is valid a Content model with the given summary and description should be |
||
33 | * returned |
||
34 | * |
||
35 | * @test |
||
36 | * @dataProvider getContentDataWithMissingAttributes |
||
37 | * @expectedException Wambo\Catalog\Error\ContentException |
||
38 | * @expectedExceptionMessageRegExp /The field '.+' is missing in the given content data/ |
||
39 | * |
||
40 | * @param array $contentData Product content data |
||
41 | */ |
||
42 | public function getContent_FieldsMissing_ContentExceptionIsThrown($contentData) |
||
50 | |||
51 | /** |
||
52 | * If the given summary is invalid a ContentException should be thrown |
||
53 | * |
||
54 | * @test |
||
55 | * @dataProvider getContentWithInvalidSummary |
||
56 | * @expectedException Wambo\Catalog\Error\ContentException |
||
57 | * @expectedExceptionMessageRegExp /The summary text should not be (shorter|longer) than \d+ characters/ |
||
58 | * |
||
59 | * @param array $contentData Product content data |
||
60 | */ |
||
61 | public function getContent_InvalidSummary_ContentExceptionIsThrown($contentData) |
||
69 | |||
70 | /** |
||
71 | * Get a list of valid content data for testing |
||
72 | * @return array |
||
73 | */ |
||
74 | View Code Duplication | public static function getValidContentData() { |
|
89 | |||
90 | /** |
||
91 | * Get a list of valid content data for testing |
||
92 | * @return array |
||
93 | */ |
||
94 | public static function getContentWithInvalidSummary() { |
||
114 | |||
115 | /** |
||
116 | * Get a list of content data object with missing attribtues for testing |
||
117 | * @return array |
||
118 | */ |
||
119 | View Code Duplication | public static function getContentDataWithMissingAttributes() { |
|
135 | } |
||
136 |
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.