Skip to content

Commit e1aaf39

Browse files
committed
Add option to allow checking assignment operators
1 parent 100a81c commit e1aaf39

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-3
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class OperatorSpacingSniff implements Sniff
3333
*/
3434
public $ignoreNewlines = false;
3535

36+
/**
37+
* Allow multiple assignment statements to be aligned (don't check space before assignment operator)
38+
*
39+
* @var boolean
40+
*/
41+
public $allowMultipleStatementsAlignment = true;
42+
3643

3744
/**
3845
* Returns an array of tokens this test wants to listen for.
@@ -144,9 +151,11 @@ public function process(File $phpcsFile, $stackPtr)
144151
}
145152

146153
$phpcsFile->recordMetric($stackPtr, 'Space before operator', 0);
147-
} else if (isset(Tokens::$assignmentTokens[$tokens[$stackPtr]['code']]) === false) {
148-
// Don't throw an error for assignments, because other standards allow
149-
// multiple spaces there to align multiple assignments.
154+
} else if (isset(Tokens::$assignmentTokens[$tokens[$stackPtr]['code']]) === false
155+
|| $this->allowMultipleStatementsAlignment === false
156+
) {
157+
// Throw an error for assignments only if that behaviour is enabled using the sniff property,
158+
// because other standards allow multiple spaces there to align multiple assignments.
150159
if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) {
151160
$found = 'newline';
152161
} else {

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,6 @@ function bar(): array {}
260260
if ($line{-1} === ':') {
261261
$line = substr($line, 0, -1);
262262
}
263+
264+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
265+
$a = 3;

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,6 @@ function bar(): array {}
254254
if ($line{-1} === ':') {
255255
$line = substr($line, 0, -1);
256256
}
257+
258+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
259+
$a = 3;

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,6 @@ x = x >>> y;
9898
x >>>= y;
9999

100100
var foo = bar.map(baz=> baz.length);
101+
102+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
103+
a = 3;

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,6 @@ x = x >>> y;
9292
x >>>= y;
9393

9494
var foo = bar.map(baz => baz.length);
95+
96+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
97+
a = 3;

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function getErrorList($testFile='OperatorSpacingUnitTest.inc')
9696
201 => 2,
9797
239 => 1,
9898
246 => 1,
99+
265 => 2,
99100
];
100101
break;
101102
case 'OperatorSpacingUnitTest.js':
@@ -138,6 +139,7 @@ public function getErrorList($testFile='OperatorSpacingUnitTest.inc')
138139
73 => 1,
139140
74 => 1,
140141
100 => 1,
142+
103 => 2,
141143
];
142144
break;
143145
default:

0 commit comments

Comments
 (0)