Skip to content

Commit

Permalink
Merge branch 'allow-multiple-assignment-alignment-at-start-of-assign-…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Nov 2, 2020
2 parents 457afdf + 4e1deb8 commit cb41118
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ class MultipleStatementAlignmentSniff implements Sniff
*/
public $maxPadding = 1000;

/**
* Controls which side of the assignment token is used for alignment
*
* The default is to use the end of the assignemnt token:
*
* $test = 'Hello';
* $test .= ' World';
*
* Setting to false reverses the alignment:
*
* $test = 'Hello';
* $test .= 'World';
*
* @var boolean
*/
public $alignAtEndOfAssignToken = true;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -253,6 +270,10 @@ public function checkAlignment($phpcsFile, $stackPtr, $end=null)
// padding length if they aligned with us.
$varEnd = $tokens[($var + 1)]['column'];
$assignLen = $tokens[$assign]['length'];
if ($this->alignAtEndOfAssignToken !== true) {
$assignLen = 1;
}

if ($assign !== $stackPtr) {
if ($prevAssign === null) {
// Processing an inner block but no assignments found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,57 @@ $foofoo = new Foo([

$i = 0;
echo "TEST: ".($i += 1)."\n";

// Valid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// Invalid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken false

// Valid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// Invalid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 8

$one = 'one';
$varonetwo = 'two';
$varonetwothree = 'three';
$varonetwothreefour = 'four';

$one = 'one';
$varonetwo .= 'two';
$varonetwo = 'two';
$varonetwo .= 'two';
$varonetwothree = 'three';
$varonetwothreefour = 'four';

$one <<= 8;
$onetwothree = 3;

// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken true

$one <<= 8;
$onetwothree = 3;

// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,57 @@ $foofoo = new Foo([

$i = 0;
echo "TEST: ".($i += 1)."\n";

// Valid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// Invalid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken false

// Valid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// Invalid
$foo = 'Hello';
$variable = 12;
$foo .= ' World';
$test = 1;
$test <<= 6;

// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 8

$one = 'one';
$varonetwo = 'two';
$varonetwothree = 'three';
$varonetwothreefour = 'four';

$one = 'one';
$varonetwo .= 'two';
$varonetwo = 'two';
$varonetwo .= 'two';
$varonetwothree = 'three';
$varonetwothreefour = 'four';

$one <<= 8;
$onetwothree = 3;

// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken true

$one <<= 8;
$onetwothree = 3;

// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public function getWarningList($testFile='MultipleStatementAlignmentUnitTest.inc
398 => 1,
399 => 1,
401 => 1,
420 => 1,
422 => 1,
436 => 1,
438 => 1,
442 => 1,
443 => 1,
454 => 1,
];
break;
case 'MultipleStatementAlignmentUnitTest.js':
Expand Down

0 comments on commit cb41118

Please sign in to comment.