Skip to content

Commit

Permalink
Fixed bug squizlabs#1109 : Wrong scope indent reported in anonymous c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
gsherwood committed Aug 11, 2016
1 parent b6dcfdd commit 71945d9
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -978,22 +978,24 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
continue;
}//end if

// Closures set the indent based on their own indent level.
if ($tokens[$i]['code'] === T_CLOSURE) {
// Anon classes and functions set the indent based on their own indent level.
if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) {
$closer = $tokens[$i]['scope_closer'];
if ($tokens[$i]['line'] === $tokens[$closer]['line']) {
if ($this->_debug === true) {
$type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2)));
$line = $tokens[$i]['line'];
echo "* ignoring single-line closure on line $line".PHP_EOL;
echo "* ignoring single-line $type on line $line".PHP_EOL;
}

$i = $closer;
continue;
}

if ($this->_debug === true) {
$type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2)));
$line = $tokens[$i]['line'];
echo "Open closure on line $line".PHP_EOL;
echo "Open $type on line $line".PHP_EOL;
}

$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true);
Expand Down Expand Up @@ -1094,14 +1096,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
continue;
}//end if

// Closing a closure.
// Closing an anon class or function.
if (isset($tokens[$i]['scope_condition']) === true
&& $tokens[$i]['scope_closer'] === $i
&& $tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
&& ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS)
) {
if ($this->_debug === true) {
$type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2)));
$line = $tokens[$i]['line'];
echo "Close closure on line $line".PHP_EOL;
echo "Close $type on line $line".PHP_EOL;
}

$prev = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
<?php
}

$foo = [
new class() implements Bar {

public static function foo(): string
{
return 'foo';
}
},
];

$foo = [
function () {
if ($foo) {
return 'foo';
}
},
];

$foo = foo(
function () {
$foo->debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
<?php
}

$foo = [
new class() implements Bar {

public static function foo(): string
{
return 'foo';
}
},
];

$foo = [
function () {
if ($foo) {
return 'foo';
}
},
];

$foo = foo(
function () {
$foo->debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
<?php
}

$foo = [
new class() implements Bar {

public static function foo(): string
{
return 'foo';
}
},
];

$foo = [
function () {
if ($foo) {
return 'foo';
}
},
];

$foo = foo(
function () {
$foo->debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
<?php
}

$foo = [
new class() implements Bar {

public static function foo(): string
{
return 'foo';
}
},
];

$foo = [
function () {
if ($foo) {
return 'foo';
}
},
];

$foo = foo(
function () {
$foo->debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
823 => 1,
858 => 1,
879 => 1,
1125 => 1,
1133 => 1,
1138 => 1,
1140 => 1,
1143 => 1,
1151 => 1,
1156 => 1,
1158 => 1,
1161 => 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 @@ -90,6 +90,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Jason McCreary for the patch
- Fixed bug #1101 : Incorrect indent errors when breaking out of PHP inside an IF statement
- Fixed bug #1102 : Squiz.Formatting.OperatorBracket.MissingBrackets faulty bracketing fix
- Fixed bug #1109 : Wrong scope indent reported in anonymous class
- Fixed bug #1112 : File docblock not recognized when require_once follows it
</notes>
<contents>
Expand Down

0 comments on commit 71945d9

Please sign in to comment.