Conditions | 14 |
Paths | 768 |
Total Lines | 81 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types = 1); |
||
180 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
181 | { |
||
182 | // Rewrite prefix if TreeNode value handler is present |
||
183 | $prefix = is_callable($this->treeNodeValueHandler) ? call_user_func($this->treeNodeValueHandler, $prefix) : $prefix; |
||
184 | |||
185 | // Create tree node |
||
186 | $newChild = $result->append($prefix); |
||
187 | |||
188 | /** |
||
189 | * Iterate all combinations of strings and group by LMP |
||
190 | */ |
||
191 | $longestPrefixes = []; |
||
192 | for ($i = 0, $count = count($input); $i < $count; $i++) { |
||
193 | for ($j = $i + 1; $j < $count; $j++) { |
||
194 | $longestMatchedPrefix = $this->getLongestMatchingPrefix($input[$i], $input[$j]); |
||
195 | |||
196 | // We have found at least one matching character between strings |
||
197 | if ($longestMatchedPrefix !== '') { |
||
198 | $this->addUniqueToArray($input[$i], $longestPrefixes[$longestMatchedPrefix]); |
||
199 | $this->addUniqueToArray($input[$j], $longestPrefixes[$longestMatchedPrefix]); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Sort LMPs(array keys) ascending by key length |
||
206 | */ |
||
207 | $this->sortArrayByKeys($longestPrefixes); |
||
208 | |||
209 | /** |
||
210 | * Iterate all sorted LMP strings and remove duplicates from LMP string ordered lower |
||
211 | */ |
||
212 | $keys = array_keys($longestPrefixes); |
||
213 | for ($i = 0, $length = count($keys); $i < $length; $i++) { |
||
214 | for ($j = $i + 1; $j < $length; $j++) { |
||
215 | $this->removeDuplicatesInSubArray($longestPrefixes[$keys[$i]], $longestPrefixes[$keys[$j]]); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | // Remove empty LMPs as they are included in smaller LMPs |
||
220 | $longestPrefixes = array_filter($longestPrefixes); |
||
221 | |||
222 | /** |
||
223 | * Search for input string that do not have LMP, and add missing as LMP |
||
224 | */ |
||
225 | foreach ($input as $string) { |
||
226 | $found = false; |
||
227 | |||
228 | if ($string !== $selfMarker) { |
||
229 | foreach ($longestPrefixes as $strings) { |
||
230 | if (in_array($string, $strings, true)) { |
||
231 | $found = true; |
||
232 | break; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | if (!$found) { |
||
237 | $longestPrefixes[$string] = [$selfMarker]; |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * After filtering LMPs remove LMP from matched string arrays |
||
244 | */ |
||
245 | $longestPrefixes = $this->removeKeyFromArrayStrings($longestPrefixes, $selfMarker); |
||
246 | |||
247 | /** |
||
248 | * If we have self marker as an input string - create LMP for it |
||
249 | */ |
||
250 | if (in_array($selfMarker, $input, true)) { |
||
251 | $longestPrefixes = array_merge([$selfMarker => []], $longestPrefixes); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Recursively iterate current level LMPs |
||
256 | */ |
||
257 | foreach ($longestPrefixes as $longestPrefix => $strings) { |
||
258 | $this->innerProcessor((string)$longestPrefix, $strings, $newChild); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 |