Skip to content

Commit 83de998

Browse files
committed
Merge branch 'multilevel-indenting' of https://github.com/marcospassos/PHP_CodeSniffer
2 parents fb04e08 + 09c6f75 commit 83de998

File tree

5 files changed

+127
-18
lines changed

5 files changed

+127
-18
lines changed

package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
119119
- Generic.WhiteSpace.ScopeIndent no longer performs exact indents checking for chained method calls
120120
-- Other sniffs can be used to enforce chained method call indent rules
121121
-- Thanks to Pieter Frenssen for the patch
122+
- PEAR.WhiteSpace.ObjectOperatorIndent now supports multi-line chained statements
123+
-- When enabled, chained calls must be indented 1 level more or less than the previous line
124+
-- Set the new "multiline" setting to TRUE in a ruleset.xml file to enable this behaviour
125+
-- Thanks to Marcos Passos for the patch
122126
- PSR2.ControlStructures.ControlStructureSpacing now allows whitespace after the opening parenthesis if followed by a comment
123127
-- Thanks to Michał Bundyra for the patch
124128
- PSR2.Classes.PropertyDeclaration now enforces a single space after a property type keyword

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

Lines changed: 27 additions & 6 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;
81+
$baseIndent = 0;
7582
if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) {
76-
$requiredIndent = $tokens[$i]['length'];
83+
$baseIndent = $tokens[$i]['length'];
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,23 +130,35 @@ 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
}
140159
}
160+
161+
$previousIndent = $expectedIndent;
141162
}//end if
142163

143164
// It cant be the last thing on the line either.

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,42 @@ 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();
83+
84+
$rootNode
85+
->one()
86+
->two()
87+
->three()
88+
->four()
89+
->five();
90+
91+
$rootNode
92+
->one()
93+
->two()
94+
->three()
95+
->four()
96+
->five();
97+
98+
$rootNode
99+
->one()
100+
->two()
101+
->three()
102+
->four()
103+
->five();
104+
105+
$rootNode
106+
->one()
107+
->two()
108+
->three()
109+
->four()
110+
->five();
111+
112+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,42 @@ 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();
83+
84+
$rootNode
85+
->one()
86+
->two()
87+
->three()
88+
->four()
89+
->five();
90+
91+
$rootNode
92+
->one()
93+
->two()
94+
->three()
95+
->four()
96+
->five();
97+
98+
$rootNode
99+
->one()
100+
->two()
101+
->three()
102+
->four()
103+
->five();
104+
105+
$rootNode
106+
->one()
107+
->two()
108+
->three()
109+
->four()
110+
->five();
111+
112+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,24 @@ class ObjectOperatorIndentUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
3 => 2,
30-
6 => 1,
31-
15 => 1,
32-
27 => 1,
33-
37 => 1,
34-
38 => 1,
35-
48 => 1,
36-
49 => 1,
37-
50 => 1,
38-
65 => 1,
39-
69 => 1,
40-
73 => 1,
29+
3 => 2,
30+
6 => 1,
31+
15 => 1,
32+
27 => 1,
33+
37 => 1,
34+
38 => 1,
35+
48 => 1,
36+
49 => 1,
37+
50 => 1,
38+
65 => 1,
39+
69 => 1,
40+
73 => 1,
41+
79 => 1,
42+
80 => 1,
43+
81 => 1,
44+
82 => 1,
45+
95 => 1,
46+
103 => 1,
4147
];
4248

4349
}//end getErrorList()

0 commit comments

Comments
 (0)