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 |
||
| 18 | class FieldNoteRepository |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Database table name that this repository maintains. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | const TABLE = 'field_note'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Connection |
||
| 29 | */ |
||
| 30 | private $connection; |
||
| 31 | /** |
||
| 32 | * @var GeoCacheRepository |
||
| 33 | */ |
||
| 34 | private $geoCacheRepository; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * FieldNoteRepository constructor. |
||
| 38 | * |
||
| 39 | * @param Connection $connection |
||
| 40 | * @param GeoCacheRepository $geoCacheRepository |
||
| 41 | */ |
||
| 42 | public function __construct(Connection $connection, GeoCacheRepository $geoCacheRepository) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Fetches all countries. |
||
| 50 | * |
||
| 51 | * @return FieldNoteEntity[] |
||
| 52 | * |
||
| 53 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function fetchAll() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Fetches all GeoCaches by given where clause. |
||
| 79 | * |
||
| 80 | * @param array $where |
||
| 81 | * |
||
| 82 | * @return FieldNoteEntity[] |
||
| 83 | * |
||
| 84 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 85 | */ |
||
| 86 | public function fetchBy(array $where = [], array $order = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Fetches a field note by given where clause. |
||
| 119 | * |
||
| 120 | * @param array $where |
||
| 121 | * |
||
| 122 | * @return null|FieldNoteEntity |
||
| 123 | * |
||
| 124 | * @throws RecordNotFoundException Thrown when no record is found |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function fetchOneBy(array $where = []) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Fetch latest user field note. |
||
| 152 | * |
||
| 153 | * @param int $userId |
||
| 154 | * |
||
| 155 | * @return FieldNoteEntity |
||
| 156 | * |
||
| 157 | * @throws RecordNotFoundException |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function getLatestUserFieldNote($userId) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Creates a field note in the database. |
||
| 182 | * |
||
| 183 | * @param FieldNoteEntity $entity |
||
| 184 | * |
||
| 185 | * @return FieldNoteEntity |
||
| 186 | * |
||
| 187 | * @throws RecordAlreadyExistsException |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function create(FieldNoteEntity $entity) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Update a field note in the database. |
||
| 209 | * |
||
| 210 | * @param FieldNoteEntity $entity |
||
| 211 | * |
||
| 212 | * @return FieldNoteEntity |
||
| 213 | * |
||
| 214 | * @throws RecordNotPersistedException |
||
| 215 | */ |
||
| 216 | public function update(FieldNoteEntity $entity) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Removes a field note from the database. |
||
| 237 | * |
||
| 238 | * @param FieldNoteEntity $entity |
||
| 239 | * |
||
| 240 | * @return FieldNoteEntity |
||
| 241 | * |
||
| 242 | * @throws RecordNotPersistedException |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function remove(FieldNoteEntity $entity) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Maps the given entity to the database array. |
||
| 262 | * |
||
| 263 | * @param FieldNoteEntity $entity |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function getDatabaseArrayFromEntity(FieldNoteEntity $entity) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Prepares database array from properties. |
||
| 281 | * |
||
| 282 | * @param array $data |
||
| 283 | * |
||
| 284 | * @return FieldNoteEntity |
||
| 285 | */ |
||
| 286 | public function getEntityFromDatabaseArray(array $data) |
||
| 301 | } |
||
| 302 |