Skip to content

Commit 460f212

Browse files
committed
PHP 8.0 | PEAR/ObjectOperatorIndent: sniff for nullsafe object operator
This adds sniffing for the indentation of nullsafe object operators to the sniff. Includes unit test.
1 parent 1371c59 commit 460f212

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class ObjectOperatorIndentSniff implements Sniff
2929
*/
3030
public $multilevel = false;
3131

32+
/**
33+
* Tokens to listen for.
34+
*
35+
* @var array
36+
*/
37+
private $targets = [
38+
T_OBJECT_OPERATOR,
39+
T_NULLSAFE_OBJECT_OPERATOR,
40+
];
41+
3242

3343
/**
3444
* Returns an array of tokens this test wants to listen for.
@@ -37,7 +47,7 @@ class ObjectOperatorIndentSniff implements Sniff
3747
*/
3848
public function register()
3949
{
40-
return [T_OBJECT_OPERATOR];
50+
return $this->targets;
4151

4252
}//end register()
4353

@@ -57,14 +67,14 @@ public function process(File $phpcsFile, $stackPtr)
5767

5868
// Make sure this is the first object operator in a chain of them.
5969
$start = $phpcsFile->findStartOfStatement($stackPtr);
60-
$prev = $phpcsFile->findPrevious(T_OBJECT_OPERATOR, ($stackPtr - 1), $start);
70+
$prev = $phpcsFile->findPrevious($this->targets, ($stackPtr - 1), $start);
6171
if ($prev !== false) {
6272
return;
6373
}
6474

6575
// Make sure this is a chained call.
6676
$end = $phpcsFile->findEndOfStatement($stackPtr);
67-
$next = $phpcsFile->findNext(T_OBJECT_OPERATOR, ($stackPtr + 1), $end);
77+
$next = $phpcsFile->findNext($this->targets, ($stackPtr + 1), $end);
6878
if ($next === false) {
6979
// Not a chained call.
7080
return;
@@ -179,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr)
179189
}//end if
180190

181191
$next = $phpcsFile->findNext(
182-
T_OBJECT_OPERATOR,
192+
$this->targets,
183193
($next + 1),
184194
null,
185195
false,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,27 @@ $rootNode
110110
->five();
111111

112112
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false
113+
114+
$object
115+
?->setBar($foo)
116+
?->setFoo($bar);
117+
118+
$someObject?->someFunction("some", "parameter")
119+
->someOtherFunc(23, 42)?->
120+
someOtherFunc2($one, $two)
121+
122+
->someOtherFunc3(23, 42)
123+
?->andAThirdFunction();
124+
125+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true
126+
$object
127+
?->setBar($foo)
128+
?->setFoo($bar);
129+
130+
$someObject?->someFunction("some", "parameter")
131+
->someOtherFunc(23, 42)
132+
?->someOtherFunc2($one, $two)
133+
134+
->someOtherFunc3(23, 42)
135+
?->andAThirdFunction();
136+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,27 @@ $rootNode
110110
->five();
111111

112112
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false
113+
114+
$object
115+
?->setBar($foo)
116+
?->setFoo($bar);
117+
118+
$someObject?->someFunction("some", "parameter")
119+
->someOtherFunc(23, 42)
120+
?->someOtherFunc2($one, $two)
121+
122+
->someOtherFunc3(23, 42)
123+
?->andAThirdFunction();
124+
125+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true
126+
$object
127+
?->setBar($foo)
128+
?->setFoo($bar);
129+
130+
$someObject?->someFunction("some", "parameter")
131+
->someOtherFunc(23, 42)
132+
?->someOtherFunc2($one, $two)
133+
134+
->someOtherFunc3(23, 42)
135+
?->andAThirdFunction();
136+
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function getErrorList()
4444
82 => 1,
4545
95 => 1,
4646
103 => 1,
47+
119 => 2,
48+
122 => 1,
49+
131 => 1,
50+
134 => 1,
4751
];
4852

4953
}//end getErrorList()

0 commit comments

Comments
 (0)