From 460f212c4539dc93de9980847dfcb0735c9be110 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 1 Aug 2020 01:50:44 +0200 Subject: [PATCH] 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. --- .../WhiteSpace/ObjectOperatorIndentSniff.php | 18 ++++++++++---- .../ObjectOperatorIndentUnitTest.inc | 24 +++++++++++++++++++ .../ObjectOperatorIndentUnitTest.inc.fixed | 24 +++++++++++++++++++ .../ObjectOperatorIndentUnitTest.php | 4 ++++ 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php b/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php index 1929b3bbe8..fb1b79a329 100644 --- a/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php +++ b/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php @@ -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. @@ -37,7 +47,7 @@ class ObjectOperatorIndentSniff implements Sniff */ public function register() { - return [T_OBJECT_OPERATOR]; + return $this->targets; }//end register() @@ -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; @@ -179,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr) }//end if $next = $phpcsFile->findNext( - T_OBJECT_OPERATOR, + $this->targets, ($next + 1), null, false, diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc index 70fd3d8ccb..2f3f991b0a 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc @@ -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 diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed index dfa642f46d..1073c9c01e 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed @@ -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 diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php index 0ae7b72ef4..8289b0f564 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php @@ -44,6 +44,10 @@ public function getErrorList() 82 => 1, 95 => 1, 103 => 1, + 119 => 2, + 122 => 1, + 131 => 1, + 134 => 1, ]; }//end getErrorList()