Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed May 3, 2021
2 parents 9f6f63c + d6428ab commit c5d6614
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 90 deletions.
20 changes: 6 additions & 14 deletions src/Fixer/Import/GroupImportFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,10 @@ private function removeSingleUseStatements(array $statements, Tokens $tokens)
private function addGroupUseStatements(array $statements, Tokens $tokens)
{
$currentUseDeclaration = null;
$insertIndex = \array_slice($statements, -1)[0]->getEndIndex();

while ($tokens[$insertIndex]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
++$insertIndex;
}
$insertIndex = \array_slice($statements, -1)[0]->getEndIndex() + 1;

foreach ($statements as $index => $useDeclaration) {
if ($this->areDeclarationsDifferent($currentUseDeclaration, $useDeclaration)) {
if ($index > 1) {
++$insertIndex;
}

$currentUseDeclaration = $useDeclaration;
$insertIndex += $this->createNewGroup(
$tokens,
Expand All @@ -170,11 +162,11 @@ private function addGroupUseStatements(array $statements, Tokens $tokens)
];

if ($useDeclaration->isAliased()) {
$tokens->insertAt($insertIndex + 1, $newTokens);
$tokens->insertAt($insertIndex, $newTokens);
$insertIndex += \count($newTokens);
$newTokens = [];

$insertIndex += $this->insertToGroupUseWithAlias($tokens, $insertIndex + 1, $useDeclaration);
$insertIndex += $this->insertToGroupUseWithAlias($tokens, $insertIndex, $useDeclaration);
}

$newTokens[] = new Token([T_STRING, $useDeclaration->getShortName()]);
Expand All @@ -185,7 +177,7 @@ private function addGroupUseStatements(array $statements, Tokens $tokens)
$newTokens[] = new Token([T_WHITESPACE, "\n"]);
}

$tokens->insertAt($insertIndex + 1, $newTokens);
$tokens->insertAt($insertIndex, $newTokens);
$insertIndex += \count($newTokens);
}
}
Expand Down Expand Up @@ -222,7 +214,7 @@ private function insertToGroupUseWithAlias(Tokens $tokens, $insertIndex, Namespa

$tokens->insertAt($insertIndex, $newTokens);

return \count($newTokens);
return \count($newTokens) + 1;
}

/**
Expand Down Expand Up @@ -276,7 +268,7 @@ private function createNewGroup(Tokens $tokens, $insertIndex, NamespaceUseAnalys
$insertIndex += $inserted;
}

$tokens->insertAt($insertIndex + 1, new Token([T_STRING, $useDeclaration->getShortName()]));
$tokens->insertAt($insertIndex, new Token([T_STRING, $useDeclaration->getShortName()]));
++$insertedTokens;

return $insertedTokens;
Expand Down
Loading

0 comments on commit c5d6614

Please sign in to comment.