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 |
||
9 | class ProductMapperTest extends PHPUnit_Framework_TestCase |
||
|
|||
10 | { |
||
11 | /** |
||
12 | * If all required fields are given and the validation passes a product should be returned |
||
13 | * |
||
14 | * @test |
||
15 | */ |
||
16 | View Code Duplication | public function getProduct_AllRequiredFieldsPresent_ValidationPasses_ProductIsReturned() |
|
38 | |||
39 | /** |
||
40 | * If given product data is missing one of the mandatory fields and exception should be thrown. |
||
41 | * |
||
42 | * @param array $productData Unstructured product data |
||
43 | * |
||
44 | * @test |
||
45 | * |
||
46 | * @expectedException \Wambo\Catalog\Error\ProductException |
||
47 | * @expectedExceptionMessageRegExp /The field '\w+' is missing in the given product data/ |
||
48 | * @dataProvider getProductDataWithMissingAttribute |
||
49 | */ |
||
50 | public function getProduct_RequiredFieldMissing_ProductExceptionIsThrown(array $productData) |
||
62 | |||
63 | /** |
||
64 | * If the SKU validation fails and exception should be thrown |
||
65 | * |
||
66 | * @test |
||
67 | * @expectedException Wambo\Catalog\Error\ProductException |
||
68 | */ |
||
69 | View Code Duplication | public function getProduct_AllRequiredFieldsPresent_SkuValidationFails_ProductIsReturned() |
|
88 | |||
89 | /** |
||
90 | * If the Slug validation fails and exception should be thrown |
||
91 | * |
||
92 | * @test |
||
93 | * @expectedException Wambo\Catalog\Error\ProductException |
||
94 | */ |
||
95 | View Code Duplication | public function getProduct_AllRequiredFieldsPresent_SlugValidationFails_ProductIsReturned() |
|
114 | |||
115 | /** |
||
116 | * Get product data with one or more missing attributes. |
||
117 | */ |
||
118 | public static function getProductDataWithMissingAttribute() |
||
151 | } |
||
152 |
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.