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 |
||
17 | abstract class AbstractResultsIterator implements \Countable, \Iterator |
||
18 | { |
||
19 | private $count = 0; |
||
20 | private $raw; |
||
21 | private $scrollId; |
||
22 | private $scrollDuration; |
||
23 | |||
24 | protected $documents = []; |
||
25 | private $aggregations = []; |
||
26 | |||
27 | private $converter; |
||
28 | private $index; |
||
29 | |||
30 | //Used to count scroll iteration. |
||
31 | private $key = 0; |
||
32 | |||
33 | public function __construct( |
||
59 | |||
60 | public function __destruct() |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getRaw() |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getAggregations() |
||
83 | |||
84 | /** |
||
85 | * Returns specific aggregation by name. |
||
86 | * |
||
87 | * @param string $name |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getAggregation($name) |
||
98 | |||
99 | /** |
||
100 | * Returns total count of documents. |
||
101 | * |
||
102 | * @return int |
||
103 | */ |
||
104 | public function count() |
||
108 | |||
109 | /** |
||
110 | * Return the current element. |
||
111 | */ |
||
112 | public function current() |
||
116 | |||
117 | /** |
||
118 | * Move forward to the next element. |
||
119 | */ |
||
120 | public function next(): self |
||
124 | |||
125 | /** |
||
126 | * Return the key of the current element. |
||
127 | */ |
||
128 | public function key(): int |
||
132 | |||
133 | /** |
||
134 | * Checks if current position is valid. |
||
135 | */ |
||
136 | public function valid(): bool |
||
151 | |||
152 | /** |
||
153 | * Rewind the Iterator to the first element. |
||
154 | */ |
||
155 | public function rewind(): void |
||
159 | |||
160 | public function isScrollable(): bool |
||
164 | |||
165 | protected function getConverter(): Converter |
||
169 | |||
170 | public function getIndex(): IndexService |
||
174 | |||
175 | /** |
||
176 | * Gets document array from the container. |
||
177 | */ |
||
178 | protected function getDocument(int $key) |
||
186 | |||
187 | /** |
||
188 | * Checks if a document exists in the local cache container. |
||
189 | */ |
||
190 | protected function documentExists(int $key): bool |
||
194 | |||
195 | /** |
||
196 | * Advances key. |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | protected function advanceKey(): self |
||
210 | |||
211 | /** |
||
212 | * Rewind's the iteration and returns first result. |
||
213 | */ |
||
214 | public function first() |
||
220 | |||
221 | protected function page(): self |
||
240 | |||
241 | /** |
||
242 | * Returns score of current hit. |
||
243 | */ |
||
244 | View Code Duplication | public function getDocumentScore(): int |
|
256 | |||
257 | /** |
||
258 | * Returns sort of current hit. |
||
259 | */ |
||
260 | View Code Duplication | public function getDocumentSort() |
|
272 | |||
273 | /** |
||
274 | * Convert`s raw array to a document object or a normalized array, depends on the iterator type. |
||
275 | */ |
||
276 | abstract protected function convertDocument(array $raw); |
||
277 | } |
||
278 |
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.