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 |
||
| 35 | class ProductRepository extends AbstractCachedRepository implements ProductRepositoryInterface |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The prepared statement to load a product with the passed SKU. |
||
| 40 | * |
||
| 41 | * @var \PDOStatement |
||
| 42 | */ |
||
| 43 | protected $productStmt; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The prepared statement to load the existing products. |
||
| 47 | * |
||
| 48 | * @var \PDOStatement |
||
| 49 | */ |
||
| 50 | protected $productsStmt; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Return's the primary key name of the entity. |
||
| 54 | * |
||
| 55 | * @return string The name of the entity's primary key |
||
| 56 | */ |
||
| 57 | public function getPrimaryKeyName() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initializes the repository's prepared statements. |
||
| 64 | * |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function init() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Return's the available products. |
||
| 82 | * |
||
| 83 | * @return array The available products |
||
| 84 | */ |
||
| 85 | public function findAll() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return's the product with the passed SKU. |
||
| 94 | * |
||
| 95 | * @param string $sku The SKU of the product to return |
||
| 96 | * |
||
| 97 | * @return array|null The product |
||
| 98 | */ |
||
| 99 | public function findOneBySku($sku) |
||
| 122 | } |
||
| 123 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.