Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to allow checking assignment operators #2515

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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