From eb378acf7b632701a0304bb5837c4d2b69cc0729 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Tue, 19 Nov 2019 07:55:22 +1100 Subject: [PATCH] Fixed bug #2702 : Generic.WhiteSpace.ScopeIndent false positive when using ternary operator with short arrays The first token was being recalculated but the indent was not being modified along with that new first token. --- package.xml | 1 + .../Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php | 12 ++++++------ .../Tests/WhiteSpace/ScopeIndentUnitTest.1.inc | 6 ++++++ .../Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed | 6 ++++++ .../Tests/WhiteSpace/ScopeIndentUnitTest.2.inc | 6 ++++++ .../Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed | 6 ++++++ .../Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php | 8 ++++---- 7 files changed, 35 insertions(+), 10 deletions(-) diff --git a/package.xml b/package.xml index 812ad63803..a64a75c71b 100644 --- a/package.xml +++ b/package.xml @@ -66,6 +66,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- Thanks to Juliette Reinders Folmer for the patch - Fixed bug #2694 : AbstractArraySniff produces invalid indices when using ternary operator -- Thanks to MichaƂ Bundyra for the patch + - Fixed bug #2702 : Generic.WhiteSpace.ScopeIndent false positive when using ternary operator with short arrays diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php index 49991ae5bd..e75928c906 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php @@ -483,12 +483,7 @@ public function process(File $phpcsFile, $stackPtr) $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); - $checkIndent = ($tokens[$first]['column'] - 1); - if (isset($adjustments[$first]) === true) { - $checkIndent += $adjustments[$first]; - } - + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); $exact = false; if ($this->debug === true) { @@ -524,6 +519,11 @@ public function process(File $phpcsFile, $stackPtr) $first = $phpcsFile->findNext(T_WHITESPACE, ($first + 1), null, true); } + $checkIndent = ($tokens[$first]['column'] - 1); + if (isset($adjustments[$first]) === true) { + $checkIndent += $adjustments[$first]; + } + if (isset($tokens[$first]['scope_closer']) === true && $tokens[$first]['scope_closer'] === $first ) { diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc index 862788ed0f..2d3c95f5b5 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc @@ -1439,6 +1439,12 @@ $result = array_map( $numbers ); +$a = $a === true ? [ + 'a' => 1, + ] : [ + 'a' => 100, +]; + ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed index 94fc928340..970fd48854 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed @@ -1439,6 +1439,12 @@ $result = array_map( $numbers ); +$a = $a === true ? [ + 'a' => 1, + ] : [ + 'a' => 100, +]; + ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc index ee065d94ab..c2ed9b7258 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc @@ -1439,6 +1439,12 @@ $result = array_map( $numbers ); +$a = $a === true ? [ + 'a' => 1, + ] : [ + 'a' => 100, +]; + ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed index 825accc389..2e94f109e7 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed @@ -1439,6 +1439,12 @@ $result = array_map( $numbers ); +$a = $a === true ? [ + 'a' => 1, + ] : [ + 'a' => 100, +]; + ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php index f5b039f8b9..50d67f0bbf 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php @@ -178,10 +178,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc') 1340 => 1, 1342 => 1, 1345 => 1, - 1449 => 1, - 1450 => 1, - 1451 => 1, - 1452 => 1, + 1455 => 1, + 1456 => 1, + 1457 => 1, + 1458 => 1, ]; }//end getErrorList()