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); |
||
13 | class StructureSorter |
||
14 | { |
||
15 | /** Variable length characters group */ |
||
16 | const G_VARIABLE = 0; |
||
17 | |||
18 | /** Fixed length characters group */ |
||
19 | const G_FIXED = 1; |
||
20 | |||
21 | /** @var string Parametrized string start marker */ |
||
22 | protected $parameterStartMarker; |
||
23 | |||
24 | /** @var string Parametrized string end marker */ |
||
25 | protected $parameterEndMarker; |
||
26 | |||
27 | /** |
||
28 | * StructureSorter constructor. |
||
29 | * |
||
30 | * @param string $parameterStartMarker Parametrized string start marker |
||
31 | * @param string $parameterEndMarker Parametrized string end marker |
||
32 | */ |
||
33 | public function __construct(string $parameterStartMarker, string $parameterEndMarker) |
||
38 | |||
39 | /** |
||
40 | * Sort strings array considering PCG and NPCG string structure. |
||
41 | * |
||
42 | * @param array $input Input array for sorting |
||
43 | * |
||
44 | * @return array Sorted keys array |
||
45 | */ |
||
46 | public function sortArrayByKeys(array $input): array |
||
61 | |||
62 | /** |
||
63 | * Build string character group structure considering parametrized |
||
64 | * and not parametrized character groups and their length(PCG, NPCG). |
||
65 | * |
||
66 | * @param string $prefix Prefix string |
||
67 | * |
||
68 | * @return array String character groups structure |
||
69 | */ |
||
70 | protected function getPrefixStructure(string $prefix): array |
||
112 | |||
113 | /** |
||
114 | * Compare string structures. |
||
115 | * |
||
116 | * @param array $initial Initial string structure |
||
117 | * @param array $compared Compared string structure |
||
118 | * |
||
119 | * @return int Result of array elements comparison |
||
120 | */ |
||
121 | protected function compareStringStructure(array $initial, array $compared): int |
||
162 | |||
163 | /** |
||
164 | * Make CGS equals size. |
||
165 | * |
||
166 | * @param array $initial Initial CGS, will be changed |
||
167 | * @param array $compared Compared CGS, will be changed |
||
168 | * |
||
169 | * @return int Longest CGS size(now they are both equal) |
||
170 | */ |
||
171 | protected function equalizeStructures(array &$initial, array &$compared): int |
||
183 | |||
184 | /** |
||
185 | * Fill CSG with previous group value if not present. |
||
186 | * |
||
187 | * @param array $groups CSG for filling |
||
188 | * @param int $index CSG index |
||
189 | */ |
||
190 | private function fillMissingStructureGroup(array &$groups, int $index) |
||
196 | |||
197 | /** |
||
198 | * Compare two character group structure(CGS) length and define |
||
199 | * which one is longer. |
||
200 | * |
||
201 | * @param array $initial Initial CGS |
||
202 | * @param array $compared Compared CGS |
||
203 | * @param int $type CGS type (Variable|Fixed length) |
||
204 | * |
||
205 | * @return int -1 if initial CGS longer |
||
206 | * 0 if initial and compared CGS are equal |
||
207 | * 1 if compared CGS longer |
||
208 | */ |
||
209 | protected function compareStructureLengths(array $initial, array $compared, int $type = self::G_FIXED): int |
||
230 | |||
231 | /** |
||
232 | * Compare longer CGS considering that: |
||
233 | * - Shortest fixed CGS should have higher priority |
||
234 | * - Longest variable CGS should have higher priority |
||
235 | * |
||
236 | * @param array $initialGroup Initial CGS |
||
237 | * @param array $comparedGroup Compared CGS |
||
238 | * @param int $type Fixed/Variable CGS |
||
239 | * |
||
240 | * @return int 0 if initial CGS is not longer than compared, |
||
241 | * otherwise -1/1 depending on CGS type. |
||
242 | */ |
||
243 | private function compareLength(array $initialGroup, array $comparedGroup, int $type) |
||
257 | } |
||
258 |
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.