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 |
||
9 | View Code Duplication | class CacheListBookmarksRepository |
|
|
|||
10 | { |
||
11 | const TABLE = 'cache_list_bookmarks'; |
||
12 | |||
13 | /** @var Connection */ |
||
14 | private $connection; |
||
15 | |||
16 | public function __construct(Connection $connection) |
||
20 | |||
21 | /** |
||
22 | * @return GeoCacheListBookmarksEntity[] |
||
23 | */ |
||
24 | public function fetchAll() |
||
45 | |||
46 | /** |
||
47 | * @param array $where |
||
48 | * @return GeoCacheListBookmarksEntity |
||
49 | */ |
||
50 | public function fetchOneBy(array $where = []) |
||
73 | |||
74 | /** |
||
75 | * @param array $where |
||
76 | * @return GeoCacheListBookmarksEntity[] |
||
77 | */ |
||
78 | public function fetchBy(array $where = []) |
||
106 | |||
107 | /** |
||
108 | * @param GeoCacheListBookmarksEntity $entity |
||
109 | * @return GeoCacheListBookmarksEntity |
||
110 | */ |
||
111 | public function create(GeoCacheListBookmarksEntity $entity) |
||
128 | |||
129 | /** |
||
130 | * @param GeoCacheListBookmarksEntity $entity |
||
131 | * @return GeoCacheListBookmarksEntity |
||
132 | */ |
||
133 | public function update(GeoCacheListBookmarksEntity $entity) |
||
149 | |||
150 | /** |
||
151 | * @param GeoCacheListBookmarksEntity $entity |
||
152 | * @return GeoCacheListBookmarksEntity |
||
153 | */ |
||
154 | public function remove(GeoCacheListBookmarksEntity $entity) |
||
169 | |||
170 | /** |
||
171 | * @param GeoCacheListBookmarksEntity $entity |
||
172 | * @return [] |
||
173 | */ |
||
174 | public function getDatabaseArrayFromEntity(GeoCacheListBookmarksEntity $entity) |
||
182 | |||
183 | /** |
||
184 | * @param array $data |
||
185 | * @return GeoCacheListBookmarksEntity |
||
186 | */ |
||
187 | public function getEntityFromDatabaseArray(array $data) |
||
196 | } |
||
197 |
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.