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); |
||
| 15 | class StructureSorter |
||
| 16 | { |
||
| 17 | /** Variable length characters group */ |
||
| 18 | const G_VARIABLE = 0; |
||
| 19 | |||
| 20 | /** Fixed length characters group */ |
||
| 21 | const G_FIXED = 1; |
||
| 22 | |||
| 23 | /** @var string Parametrized string start marker */ |
||
| 24 | protected $parameterStartMarker; |
||
| 25 | |||
| 26 | /** @var string Parametrized string end marker */ |
||
| 27 | protected $parameterEndMarker; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * StructureSorter constructor. |
||
| 31 | * |
||
| 32 | * @param string $parameterStartMarker Parametrized string start marker |
||
| 33 | * @param string $parameterEndMarker Parametrized string end marker |
||
| 34 | */ |
||
| 35 | 1 | public function __construct(string $parameterStartMarker, string $parameterEndMarker) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Sort strings array considering PCG and NPCG string structure. |
||
| 43 | * |
||
| 44 | * @param array $input Input array for sorting |
||
| 45 | * |
||
| 46 | * @return array Sorted keys array |
||
| 47 | */ |
||
| 48 | 1 | public function sortArrayByKeys(array $input): array |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Build string character group structure considering parametrized |
||
| 86 | * and not parametrized character groups and their length(PCG, NPCG). |
||
| 87 | * |
||
| 88 | * @param string $prefix Prefix string |
||
| 89 | * |
||
| 90 | * @return array String character groups structure |
||
| 91 | */ |
||
| 92 | protected function getPrefixStructure(string $prefix): array |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Compare string structures. |
||
| 137 | * |
||
| 138 | * @param array $initial Initial string structure |
||
| 139 | * @param array $compared Compared string structure |
||
| 140 | * |
||
| 141 | * @return int Result of array elements comparison |
||
| 142 | */ |
||
| 143 | protected function compareStringStructure(array $initial, array $compared): int |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Make CGS equals size. |
||
| 192 | * |
||
| 193 | * @param array $initial Initial CGS, will be changed |
||
| 194 | * @param array $compared Compared CGS, will be changed |
||
| 195 | * |
||
| 196 | * @return int Longest CGS size(now they are both equal) |
||
| 197 | */ |
||
| 198 | protected function equalizeStructures(array &$initial, array &$compared): int |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Fill CSG with previous group value if not present. |
||
| 213 | * |
||
| 214 | * @param array $groups CSG for filling |
||
| 215 | * @param int $index CSG index |
||
| 216 | */ |
||
| 217 | private function fillMissingStructureGroup(array &$groups, int $index) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Compare longer CGS considering that: |
||
| 226 | * - Shortest fixed CGS should have higher priority |
||
| 227 | * - Longest variable CGS should have higher priority |
||
| 228 | * |
||
| 229 | * @param array $initialGroup Initial CGS |
||
| 230 | * @param array $comparedGroup Compared CGS |
||
| 231 | * @param int $type Fixed/Variable CGS |
||
| 232 | * @param int $dataIndex CSG creationData index |
||
| 233 | * |
||
| 234 | * @return int 0 if initial CGS is not longer than compared, |
||
| 235 | * otherwise -1/1 depending on CGS type. |
||
| 236 | */ |
||
| 237 | private function compareCSGData(array $initialGroup, array $comparedGroup, int $type, int $dataIndex = 1): int |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Compare two character group structure(CGS) length and define |
||
| 254 | * which one is longer. |
||
| 255 | * |
||
| 256 | * @param array $initial Initial CGS |
||
| 257 | * @param array $compared Compared CGS |
||
| 258 | * @param int $type CGS type (Variable|Fixed length) |
||
| 259 | * |
||
| 260 | * @return int -1 if initial CGS longer |
||
| 261 | * 0 if initial and compared CGS are equal |
||
| 262 | * 1 if compared CGS longer |
||
| 263 | */ |
||
| 264 | protected function compareStructureLengths(array $initial, array $compared, int $type = self::G_FIXED): int |
||
| 285 | } |
||
| 286 |