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