Skip to content

Commit 0304844

Browse files
bkdotcomkukulich
authored andcommitted
SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed: New sniff to disallow partially keyed arrays
1 parent 861cdb0 commit 0304844

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Slevomat Coding Standard for [PHP_CodeSniffer](https://github.com/squizlabs/PHP_
3131

3232
- [SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys](doc/arrays.md#slevomatcodingstandardarrayalphabeticallysortedbykeys) 🔧
3333
- [SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation](doc/arrays.md#slevomatcodingstandardarraysdisallowimplicitarraycreation)
34+
- [SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed](doc/arrays.md#slevomatcodingstandardarraysdisallowpartiallykeyed) 🚧
3435
- [SlevomatCodingStandard.Arrays.MultiLineArrayEndBracketPlacement](doc/arrays.md#slevomatcodingstandardarraysmultilinearrayendbracketplacement-) 🔧
3536
- [SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace](doc/arrays.md#slevomatcodingstandardarrayssinglelinearraywhitespace-) 🔧
3637
- [SlevomatCodingStandard.Arrays.TrailingArrayComma](doc/arrays.md#slevomatcodingstandardarraystrailingarraycomma-) 🔧
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Arrays;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\ArrayHelper;
8+
use SlevomatCodingStandard\Helpers\TokenHelper;
9+
10+
class DisallowPartiallyKeyedSniff implements Sniff
11+
{
12+
13+
public const CODE_DISALLOWED_PARTIALLY_KEYED = 'DisallowedPartiallyKeyed';
14+
15+
/**
16+
* @return array<int, (int|string)>
17+
*/
18+
public function register(): array
19+
{
20+
return TokenHelper::$arrayTokenCodes;
21+
}
22+
23+
/**
24+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
25+
* @param int $stackPointer
26+
*/
27+
public function process(File $phpcsFile, $stackPointer): void
28+
{
29+
$keyValues = ArrayHelper::parse($phpcsFile, $stackPointer);
30+
31+
if (!ArrayHelper::isKeyed($keyValues)) {
32+
return;
33+
}
34+
35+
if (ArrayHelper::isKeyedAll($keyValues)) {
36+
return;
37+
}
38+
39+
$phpcsFile->addError('Partially keyed array disallowed.', $stackPointer, self::CODE_DISALLOWED_PARTIALLY_KEYED);
40+
}
41+
42+
}

doc/arrays.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ This sniff enforces natural sorting of array definitions by key in multi-line ar
1212

1313
Disallows implicit array creation.
1414

15+
#### SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed 🚧
16+
17+
Array must have keys specified for either all or none of the values.
18+
1519
#### SlevomatCodingStandard.Arrays.MultiLineArrayEndBracketPlacement 🔧
1620

1721
Enforces reasonable end bracket placement for multi-line arrays.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Arrays;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class DisallowPartiallyKeyedSniffTest extends TestCase
8+
{
9+
10+
public function testNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/disallowPartiallyKeyedNoErrors.php');
13+
self::assertNoSniffErrorInFile($report);
14+
}
15+
16+
public function testErrors(): void
17+
{
18+
$report = self::checkFile(__DIR__ . '/data/disallowPartiallyKeyedErrors.php', []);
19+
20+
self::assertSame(2, $report->getErrorCount());
21+
22+
self::assertSniffError($report, 3, DisallowPartiallyKeyedSniff::CODE_DISALLOWED_PARTIALLY_KEYED);
23+
self::assertSniffError($report, 10, DisallowPartiallyKeyedSniff::CODE_DISALLOWED_PARTIALLY_KEYED);
24+
}
25+
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$a = [
4+
1 => 'one',
5+
'two',
6+
4 => 'four',
7+
'five',
8+
];
9+
10+
$a = array(
11+
1 => 'one',
12+
'two',
13+
4 => 'four',
14+
'five',
15+
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
[];
4+
5+
array();
6+
7+
['foo', 'bar', 'baz'];
8+
9+
array('foo', 'bar', 'baz');
10+
11+
[
12+
0 => 'zero',
13+
'foo' => 'foo',
14+
'bar' => 'bar',
15+
'baz' => 'baz'
16+
];
17+
18+
array(
19+
0 => 'zero',
20+
'foo' => 'foo',
21+
'bar' => 'bar',
22+
'baz' => 'baz'
23+
);

0 commit comments

Comments
 (0)