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 |
||
161 | |||
162 | /** |
||
163 | * Make CGS equals size. |
||
164 | * |
||
165 | * @param array $initial Initial CGS, will be changed |
||
166 | * @param array $compared Compared CGS, will be changed |
||
167 | * |
||
168 | * @return int Longest CGS size(now they are both equal) |
||
169 | */ |
||
170 | protected function equalizeStructures(array &$initial, array &$compared): int |
||
182 | |||
183 | /** |
||
184 | * Fill CSG with previous group value if not present. |
||
185 | * |
||
186 | * @param array $groups CSG for filling |
||
187 | * @param int $index CSG index |
||
188 | */ |
||
189 | private function fillMissingStructureGroup(array &$groups, int $index) |
||
195 | |||
196 | /** |
||
197 | * Compare longer CGS considering that: |
||
198 | * - Shortest fixed CGS should have higher priority |
||
199 | * - Longest variable CGS should have higher priority |
||
200 | * |
||
201 | * @param array $initialGroup Initial CGS |
||
202 | * @param array $comparedGroup Compared CGS |
||
203 | * @param int $type Fixed/Variable CGS |
||
204 | * @param int $dataIndex CSG data index |
||
205 | * |
||
206 | * @return int 0 if initial CGS is not longer than compared, |
||
207 | * otherwise -1/1 depending on CGS type. |
||
208 | */ |
||
209 | private function compareCSGData(array $initialGroup, array $comparedGroup, int $type, int $dataIndex = 1) |
||
223 | |||
224 | /** |
||
225 | * Compare two character group structure(CGS) length and define |
||
226 | * which one is longer. |
||
227 | * |
||
228 | * @param array $initial Initial CGS |
||
229 | * @param array $compared Compared CGS |
||
230 | * @param int $type CGS type (Variable|Fixed length) |
||
231 | * |
||
232 | * @return int -1 if initial CGS longer |
||
233 | * 0 if initial and compared CGS are equal |
||
234 | * 1 if compared CGS longer |
||
235 | */ |
||
236 | protected function compareStructureLengths(array $initial, array $compared, int $type = self::G_FIXED): int |
||
257 | } |
||
258 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.