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 |
||
| 12 | class GeoCacheLogRepository |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Database table name that this repository maintains. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | const TABLE = 'cache_logs'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Connection |
||
|
|
|||
| 23 | */ |
||
| 24 | private $connection; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * GeoCacheLogRepository constructor. |
||
| 28 | * |
||
| 29 | * @param Connection $connection |
||
| 30 | */ |
||
| 31 | public function __construct(Connection $connection) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Fetches all GeoCacheLogs. |
||
| 38 | * |
||
| 39 | * @return GeoCacheLogEntity[] |
||
| 40 | * |
||
| 41 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function fetchAll() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Fetches a GeoCacheLog by given where clause. |
||
| 67 | * |
||
| 68 | * @param array $where |
||
| 69 | * |
||
| 70 | * @return null|GeoCacheLogEntity |
||
| 71 | * |
||
| 72 | * @throws RecordNotFoundException Thrown when no record is found |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function fetchOneBy(array $where = []) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Fetches all GeoCacheLogs by given where clause. |
||
| 100 | * |
||
| 101 | * @param array $where |
||
| 102 | * |
||
| 103 | * @return GeoCacheLogEntity[] |
||
| 104 | * |
||
| 105 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function fetchBy(array $where = []) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Fetch latest user geo cache log. |
||
| 138 | * |
||
| 139 | * @param int $userId |
||
| 140 | * |
||
| 141 | * @return GeoCacheLogEntity |
||
| 142 | * |
||
| 143 | * @throws RecordNotFoundException |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function getLatestUserLog($userId) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Creates a GeoCacheLog in the database. |
||
| 168 | * |
||
| 169 | * @param GeoCacheLogEntity $entity |
||
| 170 | * |
||
| 171 | * @return GeoCacheLogEntity |
||
| 172 | * |
||
| 173 | * @throws RecordAlreadyExistsException |
||
| 174 | */ |
||
| 175 | public function create(GeoCacheLogEntity $entity) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Update a GeoCacheLog in the database. |
||
| 195 | * |
||
| 196 | * @param GeoCacheLogEntity $entity |
||
| 197 | * |
||
| 198 | * @return GeoCacheLogEntity |
||
| 199 | * |
||
| 200 | * @throws RecordNotPersistedException |
||
| 201 | */ |
||
| 202 | public function update(GeoCacheLogEntity $entity) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Removes a GeoCacheLog from the database. |
||
| 223 | * |
||
| 224 | * @param GeoCacheLogEntity $entity |
||
| 225 | * |
||
| 226 | * @return GeoCacheLogEntity |
||
| 227 | * |
||
| 228 | * @throws RecordNotPersistedException |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function remove(GeoCacheLogEntity $entity) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Maps the given entity to the database array. |
||
| 248 | * |
||
| 249 | * @param GeoCacheLogEntity $entity |
||
| 250 | * |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function getDatabaseArrayFromEntity(GeoCacheLogEntity $entity) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Prepares database array from properties. |
||
| 282 | * |
||
| 283 | * @param array $data |
||
| 284 | * |
||
| 285 | * @return GeoCacheLogEntity |
||
| 286 | */ |
||
| 287 | public function getEntityFromDatabaseArray(array $data) |
||
| 314 | } |
||
| 315 |