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 |
||
15 | final class Collection extends MongoCollection |
||
16 | { |
||
17 | /** |
||
18 | * @var DataCollectorLoggerInterface |
||
19 | */ |
||
20 | private $logger; |
||
21 | |||
22 | /** |
||
23 | * Collection constructor. |
||
24 | * |
||
25 | * @param Manager $manager |
||
26 | * @param string $databaseName |
||
27 | * @param string $collectionName |
||
28 | * @param array $options |
||
29 | * @param DataCollectorLoggerInterface $logger |
||
30 | */ |
||
31 | 8 | public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger) |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 1 | public function find($filter = [], array $options = []) |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 1 | View Code Duplication | public function findOne($filter = [], array $options = []) |
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 1 | View Code Duplication | public function findOneAndUpdate($filter, $update, array $options = []) |
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 1 | View Code Duplication | public function findOneAndDelete($filter, array $options = []) |
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 1 | View Code Duplication | public function insertOne($document, array $options = []) |
96 | |||
97 | /** |
||
98 | * @param $filter |
||
99 | * @param string $method |
||
100 | * |
||
101 | * @return LogEvent |
||
102 | */ |
||
103 | 5 | private function startQueryLogging($filter, string $method): LogEvent |
|
116 | } |
||
117 | |||
118 |
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.