Skip to content

Commit cb41118

Browse files
committed
Merge branch 'allow-multiple-assignment-alignment-at-start-of-assign-token' of https://github.com/johnpbloch/PHP_CodeSniffer
2 parents 457afdf + 4e1deb8 commit cb41118

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

src/Standards/Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ class MultipleStatementAlignmentSniff implements Sniff
4747
*/
4848
public $maxPadding = 1000;
4949

50+
/**
51+
* Controls which side of the assignment token is used for alignment
52+
*
53+
* The default is to use the end of the assignemnt token:
54+
*
55+
* $test = 'Hello';
56+
* $test .= ' World';
57+
*
58+
* Setting to false reverses the alignment:
59+
*
60+
* $test = 'Hello';
61+
* $test .= 'World';
62+
*
63+
* @var boolean
64+
*/
65+
public $alignAtEndOfAssignToken = true;
66+
5067

5168
/**
5269
* Returns an array of tokens this test wants to listen for.
@@ -253,6 +270,10 @@ public function checkAlignment($phpcsFile, $stackPtr, $end=null)
253270
// padding length if they aligned with us.
254271
$varEnd = $tokens[($var + 1)]['column'];
255272
$assignLen = $tokens[$assign]['length'];
273+
if ($this->alignAtEndOfAssignToken !== true) {
274+
$assignLen = 1;
275+
}
276+
256277
if ($assign !== $stackPtr) {
257278
if ($prevAssign === null) {
258279
// Processing an inner block but no assignments found.

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,57 @@ $foofoo = new Foo([
406406

407407
$i = 0;
408408
echo "TEST: ".($i += 1)."\n";
409+
410+
// Valid
411+
$foo = 'Hello';
412+
$variable = 12;
413+
$foo .= ' World';
414+
$test = 1;
415+
$test <<= 6;
416+
417+
// Invalid
418+
$foo = 'Hello';
419+
$variable = 12;
420+
$foo .= ' World';
421+
$test = 1;
422+
$test <<= 6;
423+
424+
// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken false
425+
426+
// Valid
427+
$foo = 'Hello';
428+
$variable = 12;
429+
$foo .= ' World';
430+
$test = 1;
431+
$test <<= 6;
432+
433+
// Invalid
434+
$foo = 'Hello';
435+
$variable = 12;
436+
$foo .= ' World';
437+
$test = 1;
438+
$test <<= 6;
439+
440+
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 8
441+
442+
$one = 'one';
443+
$varonetwo = 'two';
444+
$varonetwothree = 'three';
445+
$varonetwothreefour = 'four';
446+
447+
$one = 'one';
448+
$varonetwo .= 'two';
449+
$varonetwo = 'two';
450+
$varonetwo .= 'two';
451+
$varonetwothree = 'three';
452+
$varonetwothreefour = 'four';
453+
454+
$one <<= 8;
455+
$onetwothree = 3;
456+
457+
// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken true
458+
459+
$one <<= 8;
460+
$onetwothree = 3;
461+
462+
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc.fixed

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,57 @@ $foofoo = new Foo([
406406

407407
$i = 0;
408408
echo "TEST: ".($i += 1)."\n";
409+
410+
// Valid
411+
$foo = 'Hello';
412+
$variable = 12;
413+
$foo .= ' World';
414+
$test = 1;
415+
$test <<= 6;
416+
417+
// Invalid
418+
$foo = 'Hello';
419+
$variable = 12;
420+
$foo .= ' World';
421+
$test = 1;
422+
$test <<= 6;
423+
424+
// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken false
425+
426+
// Valid
427+
$foo = 'Hello';
428+
$variable = 12;
429+
$foo .= ' World';
430+
$test = 1;
431+
$test <<= 6;
432+
433+
// Invalid
434+
$foo = 'Hello';
435+
$variable = 12;
436+
$foo .= ' World';
437+
$test = 1;
438+
$test <<= 6;
439+
440+
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 8
441+
442+
$one = 'one';
443+
$varonetwo = 'two';
444+
$varonetwothree = 'three';
445+
$varonetwothreefour = 'four';
446+
447+
$one = 'one';
448+
$varonetwo .= 'two';
449+
$varonetwo = 'two';
450+
$varonetwo .= 'two';
451+
$varonetwothree = 'three';
452+
$varonetwothreefour = 'four';
453+
454+
$one <<= 8;
455+
$onetwothree = 3;
456+
457+
// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEndOfAssignToken true
458+
459+
$one <<= 8;
460+
$onetwothree = 3;
461+
462+
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public function getWarningList($testFile='MultipleStatementAlignmentUnitTest.inc
111111
398 => 1,
112112
399 => 1,
113113
401 => 1,
114+
420 => 1,
115+
422 => 1,
116+
436 => 1,
117+
438 => 1,
118+
442 => 1,
119+
443 => 1,
120+
454 => 1,
114121
];
115122
break;
116123
case 'MultipleStatementAlignmentUnitTest.js':

0 commit comments

Comments
 (0)