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 declare(strict_types=1); |
||
16 | final class Collection extends MongoCollection |
||
17 | { |
||
18 | /** @var EventDispatcherInterface */ |
||
19 | private $eventDispatcher; |
||
20 | /** @var string */ |
||
21 | private $clientName; |
||
22 | /** @var string */ |
||
23 | private $databaseName; |
||
|
|||
24 | |||
25 | /** |
||
26 | * Collection constructor. |
||
27 | * |
||
28 | * @param Manager $manager |
||
29 | * @param string $clientName |
||
30 | * @param string $databaseName |
||
31 | * @param string $collectionName |
||
32 | * @param array $options |
||
33 | * @param EventDispatcherInterface $eventDispatcher |
||
34 | * |
||
35 | * @internal param DataCollectorLoggerInterface $logger |
||
36 | */ |
||
37 | 15 | View Code Duplication | public function __construct( |
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 1 | public function aggregate(array $pipeline, array $options = []) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 1 | View Code Duplication | public function count($filter = [], array $options = []) |
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 1 | View Code Duplication | public function find($filter = [], array $options = []) |
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 1 | View Code Duplication | public function findOne($filter = [], array $options = []) |
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 1 | View Code Duplication | public function findOneAndUpdate($filter, $update, array $options = []) |
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 1 | public function findOneAndDelete($filter, array $options = []) |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 2 | public function deleteMany($filter, array $options = []) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1 | public function deleteOne($filter, array $options = []) |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 1 | View Code Duplication | public function replaceOne($filter, $replacement, array $options = []) |
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | 2 | View Code Duplication | public function insertOne($document, array $options = []) |
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 1 | View Code Duplication | public function updateOne($filter, $update, array $options = []) |
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 1 | View Code Duplication | public function distinct($fieldName, $filter = [], array $options = []) |
194 | |||
195 | /** |
||
196 | * @param string $method |
||
197 | * @param array|object $filters |
||
198 | * @param array|object $data |
||
199 | * @param array $options |
||
200 | * |
||
201 | * @return Query |
||
202 | */ |
||
203 | 12 | private function prepareQuery(string $method, $filters = null, $data = null, array $options): Query |
|
221 | |||
222 | /** |
||
223 | * @param ReadPreference $readPreference |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | 12 | private function translateReadPreference(ReadPreference $readPreference): string |
|
228 | { |
||
229 | 12 | switch ($readPreference->getMode()) { |
|
230 | 12 | case ReadPreference::RP_PRIMARY: |
|
231 | 12 | return 'primary'; |
|
232 | case ReadPreference::RP_PRIMARY_PREFERRED: |
||
233 | return 'primaryPreferred'; |
||
234 | case ReadPreference::RP_SECONDARY: |
||
235 | return 'secondary'; |
||
236 | case ReadPreference::RP_SECONDARY_PREFERRED: |
||
237 | return 'secondaryPreferred'; |
||
238 | case ReadPreference::RP_NEAREST: |
||
239 | return 'nearest'; |
||
240 | default: |
||
241 | return 'undefined'; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param Query $queryLog |
||
247 | */ |
||
248 | 12 | private function notifyQueryExecution(Query $queryLog) |
|
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | 12 | public function getClientName(): string |
|
262 | |||
263 | /** |
||
264 | * @return string |
||
265 | */ |
||
266 | 12 | public function getDatabaseName(): string |
|
270 | } |
||
271 | |||
272 |