Skip to content

Commit

Permalink
SingleLineArrayWhitespaceSniff: New option "enableEmptyArrayCheck"
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 1, 2020
1 parent 620c09d commit caa2482
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ Checks whitespace in single line array declarations (whitespace between brackets
Sniff provides the following settings:

* `$spacesAroundBrackets`: number of spaces you require to have around array brackets
* `$enableEmptyArrayCheck` (defaults to `false`): enables check for empty arrays

#### SlevomatCodingStandard.Arrays.TrailingArrayComma 🔧

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class SingleLineArrayWhitespaceSniff implements Sniff
/** @var int */
public $spacesAroundBrackets = 0;

/** @var bool */
public $enableEmptyArrayCheck = false;

/**
* @return array<int, (int|string)>
*/
Expand Down Expand Up @@ -53,7 +56,9 @@ public function process(File $phpcsFile, $stackPointer): int
$content = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $arrayStart + 1, $arrayEnd + 1);
if ($content === $arrayEnd) {
// Empty array, but if the brackets aren't together, there's a problem.
$this->checkWhitespaceInEmptyArray($phpcsFile, $arrayStart, $arrayEnd);
if ($this->enableEmptyArrayCheck) {
$this->checkWhitespaceInEmptyArray($phpcsFile, $arrayStart, $arrayEnd);
}

// We can return here because there is nothing else to check.
// All code below can assume that the array is not empty.
Expand Down
9 changes: 7 additions & 2 deletions tests/Sniffs/Arrays/SingleLineArrayWhitespaceSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public function testNoErrors(): void

public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/singleLineArrayWhitespaceErrors.php');
$report = self::checkFile(__DIR__ . '/data/singleLineArrayWhitespaceErrors.php', [
'enableEmptyArrayCheck' => true,
]);

self::assertSame(16, $report->getErrorCount());

Expand All @@ -40,7 +42,10 @@ public function testErrors(): void

public function testRequireSpacesErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/singleLineArrayWhitespaceRequireSpacesErrors.php', ['spacesAroundBrackets' => 1]);
$report = self::checkFile(__DIR__ . '/data/singleLineArrayWhitespaceRequireSpacesErrors.php', [
'spacesAroundBrackets' => 1,
'enableEmptyArrayCheck' => true,
]);

self::assertSame(5, $report->getErrorCount());

Expand Down

0 comments on commit caa2482

Please sign in to comment.