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); |
||
13 | class StringConditionTree |
||
14 | { |
||
15 | /** Tree node root element identifier, needed for recursion */ |
||
16 | const ROOT_NAME = ''; |
||
17 | |||
18 | /** Final tree node branch identifier */ |
||
19 | const SELF_NAME = '@self'; |
||
20 | |||
21 | /** String parameter start marker */ |
||
22 | const PARAMETER_START = '{'; |
||
23 | |||
24 | /** String parameter end marker */ |
||
25 | const PARAMETER_END = '}'; |
||
26 | |||
27 | /** Parameter sorting length value for counting */ |
||
28 | const PARAMETER_COF = 2000; |
||
29 | |||
30 | /** @var TreeNode Resulting collection for debugging */ |
||
31 | protected $debug; |
||
32 | |||
33 | /** @var array Collection of input string => identifier */ |
||
34 | protected $source; |
||
35 | |||
36 | /** @var string Parametrized string start marker */ |
||
37 | protected $parameterStartMarker = self::PARAMETER_START; |
||
38 | |||
39 | /** @var string Parametrized string end marker */ |
||
40 | protected $parameterEndMarker = self::PARAMETER_END; |
||
41 | |||
42 | /** |
||
43 | * StringConditionTree constructor. |
||
44 | * |
||
45 | * @param string $parameterStartMarker Parametrized string start marker |
||
46 | * @param string $parameterEndMarker Parametrized string end marker |
||
47 | */ |
||
48 | public function __construct( |
||
56 | |||
57 | /** |
||
58 | * Build similarity strings tree. |
||
59 | * |
||
60 | * @param array $input Collection of strings |
||
61 | * |
||
62 | * @return TreeNode Resulting similarity strings tree |
||
63 | */ |
||
64 | public function process(array $input): TreeNode |
||
78 | |||
79 | /** |
||
80 | * Recursive string similarity tree builder. |
||
81 | * |
||
82 | * @param string $prefix |
||
83 | * @param array $input |
||
84 | * @param TreeNode $result |
||
85 | */ |
||
86 | protected function innerProcessor(string $prefix, array $input, TreeNode $result) |
||
120 | |||
121 | /** |
||
122 | * Get collection of grouped longest matching prefixes with strings sub-array. |
||
123 | * |
||
124 | * @param array $input Input strings array |
||
125 | * |
||
126 | * @return array Longest matching prefixes array |
||
127 | */ |
||
128 | protected function getLMPCollection(array $input): array |
||
153 | |||
154 | /** |
||
155 | * Find longest matching prefix between two strings. |
||
156 | * |
||
157 | * @param string $initialString Initial string |
||
158 | * @param string $comparedString Compared string |
||
159 | * |
||
160 | * @return string Longest matching prefix |
||
161 | */ |
||
162 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
213 | |||
214 | /** |
||
215 | * Add only unique value to array. |
||
216 | * |
||
217 | * @param mixed $value Unique value |
||
218 | * @param array $array Array for adding unique value |
||
219 | * @param bool $strict Strict uniqueness check |
||
220 | * |
||
221 | * @return bool True if unique value was added |
||
222 | * @see in_array(); |
||
223 | * |
||
224 | */ |
||
225 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
242 | |||
243 | /** |
||
244 | * Sort strings array considering PCG and NPCG string structure. |
||
245 | * |
||
246 | * @param array $input Input array for sorting |
||
247 | * |
||
248 | * @return array Sorted array |
||
249 | */ |
||
250 | protected function sortArrayByKeys(array &$input) |
||
263 | |||
264 | /** |
||
265 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
266 | * |
||
267 | * @param array $prefixes LMP collection, returning value |
||
268 | */ |
||
269 | protected function filterLMPStrings(array &$prefixes) |
||
278 | |||
279 | /** |
||
280 | * Find all duplication of source array values in compared array and remove them. |
||
281 | * |
||
282 | * @param array $source Source array |
||
283 | * @param array $compared Compared array for filtering duplicates |
||
284 | */ |
||
285 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
295 | |||
296 | /** |
||
297 | * Remove key string from the beginning of all sub-array strings. |
||
298 | * |
||
299 | * @param array $array Input array of key => [keyStrings...] |
||
300 | * |
||
301 | * @return array Processed array with removed keys from beginning of sub arrays |
||
302 | */ |
||
303 | protected function removeKeyFromArrayStrings(array $array): array |
||
325 | |||
326 | /** |
||
327 | * Build string character group structure considering parametrized |
||
328 | * and not parametrized character groups and their length(PCG, NPCG). |
||
329 | * |
||
330 | * @param string $prefix Prefix string |
||
331 | * |
||
332 | * @return array String character groups structure |
||
333 | */ |
||
334 | protected function getPrefixStructure(string $prefix): array |
||
376 | |||
377 | /** |
||
378 | * Compare string structures. |
||
379 | * |
||
380 | * @param array $initial Initial string structure |
||
381 | * @param array $compared Compared string structure |
||
382 | * |
||
383 | * @return int Result of array elements comparison |
||
384 | */ |
||
385 | protected function compareStringStructure(array $initial, array $compared): int |
||
460 | } |
||
461 |