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 |
||
| 12 | View Code Duplication | class CacheReportStatusRepository |
|
|
|
|||
| 13 | { |
||
| 14 | const TABLE = 'cache_report_status'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var Connection |
||
| 18 | */ |
||
| 19 | private $connection; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * CacheReportStatusRepository constructor. |
||
| 23 | * |
||
| 24 | * @param Connection $connection |
||
| 25 | */ |
||
| 26 | public function __construct(Connection $connection) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return array |
||
| 33 | * @throws RecordsNotFoundException |
||
| 34 | */ |
||
| 35 | public function fetchAll() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param array $where |
||
| 59 | * |
||
| 60 | * @return GeoCacheReportStatusEntity |
||
| 61 | * @throws RecordNotFoundException |
||
| 62 | */ |
||
| 63 | public function fetchOneBy(array $where = []) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $where |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | * @throws RecordsNotFoundException |
||
| 92 | */ |
||
| 93 | public function fetchBy(array $where = []) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param GeoCacheReportStatusEntity $entity |
||
| 124 | * |
||
| 125 | * @return GeoCacheReportStatusEntity |
||
| 126 | * @throws RecordAlreadyExistsException |
||
| 127 | * @throws \Doctrine\DBAL\DBALException |
||
| 128 | */ |
||
| 129 | public function create(GeoCacheReportStatusEntity $entity) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param GeoCacheReportStatusEntity $entity |
||
| 149 | * |
||
| 150 | * @return GeoCacheReportStatusEntity |
||
| 151 | * @throws RecordNotPersistedException |
||
| 152 | * @throws \Doctrine\DBAL\DBALException |
||
| 153 | */ |
||
| 154 | public function update(GeoCacheReportStatusEntity $entity) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param GeoCacheReportStatusEntity $entity |
||
| 173 | * |
||
| 174 | * @return GeoCacheReportStatusEntity |
||
| 175 | * @throws RecordNotPersistedException |
||
| 176 | * @throws \Doctrine\DBAL\DBALException |
||
| 177 | * @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
||
| 178 | */ |
||
| 179 | public function remove(GeoCacheReportStatusEntity $entity) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param GeoCacheReportStatusEntity $entity |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getDatabaseArrayFromEntity(GeoCacheReportStatusEntity $entity) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param array $data |
||
| 211 | * |
||
| 212 | * @return GeoCacheReportStatusEntity |
||
| 213 | */ |
||
| 214 | public function getEntityFromDatabaseArray(array $data) |
||
| 223 | } |
||
| 224 |
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.