From 85812a13682a34f971a6ee4eb544d224bd4bff77 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Tue, 6 Oct 2015 16:09:16 +1100 Subject: [PATCH] Fixed bug #723 : ScopeIndent can fail when multiple array closers are on the same line --- .../Sniffs/WhiteSpace/ScopeIndentSniff.php | 35 ++++++++++++++----- .../WhiteSpace/ScopeIndentUnitTest.1.inc | 18 ++++++++++ .../ScopeIndentUnitTest.1.inc.fixed | 18 ++++++++++ .../WhiteSpace/ScopeIndentUnitTest.2.inc | 18 ++++++++++ .../ScopeIndentUnitTest.2.inc.fixed | 18 ++++++++++ .../Tests/WhiteSpace/ScopeIndentUnitTest.php | 10 +++--- package.xml | 1 + 7 files changed, 104 insertions(+), 14 deletions(-) diff --git a/CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php b/CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php index aa817ac279..a31d84d6cf 100644 --- a/CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php +++ b/CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php @@ -226,16 +226,26 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Closing parenthesis should just be indented to at least // the same level as where they were opened (but can be more). - if ($checkToken !== null + if (($checkToken !== null && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS - && isset($tokens[$checkToken]['parenthesis_opener']) === true + && isset($tokens[$checkToken]['parenthesis_opener']) === true) + || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS + && isset($tokens[$i]['parenthesis_opener']) === true + && isset($tokens[$i]['parenthesis_owner']) === true + && $tokens[$tokens[$i]['parenthesis_owner']]['code'] === T_ARRAY) ) { + if ($checkToken !== null) { + $parenCloser = $checkToken; + } else { + $parenCloser = $i; + } + if ($this->_debug === true) { $line = $tokens[$i]['line']; echo "Closing parenthesis found on line $line".PHP_EOL; } - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$checkToken]['parenthesis_opener'], true); + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$parenCloser]['parenthesis_opener'], true); $checkIndent = ($tokens[$first]['column'] - 1); if (isset($adjustments[$first]) === true) { $checkIndent += $adjustments[$first]; @@ -249,7 +259,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) echo "\t* first token on line $line is $type *".PHP_EOL; } - if ($first === $tokens[$checkToken]['parenthesis_opener']) { + if ($first === $tokens[$parenCloser]['parenthesis_opener']) { // This is unlikely to be the start of the statement, so look // back further to find it. $first--; @@ -288,15 +298,22 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Closing short array bracket should just be indented to at least // the same level as where it was opened (but can be more). - if ($checkToken !== null - && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY + if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY + || ($checkToken !== null + && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) ) { + if ($checkToken !== null) { + $arrayCloser = $checkToken; + } else { + $arrayCloser = $i; + } + if ($this->_debug === true) { - $line = $tokens[$i]['line']; + $line = $tokens[$arrayCloser]['line']; echo "Closing short array bracket found on line $line".PHP_EOL; } - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$checkToken]['bracket_opener'], true); + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$arrayCloser]['bracket_opener'], true); $checkIndent = ($tokens[$first]['column'] - 1); if (isset($adjustments[$first]) === true) { $checkIndent += $adjustments[$first]; @@ -310,7 +327,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) echo "\t* first token on line $line is $type *".PHP_EOL; } - if ($first === $tokens[$checkToken]['bracket_opener']) { + if ($first === $tokens[$arrayCloser]['bracket_opener']) { // This is unlikely to be the start of the statement, so look // back further to find it. $first--; diff --git a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc index 61f76bef5f..6bb1fc9b55 100644 --- a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc +++ b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc @@ -891,6 +891,24 @@ $a = ]; $c = 2; +class foo +{ + public function get() + { + $foo = ['b' => 'c', + 'd' => [ + ['e' => 'f'] + ]]; + echo '42'; + + $foo = array('b' => 'c', + 'd' => array( + array('e' => 'f') + )); + echo '42'; + } +} + function foo() { $foo = array( diff --git a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed index 9cc50196ff..02aefb5aee 100644 --- a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed +++ b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed @@ -891,6 +891,24 @@ $a = ]; $c = 2; +class foo +{ + public function get() + { + $foo = ['b' => 'c', + 'd' => [ + ['e' => 'f'] + ]]; + echo '42'; + + $foo = array('b' => 'c', + 'd' => array( + array('e' => 'f') + )); + echo '42'; + } +} + function foo() { $foo = array( diff --git a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc index 07bd411138..01b8486628 100644 --- a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc +++ b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc @@ -891,6 +891,24 @@ $a = ]; $c = 2; +class foo +{ + public function get() + { + $foo = ['b' => 'c', + 'd' => [ + ['e' => 'f'] + ]]; + echo '42'; + + $foo = array('b' => 'c', + 'd' => array( + array('e' => 'f') + )); + echo '42'; + } +} + function foo() { $foo = array( diff --git a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed index bf1ab4d4da..d1e0e3547b 100644 --- a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed +++ b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed @@ -891,6 +891,24 @@ $a = ]; $c = 2; +class foo +{ + public function get() + { + $foo = ['b' => 'c', + 'd' => [ + ['e' => 'f'] + ]]; + echo '42'; + + $foo = array('b' => 'c', + 'd' => array( + array('e' => 'f') + )); + echo '42'; + } +} + function foo() { $foo = array( diff --git a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php index 296c2e7cc6..b132cd2ed9 100644 --- a/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php +++ b/CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php @@ -144,11 +144,11 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc') 862 => 1, 863 => 1, 879 => 1, - 894 => 1, - 906 => 1, - 907 => 1, - 909 => 1, - 911 => 1, + 912 => 1, + 924 => 1, + 925 => 1, + 927 => 1, + 929 => 1, ); }//end getErrorList() diff --git a/package.xml b/package.xml index 590e3f686e..99ef23da59 100644 --- a/package.xml +++ b/package.xml @@ -38,6 +38,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Fixed bug #711 : Sniffing from STDIN shows Generic.Files.LowercasedFilename.NotFound error - Fixed bug #714 : Fixes suppression of errors using docblocks -- Thanks to Andrzej Karmazyn for the patch + - Fixed bug #723 : ScopeIndent can fail when multiple array closers are on the same line - Fixed bug #730 : ScopeIndent can fail when a short array opening square bracket is on a line by itself