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 |
||
| 13 | class FieldNoteRepository |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Database table name that this repository maintains. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | const TABLE = 'field_note'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Connection |
||
|
|
|||
| 24 | */ |
||
| 25 | private $connection; |
||
| 26 | /** |
||
| 27 | * @var GeoCacheRepository |
||
| 28 | */ |
||
| 29 | private $geoCacheRepository; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param Connection $connection |
||
| 33 | * @param GeoCacheRepository $geoCacheRepository |
||
| 34 | */ |
||
| 35 | public function __construct(Connection $connection, GeoCacheRepository $geoCacheRepository) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Fetches all countries. |
||
| 43 | * |
||
| 44 | * @return FieldNoteEntity[] |
||
| 45 | * |
||
| 46 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function fetchAll() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Fetches all GeoCaches by given where clause. |
||
| 72 | * |
||
| 73 | * @param array $where |
||
| 74 | * |
||
| 75 | * @return FieldNoteEntity[] |
||
| 76 | * |
||
| 77 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 78 | */ |
||
| 79 | public function fetchBy(array $where = [], array $order = []) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Fetches a field note by given where clause. |
||
| 112 | * |
||
| 113 | * @param array $where |
||
| 114 | * |
||
| 115 | * @return null|FieldNoteEntity |
||
| 116 | * |
||
| 117 | * @throws RecordNotFoundException Thrown when no record is found |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function fetchOneBy(array $where = []) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Fetch latest user field note. |
||
| 145 | * |
||
| 146 | * @param int $userId |
||
| 147 | * |
||
| 148 | * @return FieldNoteEntity |
||
| 149 | * |
||
| 150 | * @throws RecordNotFoundException |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function getLatestUserFieldNote($userId) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Creates a field note in the database. |
||
| 175 | * |
||
| 176 | * @param FieldNoteEntity $entity |
||
| 177 | * |
||
| 178 | * @return FieldNoteEntity |
||
| 179 | * |
||
| 180 | * @throws RecordAlreadyExistsException |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function create(FieldNoteEntity $entity) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Update a field note in the database. |
||
| 202 | * |
||
| 203 | * @param FieldNoteEntity $entity |
||
| 204 | * |
||
| 205 | * @return FieldNoteEntity |
||
| 206 | * |
||
| 207 | * @throws RecordNotPersistedException |
||
| 208 | */ |
||
| 209 | public function update(FieldNoteEntity $entity) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Removes a field note from the database. |
||
| 230 | * |
||
| 231 | * @param FieldNoteEntity $entity |
||
| 232 | * |
||
| 233 | * @return FieldNoteEntity |
||
| 234 | * |
||
| 235 | * @throws RecordNotPersistedException |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function remove(FieldNoteEntity $entity) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Maps the given entity to the database array. |
||
| 255 | * |
||
| 256 | * @param FieldNoteEntity $entity |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getDatabaseArrayFromEntity(FieldNoteEntity $entity) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Prepares database array from properties. |
||
| 274 | * |
||
| 275 | * @param array $data |
||
| 276 | * |
||
| 277 | * @return FieldNoteEntity |
||
| 278 | */ |
||
| 279 | public function getEntityFromDatabaseArray(array $data) |
||
| 294 | } |
||
| 295 |