| 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); |
||
| 191 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
| 192 | { |
||
| 193 | // Rewrite prefix if TreeNode value handler is present |
||
| 194 | $prefix = is_callable($this->treeNodeValueHandler) ? call_user_func($this->treeNodeValueHandler, $prefix) : $prefix; |
||
| 195 | |||
| 196 | // Create tree node |
||
| 197 | $newChild = $result->append($prefix); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Iterate all combinations of strings and group by LMP |
||
| 201 | */ |
||
| 202 | $longestPrefixes = []; |
||
| 203 | for ($i = 0, $count = count($input); $i < $count; $i++) { |
||
| 204 | for ($j = $i + 1; $j < $count; $j++) { |
||
| 205 | $longestMatchedPrefix = $this->getLongestMatchingPrefix($input[$i], $input[$j]); |
||
| 206 | |||
| 207 | // We have found at least one matching character between strings |
||
| 208 | if ($longestMatchedPrefix !== '') { |
||
| 209 | $this->addUniqueToArray($input[$i], $longestPrefixes[$longestMatchedPrefix]); |
||
| 210 | $this->addUniqueToArray($input[$j], $longestPrefixes[$longestMatchedPrefix]); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sort LMPs(array keys) ascending by key length |
||
| 217 | */ |
||
| 218 | $this->sortArrayByKeys($longestPrefixes); |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Iterate all sorted LMP strings and remove duplicates from LMP string ordered lower |
||
| 222 | */ |
||
| 223 | $keys = array_keys($longestPrefixes); |
||
| 224 | for ($i = 0, $length = count($keys); $i < $length; $i++) { |
||
| 225 | for ($j = $i + 1; $j < $length; $j++) { |
||
| 226 | $this->removeDuplicatesInSubArray($longestPrefixes[$keys[$i]], $longestPrefixes[$keys[$j]]); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | // Remove empty LMPs as they are included in smaller LMPs |
||
| 231 | $longestPrefixes = array_filter($longestPrefixes); |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Search for input string that do not have LMP, and add missing as LMP |
||
| 235 | */ |
||
| 236 | foreach ($input as $string) { |
||
| 237 | $found = false; |
||
| 238 | |||
| 239 | if ($string !== $selfMarker) { |
||
| 240 | foreach ($longestPrefixes as $strings) { |
||
| 241 | if (in_array($string, $strings, true)) { |
||
| 242 | $found = true; |
||
| 243 | break; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | if (!$found) { |
||
| 248 | $longestPrefixes[$string] = [$selfMarker]; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * After filtering LMPs remove LMP from matched string arrays |
||
| 255 | */ |
||
| 256 | $longestPrefixes = $this->removeKeyFromArrayStrings($longestPrefixes, $selfMarker); |
||
| 257 | |||
| 258 | /** |
||
| 259 | * If we have self marker as an input string - create LMP for it |
||
| 260 | */ |
||
| 261 | if (in_array($selfMarker, $input, true)) { |
||
| 262 | $longestPrefixes = array_merge([$selfMarker => []], $longestPrefixes); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Recursively iterate current level LMPs |
||
| 267 | */ |
||
| 268 | foreach ($longestPrefixes as $longestPrefix => $strings) { |
||
| 269 | $this->innerProcessor((string)$longestPrefix, $strings, $newChild); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 |