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) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * If the SKU validation fails and exception should be thrown |
||
| 64 | * |
||
| 65 | * @test |
||
| 66 | * @expectedException Wambo\Catalog\Error\ProductException |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function getProduct_AllRequiredFieldsPresent_SkuValidationFails_ProductIsReturned() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * If the Slug validation fails and exception should be thrown |
||
| 90 | * |
||
| 91 | * @test |
||
| 92 | * @expectedException Wambo\Catalog\Error\ProductException |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function getProduct_AllRequiredFieldsPresent_SlugValidationFails_ProductIsReturned() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get product data with one or more missing attributes. |
||
| 116 | */ |
||
| 117 | public static function getProductDataWithMissingAttribute() |
||
| 161 | } |
||
| 162 |
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.