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 |
||
| 17 | class GeoCacheLogRepository |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Database table name that this repository maintains. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | const TABLE = 'cache_logs'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Connection |
||
| 28 | */ |
||
| 29 | private $connection; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * GeoCacheLogRepository constructor. |
||
| 33 | * |
||
| 34 | * @param Connection $connection |
||
| 35 | */ |
||
| 36 | public function __construct(Connection $connection) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Fetches all GeoCacheLogs. |
||
| 43 | * |
||
| 44 | * @return GeoCacheLogEntity[] |
||
| 45 | * |
||
| 46 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function fetchAll() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Fetches a GeoCacheLog by given where clause. |
||
| 72 | * |
||
| 73 | * @param array $where |
||
| 74 | * |
||
| 75 | * @return null|GeoCacheLogEntity |
||
| 76 | * |
||
| 77 | * @throws RecordNotFoundException Thrown when no record is found |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function fetchOneBy(array $where = []) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Fetches all GeoCacheLogs by given where clause. |
||
| 105 | * |
||
| 106 | * @param array $where |
||
| 107 | * |
||
| 108 | * @return GeoCacheLogEntity[] |
||
| 109 | * |
||
| 110 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function fetchBy(array $where = []) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Fetch latest user geo cache log. |
||
| 143 | * |
||
| 144 | * @param int $userId |
||
| 145 | * |
||
| 146 | * @return GeoCacheLogEntity |
||
| 147 | * |
||
| 148 | * @throws RecordNotFoundException |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function getLatestUserLog($userId) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Creates a GeoCacheLog in the database. |
||
| 173 | * |
||
| 174 | * @param GeoCacheLogEntity $entity |
||
| 175 | * |
||
| 176 | * @return GeoCacheLogEntity |
||
| 177 | * |
||
| 178 | * @throws RecordAlreadyExistsException |
||
| 179 | */ |
||
| 180 | public function create(GeoCacheLogEntity $entity) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Update a GeoCacheLog in the database. |
||
| 200 | * |
||
| 201 | * @param GeoCacheLogEntity $entity |
||
| 202 | * |
||
| 203 | * @return GeoCacheLogEntity |
||
| 204 | * |
||
| 205 | * @throws RecordNotPersistedException |
||
| 206 | */ |
||
| 207 | public function update(GeoCacheLogEntity $entity) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Removes a GeoCacheLog from the database. |
||
| 228 | * |
||
| 229 | * @param GeoCacheLogEntity $entity |
||
| 230 | * |
||
| 231 | * @return GeoCacheLogEntity |
||
| 232 | * |
||
| 233 | * @throws RecordNotPersistedException |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function remove(GeoCacheLogEntity $entity) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Maps the given entity to the database array. |
||
| 253 | * |
||
| 254 | * @param GeoCacheLogEntity $entity |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getDatabaseArrayFromEntity(GeoCacheLogEntity $entity) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Prepares database array from properties. |
||
| 287 | * |
||
| 288 | * @param array $data |
||
| 289 | * |
||
| 290 | * @return GeoCacheLogEntity |
||
| 291 | */ |
||
| 292 | public function getEntityFromDatabaseArray(array $data) |
||
| 319 | } |
||
| 320 |