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 | View Code Duplication | class CacheReportReasonsRepository |
|
|
|
|||
| 14 | { |
||
| 15 | const TABLE = 'cache_report_reasons'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var Connection |
||
| 19 | */ |
||
| 20 | private $connection; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * CacheReportReasonsRepository constructor. |
||
| 24 | * |
||
| 25 | * @param Connection $connection |
||
| 26 | */ |
||
| 27 | public function __construct(Connection $connection) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return array |
||
| 34 | * @throws RecordsNotFoundException |
||
| 35 | */ |
||
| 36 | public function fetchAll() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param array $where |
||
| 60 | * |
||
| 61 | * @return GeoCacheReportReasonsEntity |
||
| 62 | * @throws RecordNotFoundException |
||
| 63 | */ |
||
| 64 | public function fetchOneBy(array $where = []) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param array $where |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | * @throws RecordsNotFoundException |
||
| 93 | */ |
||
| 94 | public function fetchBy(array $where = []) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param GeoCacheReportReasonsEntity $entity |
||
| 125 | * |
||
| 126 | * @return GeoCacheReportReasonsEntity |
||
| 127 | * @throws RecordAlreadyExistsException |
||
| 128 | * @throws \Doctrine\DBAL\DBALException |
||
| 129 | */ |
||
| 130 | public function create(GeoCacheReportReasonsEntity $entity) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param GeoCacheReportReasonsEntity $entity |
||
| 150 | * |
||
| 151 | * @return GeoCacheReportReasonsEntity |
||
| 152 | * @throws RecordNotPersistedException |
||
| 153 | * @throws \Doctrine\DBAL\DBALException |
||
| 154 | */ |
||
| 155 | public function update(GeoCacheReportReasonsEntity $entity) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param GeoCacheReportReasonsEntity $entity |
||
| 174 | * |
||
| 175 | * @return GeoCacheReportReasonsEntity |
||
| 176 | * @throws RecordNotPersistedException |
||
| 177 | * @throws \Doctrine\DBAL\DBALException |
||
| 178 | * @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
||
| 179 | */ |
||
| 180 | public function remove(GeoCacheReportReasonsEntity $entity) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param GeoCacheReportReasonsEntity $entity |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public function getDatabaseArrayFromEntity(GeoCacheReportReasonsEntity $entity) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param array $data |
||
| 213 | * |
||
| 214 | * @return GeoCacheReportReasonsEntity |
||
| 215 | */ |
||
| 216 | public function getEntityFromDatabaseArray(array $data) |
||
| 226 | } |
||
| 227 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.