Skip to content

Commit e2c1366

Browse files
committed
Fixed bug #813 : PEAR FunctionCallSignature checks wrong indent when first token on line is part of a multi-line string
1 parent 95298f4 commit e2c1366

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php

+29-18
Original file line numberDiff line numberDiff line change
@@ -292,29 +292,40 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr,
292292
// We need to work out how far indented the function
293293
// call itself is, so we can work out how far to
294294
// indent the arguments.
295-
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
296-
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
297-
$i++;
298-
break;
295+
$start = $phpcsFile->findStartOfStatement($stackPtr);
296+
foreach (array('stackPtr', 'start') as $checkToken) {
297+
$x = $$checkToken;
298+
for ($i = ($x - 1); $i >= 0; $i--) {
299+
if ($tokens[$i]['line'] !== $tokens[$x]['line']) {
300+
$i++;
301+
break;
302+
}
299303
}
300-
}
301304

302-
if ($i <= 0) {
303-
$functionIndent = 0;
304-
} else if ($tokens[$i]['code'] === T_WHITESPACE) {
305-
$functionIndent = strlen($tokens[$i]['content']);
306-
} else {
307-
$trimmed = ltrim($tokens[$i]['content']);
308-
if ($trimmed === '') {
309-
if ($tokens[$i]['code'] === T_INLINE_HTML) {
310-
$functionIndent = strlen($tokens[$i]['content']);
305+
if ($i <= 0) {
306+
$functionIndent = 0;
307+
} else if ($tokens[$i]['code'] === T_WHITESPACE) {
308+
$functionIndent = strlen($tokens[$i]['content']);
309+
} else if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) {
310+
$functionIndent = 0;
311+
} else {
312+
$trimmed = ltrim($tokens[$i]['content']);
313+
if ($trimmed === '') {
314+
if ($tokens[$i]['code'] === T_INLINE_HTML) {
315+
$functionIndent = strlen($tokens[$i]['content']);
316+
} else {
317+
$functionIndent = ($tokens[$i]['column'] - 1);
318+
}
311319
} else {
312-
$functionIndent = ($tokens[$i]['column'] - 1);
320+
$functionIndent = (strlen($tokens[$i]['content']) - strlen($trimmed));
313321
}
314-
} else {
315-
$functionIndent = (strlen($tokens[$i]['content']) - strlen($trimmed));
316322
}
317-
}
323+
324+
$varName = $checkToken.'Indent';
325+
$$varName = $functionIndent;
326+
}//end foreach
327+
328+
$functionIndent = max($startIndent, $stackPtrIndent);
318329

319330
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket + 1), null, true);
320331
if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc

+16
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,19 @@ $this->log(// ...
259259
]
260260
); ?>
261261
</div>
262+
263+
<?php
264+
if (true) {
265+
$test = '
266+
' . function_0(
267+
$argument_0
268+
);
269+
}
270+
271+
if (true) {
272+
$test = '
273+
' . function_0(
274+
$argument_0,
275+
$argument_1
276+
);
277+
}

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,19 @@ $this->log(// ...
264264
]
265265
); ?>
266266
</div>
267+
268+
<?php
269+
if (true) {
270+
$test = '
271+
' . function_0(
272+
$argument_0
273+
);
274+
}
275+
276+
if (true) {
277+
$test = '
278+
' . function_0(
279+
$argument_0,
280+
$argument_1
281+
);
282+
}

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
9898
194 => 1,
9999
213 => 2,
100100
215 => 2,
101+
274 => 1,
102+
275 => 1,
101103
);
102104

103105
}//end getErrorList()

package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4949
- Fixed bug #807 : Cannot fix line endings when open PHP tag is not on the first line
5050
- Fixed bug #808 : JS tokeniser incorrectly setting some function and class names to control structure tokens
5151
- Fixed bug #809 : PHPCBF can break a require_once statement with a space before the open parenthesis
52+
- Fixed bug #813 : PEAR FunctionCallSignature checks wrong indent when first token on line is part of a multi-line string
5253
</notes>
5354
<contents>
5455
<dir name="/">

0 commit comments

Comments
 (0)