| Conditions | 32 |
| Paths | 112 |
| Total Lines | 176 |
| Code Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 345 | private function getNewOrder(array $uses, Tokens $tokens): array |
||
| 346 | { |
||
| 347 | $indexes = []; |
||
| 348 | $originalIndexes = []; |
||
| 349 | $lineEnding = $this->whitespacesConfig->getLineEnding(); |
||
| 350 | |||
| 351 | for ($i = \count($uses) - 1; $i >= 0; --$i) { |
||
| 352 | $index = $uses[$i]; |
||
| 353 | |||
| 354 | $startIndex = $tokens->getTokenNotOfKindsSibling($index + 1, 1, [T_WHITESPACE]); |
||
| 355 | $endIndex = $tokens->getNextTokenOfKind($startIndex, [';', [T_CLOSE_TAG]]); |
||
| 356 | $previous = $tokens->getPrevMeaningfulToken($endIndex); |
||
| 357 | |||
| 358 | $group = $tokens[$previous]->isGivenKind(CT::T_GROUP_IMPORT_BRACE_CLOSE); |
||
| 359 | if ($tokens[$startIndex]->isGivenKind(CT::T_CONST_IMPORT)) { |
||
| 360 | $type = self::IMPORT_TYPE_CONST; |
||
| 361 | $index = $tokens->getNextNonWhitespace($startIndex); |
||
| 362 | } elseif ($tokens[$startIndex]->isGivenKind(CT::T_FUNCTION_IMPORT)) { |
||
| 363 | $type = self::IMPORT_TYPE_FUNCTION; |
||
| 364 | $index = $tokens->getNextNonWhitespace($startIndex); |
||
| 365 | } else { |
||
| 366 | $type = self::IMPORT_TYPE_CLASS; |
||
| 367 | $index = $startIndex; |
||
| 368 | } |
||
| 369 | |||
| 370 | $namespaceTokens = []; |
||
| 371 | |||
| 372 | while ($index <= $endIndex) { |
||
| 373 | $token = $tokens[$index]; |
||
| 374 | |||
| 375 | if ($index === $endIndex || (!$group && $token->equals(','))) { |
||
| 376 | if ($group && self::SORT_NONE !== $this->configuration['sort_algorithm']) { |
||
| 377 | // if group import, sort the items within the group definition |
||
| 378 | |||
| 379 | // figure out where the list of namespace parts within the group def. starts |
||
| 380 | $namespaceTokensCount = \count($namespaceTokens) - 1; |
||
| 381 | $namespace = ''; |
||
| 382 | for ($k = 0; $k < $namespaceTokensCount; ++$k) { |
||
| 383 | if ($namespaceTokens[$k]->isGivenKind(CT::T_GROUP_IMPORT_BRACE_OPEN)) { |
||
| 384 | $namespace .= '{'; |
||
| 385 | |||
| 386 | break; |
||
| 387 | } |
||
| 388 | |||
| 389 | $namespace .= $namespaceTokens[$k]->getContent(); |
||
| 390 | } |
||
| 391 | |||
| 392 | // fetch all parts, split up in an array of strings, move comments to the end |
||
| 393 | $parts = []; |
||
| 394 | $firstIndent = ''; |
||
| 395 | $separator = ', '; |
||
| 396 | $lastIndent = ''; |
||
| 397 | $hasGroupTrailingComma = false; |
||
| 398 | |||
| 399 | for ($k1 = $k + 1; $k1 < $namespaceTokensCount; ++$k1) { |
||
| 400 | $comment = ''; |
||
| 401 | $namespacePart = ''; |
||
| 402 | for ($k2 = $k1;; ++$k2) { |
||
| 403 | if ($namespaceTokens[$k2]->equalsAny([',', [CT::T_GROUP_IMPORT_BRACE_CLOSE]])) { |
||
| 404 | break; |
||
| 405 | } |
||
| 406 | |||
| 407 | if ($namespaceTokens[$k2]->isComment()) { |
||
| 408 | $comment .= $namespaceTokens[$k2]->getContent(); |
||
| 409 | |||
| 410 | continue; |
||
| 411 | } |
||
| 412 | |||
| 413 | // if there is any line ending inside the group import, it should be indented properly |
||
| 414 | if ( |
||
| 415 | '' === $firstIndent |
||
| 416 | && $namespaceTokens[$k2]->isWhitespace() |
||
| 417 | && false !== strpos($namespaceTokens[$k2]->getContent(), $lineEnding) |
||
| 418 | ) { |
||
| 419 | $lastIndent = $lineEnding; |
||
| 420 | $firstIndent = $lineEnding.$this->whitespacesConfig->getIndent(); |
||
| 421 | $separator = ','.$firstIndent; |
||
| 422 | } |
||
| 423 | |||
| 424 | $namespacePart .= $namespaceTokens[$k2]->getContent(); |
||
| 425 | } |
||
| 426 | |||
| 427 | $namespacePart = trim($namespacePart); |
||
| 428 | if ('' === $namespacePart) { |
||
| 429 | $hasGroupTrailingComma = true; |
||
| 430 | |||
| 431 | continue; |
||
| 432 | } |
||
| 433 | |||
| 434 | $comment = trim($comment); |
||
| 435 | if ('' !== $comment) { |
||
| 436 | $namespacePart .= ' '.$comment; |
||
| 437 | } |
||
| 438 | |||
| 439 | $parts[] = $namespacePart; |
||
| 440 | |||
| 441 | $k1 = $k2; |
||
| 442 | } |
||
| 443 | |||
| 444 | $sortedParts = $parts; |
||
| 445 | sort($parts); |
||
| 446 | |||
| 447 | // check if the order needs to be updated, otherwise don't touch as we might change valid CS (to other valid CS). |
||
| 448 | if ($sortedParts === $parts) { |
||
| 449 | $namespace = Tokens::fromArray($namespaceTokens)->generateCode(); |
||
| 450 | } else { |
||
| 451 | $namespace .= $firstIndent.implode($separator, $parts).($hasGroupTrailingComma ? ',' : '').$lastIndent.'}'; |
||
| 452 | } |
||
| 453 | } else { |
||
| 454 | $namespace = Tokens::fromArray($namespaceTokens)->generateCode(); |
||
| 455 | } |
||
| 456 | |||
| 457 | $indexes[$startIndex] = [ |
||
| 458 | 'namespace' => $namespace, |
||
| 459 | 'startIndex' => $startIndex, |
||
| 460 | 'endIndex' => $index - 1, |
||
| 461 | 'importType' => $type, |
||
| 462 | 'group' => $group, |
||
| 463 | ]; |
||
| 464 | |||
| 465 | $originalIndexes[] = $startIndex; |
||
| 466 | |||
| 467 | if ($index === $endIndex) { |
||
| 468 | break; |
||
| 469 | } |
||
| 470 | |||
| 471 | $namespaceTokens = []; |
||
| 472 | $nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[','], [T_WHITESPACE]]); |
||
| 473 | $startIndex = $nextPartIndex; |
||
| 474 | $index = $nextPartIndex; |
||
| 475 | |||
| 476 | continue; |
||
| 477 | } |
||
| 478 | |||
| 479 | $namespaceTokens[] = $token; |
||
| 480 | ++$index; |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | // Is sort types provided, sorting by groups and each group by algorithm |
||
| 485 | if (null !== $this->configuration['imports_order']) { |
||
| 486 | // Grouping indexes by import type. |
||
| 487 | $groupedByTypes = []; |
||
| 488 | foreach ($indexes as $startIndex => $item) { |
||
| 489 | $groupedByTypes[$item['importType']][$startIndex] = $item; |
||
| 490 | } |
||
| 491 | |||
| 492 | // Sorting each group by algorithm. |
||
| 493 | foreach ($groupedByTypes as $type => $indexes) { |
||
| 494 | $groupedByTypes[$type] = $this->sortByAlgorithm($indexes); |
||
| 495 | } |
||
| 496 | |||
| 497 | // Ordering groups |
||
| 498 | $sortedGroups = []; |
||
| 499 | foreach ($this->configuration['imports_order'] as $type) { |
||
| 500 | if (isset($groupedByTypes[$type]) && !empty($groupedByTypes[$type])) { |
||
| 501 | foreach ($groupedByTypes[$type] as $startIndex => $item) { |
||
| 502 | $sortedGroups[$startIndex] = $item; |
||
| 503 | } |
||
| 504 | } |
||
| 505 | } |
||
| 506 | $indexes = $sortedGroups; |
||
| 507 | } else { |
||
| 508 | // Sorting only by algorithm |
||
| 509 | $indexes = $this->sortByAlgorithm($indexes); |
||
| 510 | } |
||
| 511 | |||
| 512 | $index = -1; |
||
| 513 | $usesOrder = []; |
||
| 514 | |||
| 515 | // Loop trough the index but use original index order |
||
| 516 | foreach ($indexes as $v) { |
||
| 517 | $usesOrder[$originalIndexes[++$index]] = $v; |
||
| 518 | } |
||
| 519 | |||
| 520 | return $usesOrder; |
||
| 521 | } |
||
| 537 |