Skip to content

Commit 75a23af

Browse files
committed
Add PSR2R.WhiteSpace.ArrayDeclarationSpacing
1 parent d2cdc73 commit 75a23af

File tree

5 files changed

+227
-2
lines changed

5 files changed

+227
-2
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace PSR2R\Sniffs\WhiteSpace;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PSR2R\Tools\AbstractSniff;
7+
8+
/**
9+
* Ensures that array opening brackets are on the same line as the assignment operator.
10+
* Prevents cases like:
11+
*
12+
* public $components =
13+
* [
14+
*
15+
* Should be:
16+
*
17+
* public $components = [
18+
*
19+
* @author Mark Scherer
20+
* @license MIT
21+
*/
22+
class ArrayDeclarationSpacingSniff extends AbstractSniff {
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function register(): array {
28+
return [T_OPEN_SHORT_ARRAY];
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function process(File $phpcsFile, $stackPtr) {
35+
$tokens = $phpcsFile->getTokens();
36+
37+
// Find the previous non-whitespace token
38+
$prevIndex = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
39+
if ($prevIndex === false) {
40+
return;
41+
}
42+
43+
// Check if the previous token is an assignment operator
44+
$assignmentTokens = [T_EQUAL, T_DOUBLE_ARROW];
45+
if (!in_array($tokens[$prevIndex]['code'], $assignmentTokens, true)) {
46+
return;
47+
}
48+
49+
// Check if the opening bracket is on a different line than the assignment
50+
if ($tokens[$prevIndex]['line'] === $tokens[$stackPtr]['line']) {
51+
return;
52+
}
53+
54+
// We found the issue: assignment on one line, bracket on another
55+
$fix = $phpcsFile->addFixableError(
56+
'Array opening bracket must be on the same line as the assignment operator',
57+
$stackPtr,
58+
'OpeningBracketNewline',
59+
);
60+
if (!$fix) {
61+
return;
62+
}
63+
64+
$phpcsFile->fixer->beginChangeset();
65+
66+
// Remove all whitespace between the assignment and the opening bracket
67+
for ($i = $prevIndex + 1; $i < $stackPtr; $i++) {
68+
$phpcsFile->fixer->replaceToken($i, '');
69+
}
70+
71+
// Add a single space after the assignment operator
72+
$phpcsFile->fixer->addContent($prevIndex, ' ');
73+
74+
$phpcsFile->fixer->endChangeset();
75+
}
76+
77+
}

docs/sniffs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PSR2R Code Sniffer
22

3-
The PSR2R standard contains 207 sniffs
3+
The PSR2R standard contains 208 sniffs
44

55
Generic (24 sniffs)
66
-------------------
@@ -108,7 +108,7 @@ PSR2 (6 sniffs)
108108
- PSR2.Namespaces.NamespaceDeclaration
109109
- PSR2.Namespaces.UseDeclaration
110110

111-
PSR2R (42 sniffs)
111+
PSR2R (43 sniffs)
112112
-----------------
113113
- PSR2R.Classes.BraceOnSameLine
114114
- PSR2R.Classes.InterfaceName
@@ -142,6 +142,7 @@ PSR2R (42 sniffs)
142142
- PSR2R.PHP.ListComma
143143
- PSR2R.PHP.NoShortOpenTag
144144
- PSR2R.PHP.PreferStaticOverSelf
145+
- PSR2R.WhiteSpace.ArrayDeclarationSpacing
145146
- PSR2R.WhiteSpace.ArraySpacing
146147
- PSR2R.WhiteSpace.DocBlockAlignment
147148
- PSR2R.WhiteSpace.EmptyEnclosingLine
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
* For full license information, please view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
namespace PSR2R\Test\PSR2R\Sniffs\WhiteSpace;
9+
10+
use PSR2R\Sniffs\WhiteSpace\ArrayDeclarationSpacingSniff;
11+
use PSR2R\Test\TestCase;
12+
13+
class ArrayDeclarationSpacingSniffTest extends TestCase {
14+
15+
/**
16+
* @return void
17+
*/
18+
public function testArrayDeclarationSpacingSniffer(): void {
19+
$this->assertSnifferFindsErrors(new ArrayDeclarationSpacingSniff(), 4);
20+
}
21+
22+
/**
23+
* @return void
24+
*/
25+
public function testArrayDeclarationSpacingFixer(): void {
26+
$this->assertSnifferCanFixErrors(new ArrayDeclarationSpacingSniff());
27+
}
28+
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PSR2R;
4+
5+
class ArrayDeclarationSpacingExample {
6+
7+
/**
8+
* Test case: class property array with opening bracket on new line
9+
*
10+
* @var array
11+
*/
12+
public $components = [
13+
'RequestHandler',
14+
'Flash',
15+
'Auth',
16+
];
17+
18+
/**
19+
* @var array
20+
*/
21+
protected array $helpers = [
22+
'Html',
23+
'Form',
24+
];
25+
26+
/**
27+
* Test case: variable assignment with array on new line
28+
*
29+
* @return void
30+
*/
31+
public function testVariableAssignment(): void {
32+
$config = [
33+
'debug' => true,
34+
'cache' => false,
35+
];
36+
37+
$options = [
38+
'verbose' => true,
39+
'timeout' => 30,
40+
];
41+
}
42+
43+
/**
44+
* Test case: Correct formatting (should not be flagged)
45+
*
46+
* @return void
47+
*/
48+
public function testCorrectFormatting(): void {
49+
$correct = [
50+
'a' => 'b',
51+
'c' => 'd',
52+
];
53+
54+
$alsoCorrect = ['x', 'y', 'z'];
55+
}
56+
57+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PSR2R;
4+
5+
class ArrayDeclarationSpacingExample {
6+
7+
/**
8+
* Test case: class property array with opening bracket on new line
9+
*
10+
* @var array
11+
*/
12+
public $components =
13+
[
14+
'RequestHandler',
15+
'Flash',
16+
'Auth',
17+
];
18+
19+
/**
20+
* @var array
21+
*/
22+
protected array $helpers =
23+
[
24+
'Html',
25+
'Form',
26+
];
27+
28+
/**
29+
* Test case: variable assignment with array on new line
30+
*
31+
* @return void
32+
*/
33+
public function testVariableAssignment(): void {
34+
$config =
35+
[
36+
'debug' => true,
37+
'cache' => false,
38+
];
39+
40+
$options =
41+
[
42+
'verbose' => true,
43+
'timeout' => 30,
44+
];
45+
}
46+
47+
/**
48+
* Test case: Correct formatting (should not be flagged)
49+
*
50+
* @return void
51+
*/
52+
public function testCorrectFormatting(): void {
53+
$correct = [
54+
'a' => 'b',
55+
'c' => 'd',
56+
];
57+
58+
$alsoCorrect = ['x', 'y', 'z'];
59+
}
60+
61+
}

0 commit comments

Comments
 (0)