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