Skip to content

Commit

Permalink
Merge branch 'multilevel-indenting' of https://github.com/marcospasso…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Sep 22, 2019
2 parents fb04e08 + 09c6f75 commit 83de998
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 18 deletions.
4 changes: 4 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -121,23 +130,35 @@ 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 {
$phpcsFile->fixer->replaceToken(($next - 1), $spaces);
}
}
}

$previousIndent = $expectedIndent;
}//end if

// It cant be the last thing on the line either.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 83de998

Please sign in to comment.