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