Skip to content

Commit

Permalink
bug #4710 SingleTraitInsertPerStatement - fix formatting for multilin…
Browse files Browse the repository at this point in the history
…e "use" (kubawerlos)

This PR was squashed before being merged into the 2.15 branch (closes #4710).

Discussion
----------

SingleTraitInsertPerStatement - fix formatting for multiline "use"

Fixes #4709

Commits
-------

a982efc SingleTraitInsertPerStatement - fix formatting for multiline \"use\"
  • Loading branch information
SpacePossum committed Jan 18, 2020
2 parents 6325744 + a982efc commit dd2f6ae
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/Fixer/ClassNotation/SingleTraitInsertPerStatementFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
Expand Down Expand Up @@ -58,24 +59,35 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
if ($tokens[$index]->isGivenKind(CT::T_USE_TRAIT)) {
$candidates = $this->getCandidates($tokens, $index);
if (\count($candidates) > 0) {
$this->fixTraitUse($tokens, array_reverse($candidates));
$this->fixTraitUse($tokens, $index, $candidates);
}
}
}
}

/**
* @param int[] $candidates ',' indexes to fix
* @param int $useTraitIndex
* @param int[] $candidates ',' indexes to fix
*/
private function fixTraitUse(Tokens $tokens, array $candidates)
private function fixTraitUse(Tokens $tokens, $useTraitIndex, array $candidates)
{
foreach ($candidates as $nextInsertIndex) {
$tokens[$nextInsertIndex] = new Token(';');
$tokens->insertAt($nextInsertIndex + 1, new Token([CT::T_USE_TRAIT, 'use']));
foreach ($candidates as $commaIndex) {
$inserts = [
new Token([CT::T_USE_TRAIT, 'use']),
new Token([T_WHITESPACE, ' ']),
];

if (!$tokens[$nextInsertIndex + 2]->isWhitespace()) {
$tokens->insertAt($nextInsertIndex + 2, new Token([T_WHITESPACE, ' ']));
$nextImportStartIndex = $tokens->getNextMeaningfulToken($commaIndex);

if ($tokens[$nextImportStartIndex - 1]->isWhitespace()) {
if (1 === Preg::match('/\R/', $tokens[$nextImportStartIndex - 1]->getContent())) {
array_unshift($inserts, clone $tokens[$useTraitIndex - 1]);
}
$tokens->clearAt($nextImportStartIndex - 1);
}

$tokens[$commaIndex] = new Token(';');
$tokens->insertAt($nextImportStartIndex, $inserts);
}
}

Expand All @@ -98,6 +110,6 @@ private function getCandidates(Tokens $tokens, $index)
$index = $tokens->getNextTokenOfKind($index, [',', ';', '{']);
}

return $indexes;
return array_reverse($indexes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ final class Example
{
use Foo, Bar ;
}
',
],
'simple III' => [
'<?php
class Example
{
use Foo;use Bar;
public function baz() {}
}
',
'<?php
class Example
{
use Foo, Bar;
public function baz() {}
}
',
],
'multiple' => [
Expand All @@ -101,6 +119,44 @@ final class Example
use Foo10, Bar11, Bar110;
use Foo20, Bar20, Bar200, Bar201;
}
',
],
'multiple_multiline' => [
'<?php
final class Example
{
use Foo;
use Bar;
use Baz;
}
',
'<?php
final class Example
{
use Foo,
Bar,
Baz;
}
',
],
'multiple_multiline_with_comment' => [
'<?php
final class Example
{
use Foo;
use Bar;
// Bazz,
use Baz;
}
',
'<?php
final class Example
{
use Foo,
Bar,
// Bazz,
Baz;
}
',
],
'namespaces' => [
Expand All @@ -126,9 +182,9 @@ class ZZ
use#2
Z/* 2 */ #3
#4
;use #5
;#5
#6
T#7
use T#7
#8
;#9
#10
Expand Down

0 comments on commit dd2f6ae

Please sign in to comment.