Skip to content

Commit 5e10e95

Browse files
committed
bug #4654 ArrayIndentationFixer - Fix array indentation for multiline values (julienfalque)
This PR was merged into the 2.15 branch. Discussion ---------- ArrayIndentationFixer - Fix array indentation for multiline values Fixes #3831 and replaces #3624. Commits ------- d65fee3 Fix array indentation for multiline values
2 parents 24ee1c3 + d65fee3 commit 5e10e95

File tree

2 files changed

+129
-28
lines changed

2 files changed

+129
-28
lines changed

src/Fixer/Whitespace/ArrayIndentationFixer.php

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,50 @@ private function findArrayTokenRanges(Tokens $tokens, $from, $to)
147147
{
148148
$arrayTokenRanges = [];
149149
$currentArray = null;
150+
$valueSinceIndex = null;
150151

151152
for ($index = $from; $index <= $to; ++$index) {
152153
$token = $tokens[$index];
153154

154-
if (null !== $currentArray && $currentArray['end'] === $index) {
155+
if ($token->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
156+
$arrayStartIndex = $index;
157+
158+
if ($token->isGivenKind(T_ARRAY)) {
159+
$index = $tokens->getNextTokenOfKind($index, ['(']);
160+
}
161+
162+
$endIndex = $tokens->findBlockEnd(
163+
$tokens[$index]->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE,
164+
$index
165+
);
166+
167+
if (null === $currentArray) {
168+
$currentArray = [
169+
'start' => $index,
170+
'end' => $endIndex,
171+
'ignored_tokens_ranges' => [],
172+
];
173+
} else {
174+
if (null === $valueSinceIndex) {
175+
$valueSinceIndex = $arrayStartIndex;
176+
}
177+
178+
$index = $endIndex;
179+
}
180+
181+
continue;
182+
}
183+
184+
if (null === $currentArray || $token->isWhitespace() || $token->isComment()) {
185+
continue;
186+
}
187+
188+
if ($currentArray['end'] === $index) {
189+
if (null !== $valueSinceIndex) {
190+
$currentArray['ignored_tokens_ranges'][] = [$valueSinceIndex, $tokens->getPrevMeaningfulToken($index)];
191+
$valueSinceIndex = null;
192+
}
193+
155194
$rangeIndexes = [$currentArray['start']];
156195
foreach ($currentArray['ignored_tokens_ranges'] as list($start, $end)) {
157196
$rangeIndexes[] = $start - 1;
@@ -172,39 +211,23 @@ private function findArrayTokenRanges(Tokens $tokens, $from, $to)
172211
continue;
173212
}
174213

175-
if (null === $currentArray && $token->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
176-
if ($token->isGivenKind(T_ARRAY)) {
177-
$index = $tokens->getNextTokenOfKind($index, ['(']);
178-
}
179-
180-
$currentArray = [
181-
'start' => $index,
182-
'end' => $tokens->findBlockEnd(
183-
$tokens[$index]->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE,
184-
$index
185-
),
186-
'ignored_tokens_ranges' => [],
187-
];
188-
189-
continue;
214+
if (null === $valueSinceIndex) {
215+
$valueSinceIndex = $index;
190216
}
191217

192218
if (
193-
null !== $currentArray && (
194-
($token->equals('(') && !$tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_ARRAY))
195-
|| $token->equals('{')
196-
)
219+
($token->equals('(') && !$tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_ARRAY))
220+
|| $token->equals('{')
197221
) {
198-
$endIndex = $tokens->findBlockEnd(
222+
$index = $tokens->findBlockEnd(
199223
$token->equals('{') ? Tokens::BLOCK_TYPE_CURLY_BRACE : Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
200224
$index
201225
);
226+
}
202227

203-
$currentArray['ignored_tokens_ranges'][] = [$index, $endIndex];
204-
205-
$index = $endIndex;
206-
207-
continue;
228+
if ($token->equals(',')) {
229+
$currentArray['ignored_tokens_ranges'][] = [$valueSinceIndex, $tokens->getPrevMeaningfulToken($index)];
230+
$valueSinceIndex = null;
208231
}
209232
}
210233

tests/Fixer/Whitespace/ArrayIndentationFixerTest.php

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,8 @@ public function bar()
629629
'bar',
630630
],[[],[]]]
631631
]],[
632-
'baz',
633-
]]
632+
'baz',
633+
]]
634634
],[]],[]]
635635
]
636636
];
@@ -732,6 +732,84 @@ class="link"
732732
"bar",
733733
];',
734734
],
735+
[
736+
<<<'EXPECTED'
737+
<?php
738+
$foo = [
739+
'foo' =>
740+
'Some'
741+
.' long'
742+
.' string',
743+
'bar' =>
744+
'Another'
745+
.' long'
746+
.' string'
747+
];
748+
EXPECTED
749+
,
750+
<<<'INPUT'
751+
<?php
752+
$foo = [
753+
'foo' =>
754+
'Some'
755+
.' long'
756+
.' string',
757+
'bar' =>
758+
'Another'
759+
.' long'
760+
.' string'
761+
];
762+
INPUT
763+
,
764+
],
765+
[
766+
<<<'EXPECTED'
767+
<?php
768+
$foo = [
769+
$test
770+
? [
771+
123,
772+
]
773+
: [
774+
321,
775+
],
776+
];
777+
EXPECTED
778+
,
779+
<<<'INPUT'
780+
<?php
781+
$foo = [
782+
$test
783+
? [
784+
123,
785+
]
786+
: [
787+
321,
788+
],
789+
];
790+
INPUT
791+
,
792+
],
793+
[
794+
<<<'EXPECTED'
795+
<?php
796+
$foo = [[
797+
new Foo(
798+
'foo'
799+
),
800+
]];
801+
EXPECTED
802+
,
803+
<<<'INPUT'
804+
<?php
805+
$foo = [[
806+
new Foo(
807+
'foo'
808+
),
809+
]];
810+
INPUT
811+
,
812+
],
735813
]);
736814
}
737815

0 commit comments

Comments
 (0)