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 UserRepository |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Database table name that this repository maintains. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | const TABLE = 'user'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Connection |
||
| 27 | */ |
||
| 28 | private $connection; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * UserRepository constructor. |
||
| 32 | * |
||
| 33 | * @param Connection $connection |
||
| 34 | */ |
||
| 35 | public function __construct(Connection $connection) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Fetches all users. |
||
| 42 | * |
||
| 43 | * @return UserEntity[] |
||
| 44 | * |
||
| 45 | * @throws RecordsNotFoundException Thrown when no records are found |
||
| 46 | */ |
||
| 47 | public function fetchAll() |
||
| 48 | { |
||
| 49 | $statement = $this->connection->createQueryBuilder() |
||
| 50 | ->select('*') |
||
| 51 | ->from(self::TABLE) |
||
| 52 | ->execute(); |
||
| 53 | |||
| 54 | $result = $statement->fetchAll(); |
||
| 55 | |||
| 56 | if ($statement->rowCount() === 0) { |
||
| 57 | throw new RecordsNotFoundException('No records found'); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $this->getEntityArrayFromDatabaseArray($result); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Fetches a user by its id. |
||
| 65 | * |
||
| 66 | * @param int $id |
||
| 67 | * |
||
| 68 | * @return UserEntity |
||
| 69 | * |
||
| 70 | * @throws RecordNotFoundException Thrown when the request record is not found |
||
| 71 | */ |
||
| 72 | public function fetchOneById($id) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Fetches a user by given where array. |
||
| 81 | * |
||
| 82 | * @param array $where |
||
| 83 | * |
||
| 84 | * @return UserEntity|null |
||
| 85 | * |
||
| 86 | * @throws RecordNotFoundException Thrown when no record is found |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function fetchOneBy(array $where = []) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Creates a user in the database. |
||
| 115 | * |
||
| 116 | * @param UserEntity $entity |
||
| 117 | * |
||
| 118 | * @return UserEntity |
||
| 119 | * |
||
| 120 | * @throws RecordAlreadyExistsException |
||
| 121 | */ |
||
| 122 | public function create(UserEntity $entity) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Update a user in the database. |
||
| 142 | * |
||
| 143 | * @param UserEntity $entity |
||
| 144 | * |
||
| 145 | * @return UserEntity |
||
| 146 | * |
||
| 147 | * @throws RecordNotPersistedException |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function update(UserEntity $entity) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Removes a user from the database. |
||
| 170 | * |
||
| 171 | * @param UserEntity $entity |
||
| 172 | * |
||
| 173 | * @return UserEntity |
||
| 174 | * |
||
| 175 | * @throws RecordNotPersistedException |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function remove(UserEntity $entity) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Converts database array to entity array. |
||
| 198 | * |
||
| 199 | * @param array $result |
||
| 200 | * |
||
| 201 | * @return UserEntity[] |
||
| 202 | */ |
||
| 203 | private function getEntityArrayFromDatabaseArray(array $result) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Maps the given entity to the database array. |
||
| 216 | * |
||
| 217 | * @param UserEntity $entity |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getDatabaseArrayFromEntity(UserEntity $entity) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Prepares database array from properties. |
||
| 240 | * |
||
| 241 | * @param array $data |
||
| 242 | * |
||
| 243 | * @return UserEntity |
||
| 244 | */ |
||
| 245 | public function getEntityFromDatabaseArray(array $data) |
||
| 262 | } |
||
| 263 |