Skip to content

Commit 5bde1a6

Browse files
committed
SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed: Fixed false positive
1 parent 999d563 commit 5bde1a6

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

SlevomatCodingStandard/Helpers/ArrayKeyValue.php

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

55
use PHP_CodeSniffer\Files\File;
6+
use function array_reverse;
7+
use function current;
68
use function in_array;
79
use function ltrim;
810
use function rtrim;
911
use function strpos;
1012
use function trim;
13+
use const T_CLOSURE;
1114
use const T_COMMA;
1215
use const T_DOUBLE_ARROW;
1316
use const T_ELLIPSIS;
@@ -119,6 +122,10 @@ private function addValues(File $phpcsFile): void
119122
}
120123

121124
if ($token['code'] === T_DOUBLE_ARROW) {
125+
if (current(array_reverse($token['conditions'])) === T_CLOSURE) {
126+
continue;
127+
}
128+
122129
$this->pointerArrow = $i;
123130
continue;
124131
}

tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
<?php // lint >= 7.4
22

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-
// ];
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+
];
3737

3838
['newsletter' => [], 'campaign' => [], 'other' => []];
3939

0 commit comments

Comments
 (0)