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:
Complex classes like StringConditionTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StringConditionTree, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
14 | class StringConditionTree |
||
15 | { |
||
16 | /** Tree node root element identifier, needed for recursion */ |
||
17 | const ROOT_NAME = ''; |
||
18 | |||
19 | /** Final tree node branch identifier */ |
||
20 | const SELF_NAME = '@self'; |
||
21 | |||
22 | /** String parameter start marker */ |
||
23 | const PARAMETER_START = '{'; |
||
24 | |||
25 | /** String parameter end marker */ |
||
26 | const PARAMETER_END = '}'; |
||
27 | |||
28 | /** Parameter sorting length value for counting */ |
||
29 | const PARAMETER_COF = 2000; |
||
30 | |||
31 | /** @var TreeNode Resulting collection for debugging */ |
||
32 | protected $debug; |
||
33 | |||
34 | /** @var string Parametrized string start marker */ |
||
35 | protected $parameterStartMarker = self::PARAMETER_START; |
||
36 | |||
37 | /** @var string Parametrized string end marker */ |
||
38 | protected $parameterEndMarker = self::PARAMETER_END; |
||
39 | |||
40 | /** |
||
41 | * StringConditionTree constructor. |
||
42 | * |
||
43 | * @param string $parameterStartMarker Parametrized string start marker |
||
44 | * @param string $parameterEndMarker Parametrized string end marker |
||
45 | */ |
||
46 | public function __construct(string $parameterStartMarker = self::PARAMETER_START, string $parameterEndMarker = self::PARAMETER_END) |
||
51 | |||
52 | /** |
||
53 | * Build similarity strings tree. |
||
54 | * |
||
55 | * @param array $input Collection of strings |
||
56 | * |
||
57 | * @return TreeNode Resulting similarity strings tree |
||
58 | */ |
||
59 | public function process(array $input): TreeNode |
||
71 | |||
72 | /** |
||
73 | * Buil string character group structure considering parametrized |
||
74 | * and not parametrized characted groups and their length(PCG, NPCG). |
||
75 | * |
||
76 | * @param string $prefix Prefix string |
||
77 | * |
||
78 | * @return array String character groups structure |
||
79 | */ |
||
80 | protected function getPrefixStructure(string $prefix): array |
||
122 | |||
123 | /** |
||
124 | * Compare string structures. |
||
125 | * |
||
126 | * @param array $initial Initial string structure |
||
127 | * @param array $compared Compared string structure |
||
128 | * |
||
129 | * @return int Result of array elements comparison |
||
130 | */ |
||
131 | protected function compareStringStructure(array $initial, array $compared): int |
||
206 | |||
207 | /** |
||
208 | * Sort strings array considering PCG and NPCG string structure. |
||
209 | * |
||
210 | * @param array $input Input array for sorting |
||
211 | * @return array Sorted array |
||
212 | */ |
||
213 | protected function sortArrayByKeys(array &$input) |
||
226 | |||
227 | /** |
||
228 | * Add only unique value to array. |
||
229 | * |
||
230 | * @param mixed $value Unique value |
||
231 | * @param array $array Array for adding unique value |
||
232 | * @param bool $strict Strict uniqueness check |
||
233 | * |
||
234 | * @see in_array(); |
||
235 | * |
||
236 | * @return bool True if unique value was added |
||
237 | */ |
||
238 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
254 | |||
255 | /** |
||
256 | * Find longest matching prefix between two strings. |
||
257 | * |
||
258 | * @param string $initialString Initial string |
||
259 | * @param string $comparedString Compared string |
||
260 | * |
||
261 | * @return string Longest matching prefix |
||
262 | */ |
||
263 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
317 | |||
318 | /** |
||
319 | * Remove key string from the beginning of all sub-array strings. |
||
320 | * |
||
321 | * @param array $array Input array of key => [keyStrings...] |
||
322 | * |
||
323 | * @param string $selfMarker Marker for storing self pointer |
||
324 | * |
||
325 | * @return array Processed array with removed keys from beginning of sub arrays |
||
326 | */ |
||
327 | protected function removeKeyFromArrayStrings(array $array, string $selfMarker): array |
||
346 | |||
347 | /** |
||
348 | * Find all duplication of source array values in compared array and remove them. |
||
349 | * |
||
350 | * @param array $source Source array |
||
351 | * @param array $compared Compared array for filtering duplicates |
||
352 | */ |
||
353 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
363 | |||
364 | /** |
||
365 | * Analyze strings array and search for missing strings in compared array sub arrays |
||
366 | * and add them as compared keys. |
||
367 | * |
||
368 | * @param array $input Input array of strings |
||
369 | * @param array $compare Compared array of strings sub-arrays |
||
370 | * @param string $selfMarker Self array key marker |
||
371 | * |
||
372 | * @return array Compared array with missing strings from input as keys => $selfMarker |
||
373 | */ |
||
374 | protected function addMissingStringsAsLMP(array $input, array $compare, string $selfMarker): array |
||
395 | |||
396 | /** |
||
397 | * Recursive string similarity tree builder. |
||
398 | * |
||
399 | * @param string $prefix |
||
400 | * @param array $input |
||
401 | * @param TreeNode $result |
||
402 | * @param string $selfMarker |
||
403 | */ |
||
404 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
474 | } |
||
475 |