Skip to content

Commit cc04334

Browse files
committed
Fixed ArrayHelper
1 parent 29b2d1c commit cc04334

File tree

3 files changed

+53
-56
lines changed

3 files changed

+53
-56
lines changed

SlevomatCodingStandard/Helpers/ArrayHelper.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,24 @@ public static function parse(File $phpcsFile, int $arrayPointer): array
6262
$nextEffectivePointer = TokenHelper::findNextEffective($phpcsFile, $i + 1);
6363

6464
if ($nextEffectivePointer === $arrayCloserPointer) {
65-
$arrayKeyValueEndPointer = $i;
65+
$arrayKeyValueEndPointer = self::getValueEndPointer($phpcsFile, $i, $arrayCloserPointer, $indentation);
6666
break;
6767
}
6868

6969
if ($token['code'] !== T_COMMA || !ScopeHelper::isInSameScope($phpcsFile, $arrayOpenerPointer, $i)) {
70+
$arrayKeyValueEndPointer = $i;
7071
continue;
7172
}
7273

73-
$arrayKeyValueEndPointer = $tokens[$nextEffectivePointer]['line'] === $tokens[$i]['line']
74-
? $nextEffectivePointer - 1
75-
: self::getValueEndPointer($phpcsFile, $i, $indentation);
74+
$arrayKeyValueEndPointer = self::getValueEndPointer($phpcsFile, $i, $arrayCloserPointer, $indentation);
7675

7776
$keyValues[] = new ArrayKeyValue($phpcsFile, $arrayKeyValueStartPointer, $arrayKeyValueEndPointer);
7877

7978
$arrayKeyValueStartPointer = $arrayKeyValueEndPointer + 1;
8079
$i = $arrayKeyValueEndPointer;
8180
}
8281

83-
$keyValues[] = new ArrayKeyValue(
84-
$phpcsFile,
85-
$arrayKeyValueStartPointer,
86-
self::getValueEndPointer($phpcsFile, $arrayKeyValueEndPointer, $indentation)
87-
);
82+
$keyValues[] = new ArrayKeyValue($phpcsFile, $arrayKeyValueStartPointer, $arrayKeyValueEndPointer);
8883

8984
return $keyValues;
9085
}
@@ -200,11 +195,15 @@ public static function openClosePointers(array $token): array
200195
return [(int) $pointerOpener, (int) $pointerCloser];
201196
}
202197

203-
private static function getValueEndPointer(File $phpcsFile, int $endPointer, string $indentation): int
198+
private static function getValueEndPointer(File $phpcsFile, int $endPointer, int $arrayCloserPointer, string $indentation): int
204199
{
205200
$tokens = $phpcsFile->getTokens();
206201

207-
$nextEffectivePointer = TokenHelper::findNextEffective($phpcsFile, $endPointer + 1);
202+
$nextEffectivePointer = TokenHelper::findNextEffective($phpcsFile, $endPointer + 1, $arrayCloserPointer + 1);
203+
204+
if ($tokens[$nextEffectivePointer]['line'] === $tokens[$endPointer]['line']) {
205+
return $nextEffectivePointer - 1;
206+
}
208207

209208
for ($i = $endPointer + 1; $i < $nextEffectivePointer; $i++) {
210209
if ($tokens[$i]['line'] === $tokens[$endPointer]['line']) {

SlevomatCodingStandard/Helpers/ArrayKeyValue.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace SlevomatCodingStandard\Helpers;
44

55
use PHP_CodeSniffer\Files\File;
6-
use function array_key_exists;
76
use function in_array;
87
use function ltrim;
98
use function rtrim;
@@ -146,18 +145,6 @@ private function addValues(File $phpcsFile): void
146145
continue;
147146
}
148147

149-
if (array_key_exists('scope_closer', $token) && $token['scope_closer'] > $i) {
150-
$key .= TokenHelper::getContent($phpcsFile, $i, $token['scope_closer']);
151-
$i = $token['scope_closer'];
152-
continue;
153-
}
154-
155-
if (array_key_exists('parenthesis_closer', $token)) {
156-
$key .= TokenHelper::getContent($phpcsFile, $i, $token['parenthesis_closer']);
157-
$i = $token['parenthesis_closer'];
158-
continue;
159-
}
160-
161148
if ($firstNonWhitespace === null && $token['code'] !== T_WHITESPACE) {
162149
$firstNonWhitespace = $i;
163150
}
Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
<?php // lint >= 7.4
22

3-
[];
3+
// [];
4+
//
5+
// array();
6+
//
7+
// ['foo', 'bar', 'baz'];
8+
//
9+
// array('foo', 'bar', 'baz');
10+
//
11+
// $a = [
12+
// 0 => 'zero',
13+
// 'foo' => 'foo',
14+
// 'bar' => 'bar',
15+
// 'baz' => 'baz'
16+
// ];
17+
//
18+
// array(
19+
// 0 => 'zero',
20+
// 'foo' => 'foo',
21+
// ...$a,
22+
// 'bar' => 'bar',
23+
// 'baz' => 'baz',
24+
// ...$a
25+
// );
26+
//
27+
// [
28+
// 'bail',
29+
// 'array',
30+
// 'required',
31+
// static function (array $value): array {
32+
// foreach ($value as $x => $z) {
33+
// $x + $z;
34+
// }
35+
// },
36+
// ];
437

5-
array();
38+
['newsletter' => [], 'campaign' => [], 'other' => []];
639

7-
['foo', 'bar', 'baz'];
8-
9-
array('foo', 'bar', 'baz');
10-
11-
$a = [
12-
0 => 'zero',
13-
'foo' => 'foo',
14-
'bar' => 'bar',
15-
'baz' => 'baz'
16-
];
17-
18-
array(
19-
0 => 'zero',
20-
'foo' => 'foo',
21-
...$a,
22-
'bar' => 'bar',
23-
'baz' => 'baz',
24-
...$a
25-
);
26-
27-
[
28-
'bail',
29-
'array',
30-
'required',
31-
static function (array $value): array {
32-
foreach ($value as $x => $z) {
33-
$x + $z;
34-
}
35-
},
36-
];
40+
$data = [
41+
'contactId' => 'string',
42+
'updates' => [
43+
[
44+
'update' => [['__tpe' => 'Personal'], '[email protected]'],
45+
'__tpe' => 'ContactEmailUpdate',
46+
],
47+
]];

0 commit comments

Comments
 (0)