Skip to content

Commit

Permalink
bug #5673 GroupImportFixer - Fix failing case (julienfalque)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.18 branch.

Discussion
----------

GroupImportFixer - Fix failing case

Commits
-------

2b1e42e Fix group_import failing case
  • Loading branch information
keradus committed May 3, 2021
2 parents 3fadaee + 2b1e42e commit d6428ab
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
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

0 comments on commit d6428ab

Please sign in to comment.