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 |
||
| 34 | class EavAttributeRepository extends AbstractRepository |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The prepared statement to load the attributes by the passed is user defined flag. |
||
| 39 | * |
||
| 40 | * @var \PDOStatement |
||
| 41 | */ |
||
| 42 | protected $eavAttributesByUserDefinedStmt; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The prepared statement to load the attributes for a specifc option value and store ID. |
||
| 46 | * |
||
| 47 | * @var \PDOStatement |
||
| 48 | */ |
||
| 49 | protected $eavAttributesByOptionValueAndStoreIdStmt; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The prepared statement to load the attributes for a specific entity type ID and name. |
||
| 53 | * |
||
| 54 | * @var \PDOStatement |
||
| 55 | */ |
||
| 56 | protected $eavAttributesByEntityTypeIdAndAttributeSetNameStmt; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Initializes the repository's prepared statements. |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function init() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Return's an array with the available EAV attributes for the passed entity type ID and attribute set name. |
||
| 77 | * |
||
| 78 | * @param integer $entityTypeId The entity type ID of the EAV attributes to return |
||
| 79 | * @param string $attributeSetName The attribute set name of the EAV attributes to return |
||
| 80 | * |
||
| 81 | * @return array The array with all available EAV attributes |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function findAllByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Return's an array with the available EAV attributes for the passed option value and store ID. |
||
| 107 | * |
||
| 108 | * @param string $optionValue The option value of the EAV attributes |
||
| 109 | * @param string $storeId The store ID of the EAV attribues |
||
| 110 | * |
||
| 111 | * @return array The array with all available EAV attributes |
||
| 112 | */ |
||
| 113 | public function findAllByOptionValueAndStoreId($optionValue, $storeId) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
| 129 | * |
||
| 130 | * @param integer $isUserDefined The flag itself |
||
| 131 | * |
||
| 132 | * @return array The array with the EAV attributes matching the passed flag |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function findAllByIsUserDefined($isUserDefined = 1) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Return's the first EAV attribute for the passed option value and store ID. |
||
| 155 | * |
||
| 156 | * @param string $optionValue The option value of the EAV attributes |
||
| 157 | * @param string $storeId The store ID of the EAV attribues |
||
| 158 | * |
||
| 159 | * @return array The array with the EAV attribute |
||
| 160 | * @see \Importer\Csv\Repositories\Pdo\EavAttributeRepository::findAllByOptionValueAndStoreId() |
||
| 161 | */ |
||
| 162 | public function findOneByOptionValueAndStoreId($optionValue, $storeId) |
||
| 170 | } |
||
| 171 |
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.