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 | /** @var TreeNode Resulting collection for debugging */ |
||
29 | protected $debug; |
||
30 | |||
31 | /** @var string Parametrized string start marker */ |
||
32 | protected $parameterStartMarker = self::PARAMETER_START; |
||
33 | |||
34 | /** @var string Parametrized string end marker */ |
||
35 | protected $parameterEndMarker = self::PARAMETER_END; |
||
36 | |||
37 | /** |
||
38 | * StringConditionTree constructor. |
||
39 | * |
||
40 | * @param string $parameterStartMarker Parametrized string start marker |
||
41 | * @param string $parameterEndMarker Parametrized string end marker |
||
42 | */ |
||
43 | public function __construct(string $parameterStartMarker = self::PARAMETER_START, string $parameterEndMarker = self::PARAMETER_END) |
||
48 | |||
49 | /** |
||
50 | * Build similarity strings tree. |
||
51 | * |
||
52 | * @param array $input Collection of strings |
||
53 | * |
||
54 | * @return TreeNode Resulting similarity strings tree |
||
55 | */ |
||
56 | public function process(array $input): TreeNode |
||
68 | |||
69 | /** |
||
70 | * Prefix length counter for array sorting callback to sort by prefix length and put |
||
71 | * parametrized prefixed at the end. |
||
72 | * |
||
73 | * @param string $prefix Prefix string |
||
74 | * |
||
75 | * @return int Prefix length |
||
76 | */ |
||
77 | protected function prefixLength(string $prefix): int |
||
81 | |||
82 | /** |
||
83 | * Sort array by key string lengths. |
||
84 | * |
||
85 | * @param array $input Input array for sorting |
||
86 | * @param int $order Sorting order |
||
87 | */ |
||
88 | protected function sortArrayByKeys(array &$input, int $order = SORT_ASC) |
||
92 | |||
93 | /** |
||
94 | * Add only unique value to array. |
||
95 | * |
||
96 | * @param mixed $value Unique value |
||
97 | * @param array $array Array for adding unique value |
||
98 | * @param bool $strict Strict uniqueness check |
||
99 | * |
||
100 | * @see in_array(); |
||
101 | * |
||
102 | * @return bool True if unique value was added |
||
103 | */ |
||
104 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
120 | |||
121 | /** |
||
122 | * Find longest matching prefix between two strings. |
||
123 | * |
||
124 | * @param string $initialString Initial string |
||
125 | * @param string $comparedString Compared string |
||
126 | * |
||
127 | * @return string Longest matching prefix |
||
128 | */ |
||
129 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
183 | |||
184 | /** |
||
185 | * Remove key string from the beginning of all sub-array strings. |
||
186 | * |
||
187 | * @param array $array Input array of key => [keyStrings...] |
||
188 | * |
||
189 | * @param string $selfMarker Marker for storing self pointer |
||
190 | * |
||
191 | * @return array Processed array with removed keys from beginning of sub arrays |
||
192 | */ |
||
193 | protected function removeKeyFromArrayStrings(array $array, string $selfMarker): array |
||
212 | |||
213 | /** |
||
214 | * Find all duplication of source array values in compared array and remove them. |
||
215 | * |
||
216 | * @param array $source Source array |
||
217 | * @param array $compared Compared array for filtering duplicates |
||
218 | */ |
||
219 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
229 | |||
230 | /** |
||
231 | * Recursive string similarity tree builder. |
||
232 | * |
||
233 | * @param string $prefix |
||
234 | * @param array $input |
||
235 | * @param TreeNode $result |
||
236 | * @param string $selfMarker |
||
237 | */ |
||
238 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
326 | } |
||
327 |