Skip to content

Commit 91428d5

Browse files
committed
Slevomat.Arrays.DisallowPartiallyKeyed: Fixed false positive with array unpacking
1 parent 43d5d8c commit 91428d5

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

SlevomatCodingStandard/Helpers/ArrayHelper.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public static function isKeyed(array $keyValues): bool
146146
public static function isKeyedAll(array $keyValues): bool
147147
{
148148
foreach ($keyValues as $keyValue) {
149-
if ($keyValue->getKey() === null) {
149+
if (!$keyValue->isUnpacking() && $keyValue->getKey() === null) {
150150
return false;
151151
}
152152
}
@@ -187,14 +187,19 @@ public static function isNotEmpty(File $phpcsFile, int $pointer): bool
187187
*/
188188
public static function isSortedByKey(array $keyValues): bool
189189
{
190-
$prev = '';
190+
$previousKey = '';
191191
foreach ($keyValues as $keyValue) {
192-
$cmp = strnatcasecmp((string) $prev, (string) $keyValue->getKey());
193-
if ($cmp === 1) {
192+
if ($keyValue->isUnpacking()) {
193+
continue;
194+
}
195+
196+
if (strnatcasecmp($previousKey, $keyValue->getKey()) === 1) {
194197
return false;
195198
}
196-
$prev = $keyValue->getKey();
199+
200+
$previousKey = $keyValue->getKey();
197201
}
202+
198203
return true;
199204
}
200205

SlevomatCodingStandard/Helpers/ArrayKeyValue.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function trim;
1111
use const T_COMMA;
1212
use const T_DOUBLE_ARROW;
13+
use const T_ELLIPSIS;
1314
use const T_WHITESPACE;
1415

1516
/**
@@ -36,6 +37,9 @@ class ArrayKeyValue
3637
/** @var ?int */
3738
private $pointerComma = null;
3839

40+
/** @var bool */
41+
private $unpacking = false;
42+
3943
public function __construct(File $phpcsFile, int $pointerStart, int $pointerEnd)
4044
{
4145
$this->pointerStart = $pointerStart;
@@ -102,6 +106,11 @@ public function getPointerStart(): int
102106
return $this->pointerStart;
103107
}
104108

109+
public function isUnpacking(): bool
110+
{
111+
return $this->unpacking;
112+
}
113+
105114
private function addValues(File $phpcsFile): void
106115
{
107116
$key = '';
@@ -119,6 +128,8 @@ private function addValues(File $phpcsFile): void
119128
$this->pointerArrow = $pointer;
120129
} elseif ($token['code'] === T_COMMA) {
121130
$this->pointerComma = $pointer;
131+
} elseif ($token['code'] === T_ELLIPSIS) {
132+
$this->unpacking = true;
122133
} elseif ($this->pointerArrow === null) {
123134
if ($firstNonWhitespace === null && $token['code'] !== T_WHITESPACE) {
124135
$firstNonWhitespace = $pointer;

tests/Sniffs/Arrays/data/alphabeticallySortedByKeysErrors.fixed.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function log($msg)
4848
'a4' => 'a4 val',
4949
),
5050
);
51+
// @phpcs:enable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
5152

5253
[
5354
'I am',

tests/Sniffs/Arrays/data/alphabeticallySortedByKeysErrors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function log($msg)
4747
'a4' => 'a4 val',
4848
),
4949
);
50+
// @phpcs:enable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
5051

5152
[
5253
'I am',

tests/Sniffs/Arrays/data/alphabeticallySortedByKeysNoErrors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ public function log($msg)
5353
'just a list',
5454
'no keys',
5555
];
56+
57+
$a = [];
58+
$b = [];
59+
60+
[
61+
'b' => 'b',
62+
...$b,
63+
'a' => 'a',
64+
...$a
65+
];

tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php

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

33
[];
44

@@ -8,7 +8,7 @@
88

99
array('foo', 'bar', 'baz');
1010

11-
[
11+
$a = [
1212
0 => 'zero',
1313
'foo' => 'foo',
1414
'bar' => 'bar',
@@ -18,6 +18,8 @@
1818
array(
1919
0 => 'zero',
2020
'foo' => 'foo',
21+
...$a,
2122
'bar' => 'bar',
22-
'baz' => 'baz'
23+
'baz' => 'baz',
24+
...$a
2325
);

0 commit comments

Comments
 (0)