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 |
||
| 41 | class ProductVarcharRepository extends AbstractRepository implements ProductVarcharRepositoryInterface |
||
| 42 | { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The cache adapter instance. |
||
| 46 | * |
||
| 47 | * @var \TechDivision\Import\Cache\CacheAdapterInterface |
||
| 48 | */ |
||
| 49 | protected $cacheAdapter; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The prepared statement to load the existing product varchar attributes with the passed entity/store ID. |
||
| 53 | * |
||
| 54 | * @var \PDOStatement |
||
| 55 | */ |
||
| 56 | protected $productVarcharsStmt; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The prepared statement to load the existing product varchar attribute with the passed attribute code |
||
| 60 | * entity type/store ID. |
||
| 61 | * |
||
| 62 | * @var \PDOStatement |
||
| 63 | */ |
||
| 64 | protected $productVarcharsByAttributeCodeAndEntityTypeIdAndStoreIdStmt; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The prepared statement to load the existing product varchar attribute with the passed attribute code |
||
| 68 | * entity type/store ID as well as the passed value. |
||
| 69 | * |
||
| 70 | * @var \PDOStatement |
||
| 71 | */ |
||
| 72 | protected $productVarcharByAttributeCodeAndEntityTypeIdAndStoreIdAndValueStmt; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Initialize the repository with the passed connection and utility class name. |
||
| 76 | * . |
||
| 77 | * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection instance |
||
| 78 | * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance |
||
| 79 | * @param \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter The cache adapter instance |
||
| 80 | */ |
||
| 81 | public function __construct( |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the cache adapter instance used to warm the repository. |
||
| 96 | * |
||
| 97 | * @return \TechDivision\Import\Cache\CacheAdapterInterface The repository's cache adapter instance |
||
| 98 | */ |
||
| 99 | public function getCacheAdapter() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Return's the primary key name of the entity. |
||
| 106 | * |
||
| 107 | * @return string The name of the entity's primary key |
||
| 108 | */ |
||
| 109 | public function getPrimaryKeyName() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Initializes the repository's prepared statements. |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | public function init() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Load's and return's the varchar attributes with the passed primary key/store ID. |
||
| 133 | * |
||
| 134 | * @param integer $pk The primary key of the attributes |
||
| 135 | * @param integer $storeId The store ID of the attributes |
||
| 136 | * |
||
| 137 | * @return array The varchar attributes |
||
| 138 | */ |
||
| 139 | public function findAllByPrimaryKeyAndStoreId($pk, $storeId) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Load's and return's the varchar attributes with the passed params. |
||
| 159 | * |
||
| 160 | * @param integer $attributeCode The attribute code of the varchar attribute |
||
| 161 | * @param integer $entityTypeId The entity type ID of the varchar attribute |
||
| 162 | * @param integer $storeId The store ID of the varchar attribute |
||
| 163 | * |
||
| 164 | * @return array The varchar attributes |
||
| 165 | */ |
||
| 166 | public function findAllByAttributeCodeAndEntityTypeIdAndStoreId($attributeCode, $entityTypeId, $storeId) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Load's and return's the varchar attribute with the passed params. |
||
| 187 | * |
||
| 188 | * @param integer $attributeCode The attribute code of the varchar attribute |
||
| 189 | * @param integer $entityTypeId The entity type ID of the varchar attribute |
||
| 190 | * @param integer $storeId The store ID of the varchar attribute |
||
| 191 | * @param string $value The value of the varchar attribute |
||
| 192 | * |
||
| 193 | * @return array|null The varchar attribute |
||
| 194 | */ |
||
| 195 | public function findOneByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value) |
||
| 228 | } |
||
| 229 |
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.