Skip to content

Commit

Permalink
Fixed bug #723 : ScopeIndent can fail when multiple array closers are…
Browse files Browse the repository at this point in the history
… on the same line
  • Loading branch information
gsherwood committed Oct 6, 2015
1 parent be47642 commit 85812a1
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

This comment has been minimized.

Copy link
@aik099

aik099 Oct 6, 2015

Contributor

The t letter is missing in $parenCloser variable name. I guess it should be named $parentCloser instead.

This comment has been minimized.

Copy link
@gsherwood

gsherwood Oct 6, 2015

Author Member

It's just an abbreviation of parenthesis closer. The token is of type T_CLOSE_PARENTHESIS.

This comment has been minimized.

Copy link
@aik099

aik099 Oct 6, 2015

Contributor

Ah, ok. I thought this was a typo.

$checkIndent = ($tokens[$first]['column'] - 1);
if (isset($adjustments[$first]) === true) {
$checkIndent += $adjustments[$first];
Expand All @@ -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--;
Expand Down Expand Up @@ -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];
Expand All @@ -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--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
</notes>
<contents>
Expand Down

0 comments on commit 85812a1

Please sign in to comment.