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); |
||
18 | class StringConditionTree |
||
19 | { |
||
20 | /** Tree node root element identifier, needed for recursion */ |
||
21 | const ROOT_NAME = ''; |
||
22 | |||
23 | /** Final tree node branch identifier */ |
||
24 | const SELF_NAME = '@self'; |
||
25 | |||
26 | /** String parameter start marker */ |
||
27 | const PARAMETER_START = '{'; |
||
28 | |||
29 | /** String parameter end marker */ |
||
30 | const PARAMETER_END = '}'; |
||
31 | |||
32 | /** Parameter sorting length value for counting */ |
||
33 | const PARAMETER_COF = 2000; |
||
34 | |||
35 | /** @var TreeNode Resulting collection for debugging */ |
||
36 | protected $debug; |
||
37 | |||
38 | /** @var array Collection of input string => identifier */ |
||
39 | protected $source; |
||
40 | |||
41 | /** @var string Parametrized string start marker */ |
||
42 | protected $parameterStartMarker = self::PARAMETER_START; |
||
43 | |||
44 | /** @var string Parametrized string end marker */ |
||
45 | protected $parameterEndMarker = self::PARAMETER_END; |
||
46 | |||
47 | /** |
||
48 | * StringConditionTree constructor. |
||
49 | * |
||
50 | * @param string $parameterStartMarker Parametrized string start marker |
||
51 | * @param string $parameterEndMarker Parametrized string end marker |
||
52 | */ |
||
53 | 1 | public function __construct( |
|
60 | |||
61 | /** |
||
62 | * Build similarity strings tree. |
||
63 | * |
||
64 | * @param array $input Collection of strings |
||
65 | * |
||
66 | * @return TreeNode Resulting similarity strings tree |
||
67 | */ |
||
68 | 1 | public function process(array $input): TreeNode |
|
80 | |||
81 | /** |
||
82 | * Recursive string similarity tree builder. |
||
83 | * |
||
84 | * @param string $prefix |
||
85 | * @param array $input |
||
86 | * @param TreeNode $result |
||
87 | */ |
||
88 | 1 | protected function innerProcessor(string $prefix, array $input, TreeNode $result) |
|
121 | |||
122 | /** |
||
123 | * Get collection of grouped longest matching prefixes with strings sub-array. |
||
124 | * |
||
125 | * @param array $input Input strings array |
||
126 | * |
||
127 | * @return array Longest matching prefixes array |
||
128 | */ |
||
129 | 1 | protected function getLMPCollection(array $input): array |
|
173 | |||
174 | /** |
||
175 | * Find longest matching prefix between two strings. |
||
176 | * |
||
177 | * @param string $initialString Initial string |
||
178 | * @param string $comparedString Compared string |
||
179 | * |
||
180 | * TODO: Refactor this method |
||
181 | * |
||
182 | * @return string Longest matching prefix |
||
183 | */ |
||
184 | 1 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
|
235 | |||
236 | /** |
||
237 | * Add only unique value to array. |
||
238 | * |
||
239 | * @param mixed $value Unique value |
||
240 | * @param array $array Array for adding unique value |
||
241 | * @param bool $strict Strict uniqueness check |
||
242 | * |
||
243 | * @return bool True if unique value was added |
||
244 | * @see in_array(); |
||
245 | * |
||
246 | */ |
||
247 | 1 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
|
264 | |||
265 | /** |
||
266 | * Sort strings array considering PCG and NPCG string structure. |
||
267 | * |
||
268 | * @param array $input Input array for sorting |
||
269 | * |
||
270 | * @return array Sorted keys array |
||
271 | */ |
||
272 | 1 | public function sortArrayByKeys(array $input): array |
|
294 | |||
295 | /** |
||
296 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
297 | * |
||
298 | * @param array $prefixes LMP collection, returning value |
||
299 | */ |
||
300 | 1 | protected function filterLMPStrings(array &$prefixes) |
|
309 | |||
310 | /** |
||
311 | * Find all duplication of source array values in compared array and remove them. |
||
312 | * |
||
313 | * @param array $source Source array |
||
314 | * @param array $compared Compared array for filtering duplicates |
||
315 | */ |
||
316 | 1 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
|
326 | |||
327 | /** |
||
328 | * Remove key string from the beginning of all sub-array strings. |
||
329 | * |
||
330 | * @param array $array Input array of key => [keyStrings...] |
||
331 | * |
||
332 | * @return array Processed array with removed keys from beginning of sub arrays |
||
333 | */ |
||
334 | 1 | protected function removeKeyFromArrayStrings(array $array): array |
|
356 | } |
||
357 |