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 | * Prefix length counter for array sorting callback to sort by prefix length and put |
||
74 | * parametrized prefixed at the end. |
||
75 | * |
||
76 | * @param string $prefix Prefix string |
||
77 | * |
||
78 | * @return int Prefix length |
||
79 | */ |
||
80 | protected function prefixLength(string $prefix): int |
||
123 | |||
124 | /** |
||
125 | * Sort array by key string lengths. |
||
126 | * |
||
127 | * @param array $input Input array for sorting |
||
128 | * @param int $order Sorting order |
||
129 | */ |
||
130 | protected function sortArrayByKeys(array &$input, int $order = SORT_ASC) |
||
134 | |||
135 | /** |
||
136 | * Add only unique value to array. |
||
137 | * |
||
138 | * @param mixed $value Unique value |
||
139 | * @param array $array Array for adding unique value |
||
140 | * @param bool $strict Strict uniqueness check |
||
141 | * |
||
142 | * @see in_array(); |
||
143 | * |
||
144 | * @return bool True if unique value was added |
||
145 | */ |
||
146 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
162 | |||
163 | /** |
||
164 | * Find longest matching prefix between two strings. |
||
165 | * |
||
166 | * @param string $initialString Initial string |
||
167 | * @param string $comparedString Compared string |
||
168 | * |
||
169 | * @return string Longest matching prefix |
||
170 | */ |
||
171 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
225 | |||
226 | /** |
||
227 | * Remove key string from the beginning of all sub-array strings. |
||
228 | * |
||
229 | * @param array $array Input array of key => [keyStrings...] |
||
230 | * |
||
231 | * @param string $selfMarker Marker for storing self pointer |
||
232 | * |
||
233 | * @return array Processed array with removed keys from beginning of sub arrays |
||
234 | */ |
||
235 | protected function removeKeyFromArrayStrings(array $array, string $selfMarker): array |
||
254 | |||
255 | /** |
||
256 | * Find all duplication of source array values in compared array and remove them. |
||
257 | * |
||
258 | * @param array $source Source array |
||
259 | * @param array $compared Compared array for filtering duplicates |
||
260 | */ |
||
261 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
271 | |||
272 | /** |
||
273 | * Recursive string similarity tree builder. |
||
274 | * |
||
275 | * @param string $prefix |
||
276 | * @param array $input |
||
277 | * @param TreeNode $result |
||
278 | * @param string $selfMarker |
||
279 | */ |
||
280 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
363 | } |
||
364 |