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 |
||
11 | class Collection |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $name; |
||
17 | |||
18 | /** |
||
19 | * @var DocumentStore |
||
20 | */ |
||
21 | protected $store; |
||
22 | |||
23 | /** |
||
24 | * @var Database |
||
25 | */ |
||
26 | protected $database; |
||
27 | |||
28 | /** |
||
29 | * @var Parser |
||
30 | */ |
||
31 | protected $parser; |
||
32 | |||
33 | /** |
||
34 | * Construct a new collection. |
||
35 | * |
||
36 | * @param Database $database |
||
37 | * @param DocumentStore $store |
||
38 | * @param string $name |
||
39 | */ |
||
40 | 7 | public function __construct(Database $database, DocumentStore $store, $name) |
|
47 | |||
48 | /** |
||
49 | * Drop the collection. |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 1 | public function drop() |
|
57 | |||
58 | /** |
||
59 | * Remove all the documents from the collection. |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | 1 | public function truncate() |
|
67 | |||
68 | /** |
||
69 | * Insert a new document. |
||
70 | * |
||
71 | * @param DocumentInterface $document |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 1 | public function insert(DocumentInterface $document) |
|
79 | |||
80 | /** |
||
81 | * Update existing documents. |
||
82 | * |
||
83 | * @param array $criteria |
||
84 | * @param DocumentInterface $updated |
||
85 | * @param bool $multiple |
||
86 | * |
||
87 | * @return int The count of the documents updated. |
||
88 | */ |
||
89 | View Code Duplication | public function update($criteria, DocumentInterface $updated, $multiple = false) |
|
103 | |||
104 | /** |
||
105 | * Remove documents from the collection. |
||
106 | * |
||
107 | * @param mixed $criteria |
||
108 | * @param bool $multiple |
||
109 | * |
||
110 | * @return int The count of the document deleted. |
||
111 | */ |
||
112 | 1 | View Code Duplication | public function remove($criteria, $multiple = false) |
126 | |||
127 | /** |
||
128 | * Find documents in the collection. |
||
129 | * |
||
130 | * @param array $criteria |
||
131 | * |
||
132 | * @return array The array of results. |
||
133 | */ |
||
134 | 2 | public function find($criteria = []) |
|
138 | |||
139 | /** |
||
140 | * Count documents in the collection. |
||
141 | * |
||
142 | * @param array $criteria |
||
143 | * |
||
144 | * @return int The number of documents. |
||
145 | */ |
||
146 | public function count($criteria = []) |
||
154 | |||
155 | 3 | protected function onMatch($criteria, $limit = null) |
|
164 | |||
165 | 3 | protected function newMatcher(ExpressionInterface $expression) |
|
169 | } |
||
170 |
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.