diff --git a/package.xml b/package.xml index 93133c96c3..7e9598c549 100644 --- a/package.xml +++ b/package.xml @@ -119,6 +119,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Generic.WhiteSpace.ScopeIndent no longer performs exact indents checking for chained method calls -- Other sniffs can be used to enforce chained method call indent rules -- Thanks to Pieter Frenssen for the patch + - PEAR.WhiteSpace.ObjectOperatorIndent now supports multi-line chained statements + -- When enabled, chained calls must be indented 1 level more or less than the previous line + -- Set the new "multiline" setting to TRUE in a ruleset.xml file to enable this behaviour + -- Thanks to Marcos Passos for the patch - PSR2.ControlStructures.ControlStructureSpacing now allows whitespace after the opening parenthesis if followed by a comment -- Thanks to MichaƂ Bundyra for the patch - PSR2.Classes.PropertyDeclaration now enforces a single space after a property type keyword diff --git a/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php b/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php index 6a91ef08dc..621b9a2f63 100644 --- a/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php +++ b/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php @@ -22,6 +22,13 @@ class ObjectOperatorIndentSniff implements Sniff */ public $indent = 4; + /** + * Indicates whether multilevel indenting is allowed. + * + * @var boolean + */ + public $multilevel = false; + /** * Returns an array of tokens this test wants to listen for. @@ -71,12 +78,12 @@ public function process(File $phpcsFile, $stackPtr) } } - $requiredIndent = 0; + $baseIndent = 0; if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) { - $requiredIndent = $tokens[$i]['length']; + $baseIndent = $tokens[$i]['length']; } - $requiredIndent += $this->indent; + $baseIndent += $this->indent; // Determine the scope of the original object operator. $origBrackets = null; @@ -96,6 +103,8 @@ public function process(File $phpcsFile, $stackPtr) $next = $stackPtr; } + $previousIndent = $baseIndent; + while ($next !== false) { // Make sure it is in the same scope, otherwise don't check indent. $brackets = null; @@ -121,16 +130,26 @@ public function process(File $phpcsFile, $stackPtr) $foundIndent = 0; } - if ($foundIndent !== $requiredIndent) { + $minIndent = $previousIndent; + $maxIndent = $previousIndent; + $expectedIndent = $previousIndent; + + if ($this->multilevel === true) { + $minIndent = max(($previousIndent - $this->indent), $baseIndent); + $maxIndent = ($previousIndent + $this->indent); + $expectedIndent = min(max($foundIndent, $minIndent), $maxIndent); + } + + if ($foundIndent < $minIndent || $foundIndent > $maxIndent) { $error = 'Object operator not indented correctly; expected %s spaces but found %s'; $data = [ - $requiredIndent, + $expectedIndent, $foundIndent, ]; $fix = $phpcsFile->addFixableError($error, $next, 'Incorrect', $data); if ($fix === true) { - $spaces = str_repeat(' ', $requiredIndent); + $spaces = str_repeat(' ', $expectedIndent); if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($next, $spaces); } else { @@ -138,6 +157,8 @@ public function process(File $phpcsFile, $stackPtr) } } } + + $previousIndent = $expectedIndent; }//end if // It cant be the last thing on the line either. diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc index c217977e89..70fd3d8ccb 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc @@ -71,3 +71,42 @@ someclass::one() (new someclass())->one() ->two() ->three(); + +// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true + +$someObject + ->startSomething() + ->someOtherFunc(23, 42) +->endSomething() +->doSomething(23, 42) +->endEverything(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +$rootNode + ->one() + ->two() + ->three() + ->four() +->five(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed index 3e524d4655..dfa642f46d 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed @@ -71,3 +71,42 @@ someclass::one() (new someclass())->one() ->two() ->three(); + +// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true + +$someObject + ->startSomething() + ->someOtherFunc(23, 42) + ->endSomething() + ->doSomething(23, 42) + ->endEverything(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +$rootNode + ->one() + ->two() + ->three() + ->four() + ->five(); + +// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php index e850b058e1..0ae7b72ef4 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php @@ -26,18 +26,24 @@ class ObjectOperatorIndentUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 3 => 2, - 6 => 1, - 15 => 1, - 27 => 1, - 37 => 1, - 38 => 1, - 48 => 1, - 49 => 1, - 50 => 1, - 65 => 1, - 69 => 1, - 73 => 1, + 3 => 2, + 6 => 1, + 15 => 1, + 27 => 1, + 37 => 1, + 38 => 1, + 48 => 1, + 49 => 1, + 50 => 1, + 65 => 1, + 69 => 1, + 73 => 1, + 79 => 1, + 80 => 1, + 81 => 1, + 82 => 1, + 95 => 1, + 103 => 1, ]; }//end getErrorList()