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 | abstract class AbstractDbEntityFetcher |
||
17 | { |
||
18 | /** |
||
19 | * The database adapter/connection/handler. |
||
20 | * |
||
21 | * @var Db |
||
22 | */ |
||
23 | protected $db; |
||
24 | |||
25 | /** |
||
26 | * Database entity's class name (optional, meant to be overridden). |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $dbEntityClass; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param Db $db |
||
36 | */ |
||
37 | 13 | public function __construct(Db $db) |
|
41 | |||
42 | /** |
||
43 | * Helper method to get LIMIT SQL and paging flag from limit parameter. |
||
44 | * |
||
45 | * @param int $limit A limit number (like 5) |
||
46 | * @param array $pageItem A pagination array like [1, 10], where 1 is the page number and 10 the number of |
||
47 | * rows per page (first page is 1). |
||
48 | * @return string |
||
49 | */ |
||
50 | 3 | protected function getLimitSql($limit, array $pageItem = []) |
|
74 | |||
75 | /** |
||
76 | * Helper method to get pagination result array if pagination is requested. |
||
77 | * |
||
78 | * @param array $objects |
||
79 | * @param bool $pagination |
||
80 | * @return array |
||
81 | */ |
||
82 | 2 | View Code Duplication | protected function getFetchPaginationResult(array $objects, $pagination) |
92 | |||
93 | /** |
||
94 | * Helper method to get pagination result as objects if pagination is requested. |
||
95 | * |
||
96 | * @param array $rows |
||
97 | * @param bool $pagination |
||
98 | * @return array |
||
99 | */ |
||
100 | 2 | View Code Duplication | protected function getDbEntitiesFromRowsPaginated(array $rows, $pagination) |
109 | |||
110 | /** |
||
111 | * @param array $rows |
||
112 | * @param string $keyPropertyName Optional, will default to primary if not provided |
||
113 | * @return array |
||
114 | */ |
||
115 | 6 | protected function getDbEntitiesFromRows(array &$rows, $keyPropertyName = null) |
|
149 | |||
150 | /** |
||
151 | * @param array $rowData |
||
152 | * @return AbstractDbEntity |
||
153 | */ |
||
154 | 6 | protected function createNewDbEntity(array $rowData) |
|
162 | |||
163 | /** |
||
164 | * @param AbstractDbEntity $dbEntity |
||
165 | * @param array $rowData |
||
166 | */ |
||
167 | 6 | protected function setDbEntityDataFromRow(AbstractDbEntity $dbEntity, array $rowData) |
|
171 | |||
172 | /** |
||
173 | * @param array|false $row |
||
174 | * @return AbstractDbEntity|null |
||
175 | */ |
||
176 | 2 | protected function getDbEntityFromRow($row) |
|
184 | } |
||
185 |
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.