Skip to content

Commit 8f843c3

Browse files
committed
Add support for multiline indenting to ObjectOperatorIndentSniff
1 parent f693a3f commit 8f843c3

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class ObjectOperatorIndentSniff implements Sniff
2222
*/
2323
public $indent = 4;
2424

25+
/**
26+
* Indicates whether multilevel indenting is allowed.
27+
*
28+
* @var boolean
29+
*/
30+
public $multilevel = false;
31+
2532

2633
/**
2734
* Returns an array of tokens this test wants to listen for.
@@ -71,12 +78,12 @@ public function process(File $phpcsFile, $stackPtr)
7178
}
7279
}
7380

74-
$requiredIndent = 0;
75-
if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) {
76-
$requiredIndent = strlen($tokens[$i]['content']);
81+
$baseIndent = 0;
82+
if ($i >= 0 && $tokens[$i]['code'] === \T_WHITESPACE) {
83+
$baseIndent = \strlen($tokens[$i]['content']);
7784
}
7885

79-
$requiredIndent += $this->indent;
86+
$baseIndent += $this->indent;
8087

8188
// Determine the scope of the original object operator.
8289
$origBrackets = null;
@@ -96,6 +103,8 @@ public function process(File $phpcsFile, $stackPtr)
96103
$next = $stackPtr;
97104
}
98105

106+
$previousIndent = $baseIndent;
107+
99108
while ($next !== false) {
100109
// Make sure it is in the same scope, otherwise don't check indent.
101110
$brackets = null;
@@ -121,22 +130,34 @@ public function process(File $phpcsFile, $stackPtr)
121130
$foundIndent = 0;
122131
}
123132

124-
if ($foundIndent !== $requiredIndent) {
133+
$minIndent = $previousIndent;
134+
$maxIndent = $previousIndent;
135+
$expectedIndent = $previousIndent;
136+
137+
if ($this->multilevel === true) {
138+
$minIndent = \max(($previousIndent - $this->indent), $baseIndent);
139+
$maxIndent = ($previousIndent + $this->indent);
140+
$expectedIndent = \min(\max($foundIndent, $minIndent), $maxIndent);
141+
}
142+
143+
if ($foundIndent < $minIndent || $foundIndent > $maxIndent) {
125144
$error = 'Object operator not indented correctly; expected %s spaces but found %s';
126145
$data = [
127-
$requiredIndent,
146+
$expectedIndent,
128147
$foundIndent,
129148
];
130149

131150
$fix = $phpcsFile->addFixableError($error, $next, 'Incorrect', $data);
132151
if ($fix === true) {
133-
$spaces = str_repeat(' ', $requiredIndent);
152+
$spaces = str_repeat(' ', $expectedIndent);
134153
if ($foundIndent === 0) {
135154
$phpcsFile->fixer->addContentBefore($next, $spaces);
136155
} else {
137156
$phpcsFile->fixer->replaceToken(($next - 1), $spaces);
138157
}
139158
}
159+
} else {
160+
$previousIndent = $foundIndent;
140161
}
141162
}//end if
142163

src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,12 @@ someclass::one()
7171
(new someclass())->one()
7272
->two()
7373
->three();
74+
75+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true
76+
77+
$someObject
78+
->startSomething()
79+
->someOtherFunc(23, 42)
80+
->endSomething()
81+
->doSomething(23, 42)
82+
->endEverything();

src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,12 @@ someclass::one()
7171
(new someclass())->one()
7272
->two()
7373
->three();
74+
75+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true
76+
77+
$someObject
78+
->startSomething()
79+
->someOtherFunc(23, 42)
80+
->endSomething()
81+
->doSomething(23, 42)
82+
->endEverything();

src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public function getErrorList()
3838
65 => 1,
3939
69 => 1,
4040
73 => 1,
41+
79 => 1,
42+
80 => 1,
43+
81 => 1,
44+
82 => 1,
4145
];
4246

4347
}//end getErrorList()

0 commit comments

Comments
 (0)