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 |
||
| 16 | class CacheStatusModifiedRepository |
||
| 17 | { |
||
| 18 | const TABLE = 'cache_status_modified'; |
||
| 19 | |||
| 20 | /** @var Connection */ |
||
| 21 | private $connection; |
||
| 22 | |||
| 23 | /** @var UserRepository */ |
||
| 24 | private $userRepository; |
||
| 25 | |||
| 26 | /** @var CacheReportStatusRepository */ |
||
| 27 | private $cacheReportStatusRepository; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * CacheStatusModifiedRepository constructor. |
||
| 31 | * |
||
| 32 | * @param Connection $connection |
||
| 33 | */ |
||
| 34 | public function __construct( |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return array |
||
| 47 | * @throws RecordsNotFoundException |
||
| 48 | */ |
||
| 49 | public function fetchAll() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param array $where |
||
| 73 | * |
||
| 74 | * @return GeoCacheStatusModifiedEntity |
||
| 75 | * @throws RecordNotFoundException |
||
| 76 | */ |
||
| 77 | public function fetchOneBy(array $where = []) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $where |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | * @throws RecordsNotFoundException |
||
| 106 | */ |
||
| 107 | public function fetchBy(array $where = []) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param GeoCacheStatusModifiedEntity $entity |
||
| 138 | * |
||
| 139 | * @return GeoCacheStatusModifiedEntity |
||
| 140 | * @throws RecordAlreadyExistsException |
||
| 141 | * @throws \Doctrine\DBAL\DBALException |
||
| 142 | */ |
||
| 143 | public function create(GeoCacheStatusModifiedEntity $entity) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param GeoCacheStatusModifiedEntity $entity |
||
| 163 | * |
||
| 164 | * @return GeoCacheStatusModifiedEntity |
||
| 165 | * @throws RecordNotPersistedException |
||
| 166 | * @throws \Doctrine\DBAL\DBALException |
||
| 167 | */ |
||
| 168 | public function update(GeoCacheStatusModifiedEntity $entity) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param GeoCacheStatusModifiedEntity $entity |
||
| 187 | * |
||
| 188 | * @return GeoCacheStatusModifiedEntity |
||
| 189 | * @throws RecordNotPersistedException |
||
| 190 | * @throws \Doctrine\DBAL\DBALException |
||
| 191 | * @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
||
| 192 | */ |
||
| 193 | public function remove(GeoCacheStatusModifiedEntity $entity) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param GeoCacheStatusModifiedEntity $entity |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function getDatabaseArrayFromEntity(GeoCacheStatusModifiedEntity $entity) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $data |
||
| 227 | * |
||
| 228 | * @return GeoCacheStatusModifiedEntity |
||
| 229 | */ |
||
| 230 | public function getEntityFromDatabaseArray(array $data) |
||
| 244 | } |
||
| 245 |
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.