Skip to content

Commit

Permalink
PHP 8.0 | PEAR/ObjectOperatorIndent: sniff for nullsafe object operator
Browse files Browse the repository at this point in the history
This adds sniffing for the indentation of nullsafe object operators to the sniff.

Includes unit test.
  • Loading branch information
jrfnl committed Sep 2, 2020
1 parent 1371c59 commit 460f212
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class ObjectOperatorIndentSniff implements Sniff
*/
public $multilevel = false;

/**
* Tokens to listen for.
*
* @var array
*/
private $targets = [
T_OBJECT_OPERATOR,
T_NULLSAFE_OBJECT_OPERATOR,
];


/**
* Returns an array of tokens this test wants to listen for.
Expand All @@ -37,7 +47,7 @@ class ObjectOperatorIndentSniff implements Sniff
*/
public function register()
{
return [T_OBJECT_OPERATOR];
return $this->targets;

}//end register()

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

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

// Make sure this is a chained call.
$end = $phpcsFile->findEndOfStatement($stackPtr);
$next = $phpcsFile->findNext(T_OBJECT_OPERATOR, ($stackPtr + 1), $end);
$next = $phpcsFile->findNext($this->targets, ($stackPtr + 1), $end);
if ($next === false) {
// Not a chained call.
return;
Expand Down Expand Up @@ -179,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr)
}//end if

$next = $phpcsFile->findNext(
T_OBJECT_OPERATOR,
$this->targets,
($next + 1),
null,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,27 @@ $rootNode
->five();

// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false

$object
?->setBar($foo)
?->setFoo($bar);

$someObject?->someFunction("some", "parameter")
->someOtherFunc(23, 42)?->
someOtherFunc2($one, $two)

->someOtherFunc3(23, 42)
?->andAThirdFunction();

// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true
$object
?->setBar($foo)
?->setFoo($bar);

$someObject?->someFunction("some", "parameter")
->someOtherFunc(23, 42)
?->someOtherFunc2($one, $two)

->someOtherFunc3(23, 42)
?->andAThirdFunction();
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,27 @@ $rootNode
->five();

// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false

$object
?->setBar($foo)
?->setFoo($bar);

$someObject?->someFunction("some", "parameter")
->someOtherFunc(23, 42)
?->someOtherFunc2($one, $two)

->someOtherFunc3(23, 42)
?->andAThirdFunction();

// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel true
$object
?->setBar($foo)
?->setFoo($bar);

$someObject?->someFunction("some", "parameter")
->someOtherFunc(23, 42)
?->someOtherFunc2($one, $two)

->someOtherFunc3(23, 42)
?->andAThirdFunction();
// phpcs:set PEAR.WhiteSpace.ObjectOperatorIndent multilevel false
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function getErrorList()
82 => 1,
95 => 1,
103 => 1,
119 => 2,
122 => 1,
131 => 1,
134 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 460f212

Please sign in to comment.