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 |
||
14 | class LanguageRepository |
||
15 | { |
||
16 | /** |
||
17 | * Database table name that this repository maintains. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | const TABLE = 'languages'; |
||
22 | |||
23 | /** |
||
24 | * @var Connection |
||
25 | */ |
||
26 | private $connection; |
||
27 | |||
28 | /** |
||
29 | * LanguageRepository constructor. |
||
30 | * |
||
31 | * @param Connection $connection |
||
32 | */ |
||
33 | public function __construct(Connection $connection) |
||
37 | |||
38 | /** |
||
39 | * Fetches all languages. |
||
40 | * |
||
41 | * @return LanguageEntity[] |
||
42 | */ |
||
43 | public function fetchAll() |
||
44 | { |
||
45 | $statement = $this->connection->createQueryBuilder() |
||
46 | ->select('*') |
||
47 | ->from(self::TABLE) |
||
48 | ->execute(); |
||
49 | |||
50 | $result = $statement->fetchAll(); |
||
51 | |||
52 | if ($result === false) { |
||
53 | return []; |
||
54 | } |
||
55 | |||
56 | return $this->getEntityArrayFromDatabaseArray($result); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Fetches all translated languages. |
||
61 | * |
||
62 | * @return LanguageEntity[] |
||
63 | */ |
||
64 | View Code Duplication | public function fetchAllTranslated() |
|
80 | |||
81 | /** |
||
82 | * Creates a language in the database. |
||
83 | * |
||
84 | * @param LanguageEntity $entity |
||
85 | * |
||
86 | * @return LanguageEntity |
||
87 | * |
||
88 | * @throws RecordAlreadyExistsException |
||
89 | */ |
||
90 | public function create(LanguageEntity $entity) |
||
107 | |||
108 | /** |
||
109 | * Update a language in the database. |
||
110 | * |
||
111 | * @param LanguageEntity $entity |
||
112 | * |
||
113 | * @return LanguageEntity |
||
114 | * |
||
115 | * @throws RecordNotPersistedException |
||
116 | */ |
||
117 | View Code Duplication | public function update(LanguageEntity $entity) |
|
135 | |||
136 | /** |
||
137 | * Removes a language from the database. |
||
138 | * |
||
139 | * @param LanguageEntity $entity |
||
140 | * |
||
141 | * @return LanguageEntity |
||
142 | * |
||
143 | * @throws RecordNotPersistedException |
||
144 | */ |
||
145 | View Code Duplication | public function remove(LanguageEntity $entity) |
|
163 | |||
164 | /** |
||
165 | * Converts database array to entity array. |
||
166 | * |
||
167 | * @param array $result |
||
168 | * |
||
169 | * @return LanguageEntity[] |
||
170 | */ |
||
171 | private function getEntityArrayFromDatabaseArray(array $result) |
||
181 | |||
182 | /** |
||
183 | * Maps the given entity to the database array. |
||
184 | * |
||
185 | * @param LanguageEntity $entity |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | View Code Duplication | public function getDatabaseArrayFromEntity(LanguageEntity $entity) |
|
203 | |||
204 | /** |
||
205 | * Prepares database array from properties. |
||
206 | * |
||
207 | * @param array $data |
||
208 | * |
||
209 | * @return LanguageEntity |
||
210 | */ |
||
211 | View Code Duplication | public function getEntityFromDatabaseArray(array $data) |
|
226 | } |
||
227 |
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.