Skip to content

Commit 03e11ba

Browse files
committed
Fixed bug #1637 : Generic.WhiteSpaceScopeIndent closure argument indenting incorrect with multi-line strings
1 parent ea18874 commit 03e11ba

File tree

7 files changed

+107
-8
lines changed

7 files changed

+107
-8
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
7878
- Fixed bug #1591 : Autoloader failing to load arbitrary files when installed_paths only set via a custom ruleset
7979
- Fixed bug #1605 : Squiz.WhiteSpace.OperatorSpacing false positive on unary minus after comment
8080
-- Thanks to Juliette Reinders Folmer for the patch
81+
- Fixed bug #1637 : Generic.WhiteSpaceScopeIndent closure argument indenting incorrect with multi-line strings
8182
</notes>
8283
<contents>
8384
<dir name="/">

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,22 @@ public function process(File $phpcsFile, $stackPtr)
590590

591591
if (isset($tokens[$scopeCloser]['scope_condition']) === true) {
592592
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true);
593+
if ($this->debug === true) {
594+
$line = $tokens[$first]['line'];
595+
$type = $tokens[$first]['type'];
596+
echo "\t* first token is $first ($type) on line $line *".PHP_EOL;
597+
}
598+
599+
while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
600+
&& $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING
601+
) {
602+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true);
603+
if ($this->debug === true) {
604+
$line = $tokens[$first]['line'];
605+
$type = $tokens[$first]['type'];
606+
echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL;
607+
}
608+
}
593609

594610
$currentIndent = ($tokens[$first]['column'] - 1);
595611
if (isset($adjustments[$first]) === true) {
@@ -1003,7 +1019,24 @@ public function process(File $phpcsFile, $stackPtr)
10031019
echo "Open $type on line $line".PHP_EOL;
10041020
}
10051021

1006-
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true);
1022+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true);
1023+
if ($this->debug === true) {
1024+
$line = $tokens[$first]['line'];
1025+
$type = $tokens[$first]['type'];
1026+
echo "\t* first token is $first ($type) on line $line *".PHP_EOL;
1027+
}
1028+
1029+
while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
1030+
&& $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING
1031+
) {
1032+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true);
1033+
if ($this->debug === true) {
1034+
$line = $tokens[$first]['line'];
1035+
$type = $tokens[$first]['type'];
1036+
echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL;
1037+
}
1038+
}
1039+
10071040
$currentIndent = (($tokens[$first]['column'] - 1) + $this->indent);
10081041
$openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition'];
10091042

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,22 @@ switch ($sContext) {
12281228
}
12291229
}
12301230

1231+
public function foo()
1232+
{
1233+
$foo('some
1234+
long description', function () {
1235+
});
1236+
1237+
$foo('some
1238+
long
1239+
description', function () {
1240+
});
1241+
1242+
$foo(
1243+
'some long description', function () {
1244+
});
1245+
}
1246+
12311247
function foo()
12321248
{
12331249
$foo = array(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,22 @@ switch ($sContext) {
12281228
}
12291229
}
12301230

1231+
public function foo()
1232+
{
1233+
$foo('some
1234+
long description', function () {
1235+
});
1236+
1237+
$foo('some
1238+
long
1239+
description', function () {
1240+
});
1241+
1242+
$foo(
1243+
'some long description', function () {
1244+
});
1245+
}
1246+
12311247
function foo()
12321248
{
12331249
$foo = array(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,22 @@ switch ($sContext) {
12281228
}
12291229
}
12301230

1231+
public function foo()
1232+
{
1233+
$foo('some
1234+
long description', function () {
1235+
});
1236+
1237+
$foo('some
1238+
long
1239+
description', function () {
1240+
});
1241+
1242+
$foo(
1243+
'some long description', function () {
1244+
});
1245+
}
1246+
12311247
function foo()
12321248
{
12331249
$foo = array(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,22 @@ switch ($sContext) {
12281228
}
12291229
}
12301230

1231+
public function foo()
1232+
{
1233+
$foo('some
1234+
long description', function () {
1235+
});
1236+
1237+
$foo('some
1238+
long
1239+
description', function () {
1240+
});
1241+
1242+
$foo(
1243+
'some long description', function () {
1244+
});
1245+
}
1246+
12311247
function foo()
12321248
{
12331249
$foo = array(

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
141141
1163 => 1,
142142
1197 => 1,
143143
1198 => 1,
144-
1231 => 1,
145-
1236 => 1,
146-
1238 => 1,
147-
1241 => 1,
148-
1245 => 1,
149-
1246 => 1,
144+
1243 => 1,
150145
1247 => 1,
151-
1248 => 1,
146+
1252 => 1,
147+
1254 => 1,
148+
1257 => 1,
149+
1261 => 1,
150+
1262 => 1,
151+
1263 => 1,
152+
1264 => 1,
152153
);
153154

154155
}//end getErrorList()

0 commit comments

Comments
 (0)