Skip to content

Commit

Permalink
Slevomat.Arrays.DisallowPartiallyKeyed: Fixed false positive with arr…
Browse files Browse the repository at this point in the history
…ay unpacking
  • Loading branch information
kukulich committed Apr 21, 2023
1 parent 43d5d8c commit 91428d5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
15 changes: 10 additions & 5 deletions SlevomatCodingStandard/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static function isKeyed(array $keyValues): bool
public static function isKeyedAll(array $keyValues): bool
{
foreach ($keyValues as $keyValue) {
if ($keyValue->getKey() === null) {
if (!$keyValue->isUnpacking() && $keyValue->getKey() === null) {
return false;
}
}
Expand Down Expand Up @@ -187,14 +187,19 @@ public static function isNotEmpty(File $phpcsFile, int $pointer): bool
*/
public static function isSortedByKey(array $keyValues): bool
{
$prev = '';
$previousKey = '';
foreach ($keyValues as $keyValue) {
$cmp = strnatcasecmp((string) $prev, (string) $keyValue->getKey());
if ($cmp === 1) {
if ($keyValue->isUnpacking()) {
continue;
}

if (strnatcasecmp($previousKey, $keyValue->getKey()) === 1) {
return false;
}
$prev = $keyValue->getKey();

$previousKey = $keyValue->getKey();
}

return true;
}

Expand Down
11 changes: 11 additions & 0 deletions SlevomatCodingStandard/Helpers/ArrayKeyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function trim;
use const T_COMMA;
use const T_DOUBLE_ARROW;
use const T_ELLIPSIS;
use const T_WHITESPACE;

/**
Expand All @@ -36,6 +37,9 @@ class ArrayKeyValue
/** @var ?int */
private $pointerComma = null;

/** @var bool */
private $unpacking = false;

public function __construct(File $phpcsFile, int $pointerStart, int $pointerEnd)
{
$this->pointerStart = $pointerStart;
Expand Down Expand Up @@ -102,6 +106,11 @@ public function getPointerStart(): int
return $this->pointerStart;
}

public function isUnpacking(): bool
{
return $this->unpacking;
}

private function addValues(File $phpcsFile): void
{
$key = '';
Expand All @@ -119,6 +128,8 @@ private function addValues(File $phpcsFile): void
$this->pointerArrow = $pointer;
} elseif ($token['code'] === T_COMMA) {
$this->pointerComma = $pointer;
} elseif ($token['code'] === T_ELLIPSIS) {
$this->unpacking = true;
} elseif ($this->pointerArrow === null) {
if ($firstNonWhitespace === null && $token['code'] !== T_WHITESPACE) {
$firstNonWhitespace = $pointer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function log($msg)
'a4' => 'a4 val',
),
);
// @phpcs:enable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder

[
'I am',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function log($msg)
'a4' => 'a4 val',
),
);
// @phpcs:enable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder

[
'I am',
Expand Down
10 changes: 10 additions & 0 deletions tests/Sniffs/Arrays/data/alphabeticallySortedByKeysNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ public function log($msg)
'just a list',
'no keys',
];

$a = [];
$b = [];

[
'b' => 'b',
...$b,
'a' => 'a',
...$a
];
8 changes: 5 additions & 3 deletions tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 7.4

[];

Expand All @@ -8,7 +8,7 @@

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

[
$a = [
0 => 'zero',
'foo' => 'foo',
'bar' => 'bar',
Expand All @@ -18,6 +18,8 @@
array(
0 => 'zero',
'foo' => 'foo',
...$a,
'bar' => 'bar',
'baz' => 'baz'
'baz' => 'baz',
...$a
);

0 comments on commit 91428d5

Please sign in to comment.