Skip to content

Commit a982efc

Browse files
kubawerlosSpacePossum
authored andcommitted
SingleTraitInsertPerStatement - fix formatting for multiline \"use\"
1 parent 6325744 commit a982efc

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

src/Fixer/ClassNotation/SingleTraitInsertPerStatementFixer.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpCsFixer\AbstractFixer;
1616
use PhpCsFixer\FixerDefinition\CodeSample;
1717
use PhpCsFixer\FixerDefinition\FixerDefinition;
18+
use PhpCsFixer\Preg;
1819
use PhpCsFixer\Tokenizer\CT;
1920
use PhpCsFixer\Tokenizer\Token;
2021
use PhpCsFixer\Tokenizer\Tokens;
@@ -58,24 +59,35 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5859
if ($tokens[$index]->isGivenKind(CT::T_USE_TRAIT)) {
5960
$candidates = $this->getCandidates($tokens, $index);
6061
if (\count($candidates) > 0) {
61-
$this->fixTraitUse($tokens, array_reverse($candidates));
62+
$this->fixTraitUse($tokens, $index, $candidates);
6263
}
6364
}
6465
}
6566
}
6667

6768
/**
68-
* @param int[] $candidates ',' indexes to fix
69+
* @param int $useTraitIndex
70+
* @param int[] $candidates ',' indexes to fix
6971
*/
70-
private function fixTraitUse(Tokens $tokens, array $candidates)
72+
private function fixTraitUse(Tokens $tokens, $useTraitIndex, array $candidates)
7173
{
72-
foreach ($candidates as $nextInsertIndex) {
73-
$tokens[$nextInsertIndex] = new Token(';');
74-
$tokens->insertAt($nextInsertIndex + 1, new Token([CT::T_USE_TRAIT, 'use']));
74+
foreach ($candidates as $commaIndex) {
75+
$inserts = [
76+
new Token([CT::T_USE_TRAIT, 'use']),
77+
new Token([T_WHITESPACE, ' ']),
78+
];
7579

76-
if (!$tokens[$nextInsertIndex + 2]->isWhitespace()) {
77-
$tokens->insertAt($nextInsertIndex + 2, new Token([T_WHITESPACE, ' ']));
80+
$nextImportStartIndex = $tokens->getNextMeaningfulToken($commaIndex);
81+
82+
if ($tokens[$nextImportStartIndex - 1]->isWhitespace()) {
83+
if (1 === Preg::match('/\R/', $tokens[$nextImportStartIndex - 1]->getContent())) {
84+
array_unshift($inserts, clone $tokens[$useTraitIndex - 1]);
85+
}
86+
$tokens->clearAt($nextImportStartIndex - 1);
7887
}
88+
89+
$tokens[$commaIndex] = new Token(';');
90+
$tokens->insertAt($nextImportStartIndex, $inserts);
7991
}
8092
}
8193

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

101-
return $indexes;
113+
return array_reverse($indexes);
102114
}
103115
}

tests/Fixer/ClassNotation/SingleTraitInsertPerStatementFixerTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ final class Example
8181
{
8282
use Foo, Bar ;
8383
}
84+
',
85+
],
86+
'simple III' => [
87+
'<?php
88+
class Example
89+
{
90+
use Foo;use Bar;
91+
92+
public function baz() {}
93+
}
94+
',
95+
'<?php
96+
class Example
97+
{
98+
use Foo, Bar;
99+
100+
public function baz() {}
101+
}
84102
',
85103
],
86104
'multiple' => [
@@ -101,6 +119,44 @@ final class Example
101119
use Foo10, Bar11, Bar110;
102120
use Foo20, Bar20, Bar200, Bar201;
103121
}
122+
',
123+
],
124+
'multiple_multiline' => [
125+
'<?php
126+
final class Example
127+
{
128+
use Foo;
129+
use Bar;
130+
use Baz;
131+
}
132+
',
133+
'<?php
134+
final class Example
135+
{
136+
use Foo,
137+
Bar,
138+
Baz;
139+
}
140+
',
141+
],
142+
'multiple_multiline_with_comment' => [
143+
'<?php
144+
final class Example
145+
{
146+
use Foo;
147+
use Bar;
148+
// Bazz,
149+
use Baz;
150+
}
151+
',
152+
'<?php
153+
final class Example
154+
{
155+
use Foo,
156+
Bar,
157+
// Bazz,
158+
Baz;
159+
}
104160
',
105161
],
106162
'namespaces' => [
@@ -126,9 +182,9 @@ class ZZ
126182
use#2
127183
Z/* 2 */ #3
128184
#4
129-
;use #5
185+
;#5
130186
#6
131-
T#7
187+
use T#7
132188
#8
133189
;#9
134190
#10

0 commit comments

Comments
 (0)