Skip to content

Commit

Permalink
Add option to allow checking assignment operators
Browse files Browse the repository at this point in the history
  • Loading branch information
grongor committed May 23, 2019
1 parent 100a81c commit e1aaf39
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class OperatorSpacingSniff implements Sniff
*/
public $ignoreNewlines = false;

/**
* Allow multiple assignment statements to be aligned (don't check space before assignment operator)
*
* @var boolean
*/
public $allowMultipleStatementsAlignment = true;


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

$phpcsFile->recordMetric($stackPtr, 'Space before operator', 0);
} else if (isset(Tokens::$assignmentTokens[$tokens[$stackPtr]['code']]) === false) {
// Don't throw an error for assignments, because other standards allow
// multiple spaces there to align multiple assignments.
} else if (isset(Tokens::$assignmentTokens[$tokens[$stackPtr]['code']]) === false
|| $this->allowMultipleStatementsAlignment === false
) {
// Throw an error for assignments only if that behaviour is enabled using the sniff property,
// because other standards allow multiple spaces there to align multiple assignments.
if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) {
$found = 'newline';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,6 @@ function bar(): array {}
if ($line{-1} === ':') {
$line = substr($line, 0, -1);
}

// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
$a = 3;
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,6 @@ function bar(): array {}
if ($line{-1} === ':') {
$line = substr($line, 0, -1);
}

// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
$a = 3;
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ x = x >>> y;
x >>>= y;

var foo = bar.map(baz=> baz.length);

// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
a = 3;
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ x = x >>> y;
x >>>= y;

var foo = bar.map(baz => baz.length);

// phpcs:set Squiz.WhiteSpace.OperatorSpacing allowMultipleStatementsAlignment false
a = 3;
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function getErrorList($testFile='OperatorSpacingUnitTest.inc')
201 => 2,
239 => 1,
246 => 1,
265 => 2,
];
break;
case 'OperatorSpacingUnitTest.js':
Expand Down Expand Up @@ -138,6 +139,7 @@ public function getErrorList($testFile='OperatorSpacingUnitTest.inc')
73 => 1,
74 => 1,
100 => 1,
103 => 2,
];
break;
default:
Expand Down

0 comments on commit e1aaf39

Please sign in to comment.