Skip to content

Commit 71945d9

Browse files
committed
Fixed bug #1109 : Wrong scope indent reported in anonymous class
1 parent b6dcfdd commit 71945d9

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -978,22 +978,24 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
978978
continue;
979979
}//end if
980980

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

990991
$i = $closer;
991992
continue;
992993
}
993994

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

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

1097-
// Closing a closure.
1099+
// Closing an anon class or function.
10981100
if (isset($tokens[$i]['scope_condition']) === true
10991101
&& $tokens[$i]['scope_closer'] === $i
1100-
&& $tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
1102+
&& ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
1103+
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS)
11011104
) {
11021105
if ($this->_debug === true) {
1106+
$type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2)));
11031107
$line = $tokens[$i]['line'];
1104-
echo "Close closure on line $line".PHP_EOL;
1108+
echo "Close $type on line $line".PHP_EOL;
11051109
}
11061110

11071111
$prev = false;

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
11171117
<?php
11181118
}
11191119

1120+
$foo = [
1121+
new class() implements Bar {
1122+
1123+
public static function foo(): string
1124+
{
1125+
return 'foo';
1126+
}
1127+
},
1128+
];
1129+
1130+
$foo = [
1131+
function () {
1132+
if ($foo) {
1133+
return 'foo';
1134+
}
1135+
},
1136+
];
1137+
11201138
$foo = foo(
11211139
function () {
11221140
$foo->debug(

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
11171117
<?php
11181118
}
11191119

1120+
$foo = [
1121+
new class() implements Bar {
1122+
1123+
public static function foo(): string
1124+
{
1125+
return 'foo';
1126+
}
1127+
},
1128+
];
1129+
1130+
$foo = [
1131+
function () {
1132+
if ($foo) {
1133+
return 'foo';
1134+
}
1135+
},
1136+
];
1137+
11201138
$foo = foo(
11211139
function () {
11221140
$foo->debug(

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
11171117
<?php
11181118
}
11191119

1120+
$foo = [
1121+
new class() implements Bar {
1122+
1123+
public static function foo(): string
1124+
{
1125+
return 'foo';
1126+
}
1127+
},
1128+
];
1129+
1130+
$foo = [
1131+
function () {
1132+
if ($foo) {
1133+
return 'foo';
1134+
}
1135+
},
1136+
];
1137+
11201138
$foo = foo(
11211139
function () {
11221140
$foo->debug(

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,24 @@ if (somethingIsTrue()) {
11171117
<?php
11181118
}
11191119

1120+
$foo = [
1121+
new class() implements Bar {
1122+
1123+
public static function foo(): string
1124+
{
1125+
return 'foo';
1126+
}
1127+
},
1128+
];
1129+
1130+
$foo = [
1131+
function () {
1132+
if ($foo) {
1133+
return 'foo';
1134+
}
1135+
},
1136+
];
1137+
11201138
$foo = foo(
11211139
function () {
11221140
$foo->debug(

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
154154
823 => 1,
155155
858 => 1,
156156
879 => 1,
157-
1125 => 1,
158-
1133 => 1,
159-
1138 => 1,
160-
1140 => 1,
161157
1143 => 1,
158+
1151 => 1,
159+
1156 => 1,
160+
1158 => 1,
161+
1161 => 1,
162162
);
163163

164164
}//end getErrorList()

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
9090
-- Thanks to Jason McCreary for the patch
9191
- Fixed bug #1101 : Incorrect indent errors when breaking out of PHP inside an IF statement
9292
- Fixed bug #1102 : Squiz.Formatting.OperatorBracket.MissingBrackets faulty bracketing fix
93+
- Fixed bug #1109 : Wrong scope indent reported in anonymous class
9394
- Fixed bug #1112 : File docblock not recognized when require_once follows it
9495
</notes>
9596
<contents>

0 commit comments

Comments
 (0)