Skip to content

Commit 6b4d9e5

Browse files
committed
If the opening statement is adjusted, adjust all included lines as well (ref #1793)
This only happens during fixing, which allows the OpeningIndent message to be muted and not cause other errors to be reported. But if you don't mute this error, the fixing happens properly and still ensures no auto-fixer conflicts.
1 parent 87398d2 commit 6b4d9e5

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

package.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29-
- PEAR.Functions.FunctionCallSignature now ensures the closing brace is aligned with the opening statement again
30-
-- Previously, it was enforcing positioning on an exact tab stop, like the opening brace
31-
-- This fixes an incorrect error when using the PSR2 standard with some code blocks
29+
- PEAR.Functions.FunctionCallSignature now only forces alignment at a specific tab stop while fixing
30+
-- It was enforcing this during checking, but this meant invalid errors if the OpeningIndent message was being muted
31+
-- This fixes incorrect errors when using the PSR2 standard with some code blocks
3232
- Fixed bug #1793 : PSR2 forcing exact indent for function call opening statements
3333
</notes>
3434
<contents>

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
368368
// at a tab stop. Without this, the function will be indented a further
369369
// $indent spaces to the right.
370370
$functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
371+
$adjustment = 0;
372+
371373
if ($foundFunctionIndent !== $functionIndent) {
372374
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
373375
$data = [
@@ -377,7 +379,8 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
377379

378380
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
379381
if ($fix === true) {
380-
$padding = str_repeat(' ', $functionIndent);
382+
$adjustment = ($functionIndent - $foundFunctionIndent);
383+
$padding = str_repeat(' ', $functionIndent);
381384
if ($foundFunctionIndent === 0) {
382385
$phpcsFile->fixer->addContentBefore($first, $padding);
383386
} else {
@@ -393,7 +396,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
393396
if ($fix === true) {
394397
$phpcsFile->fixer->addContent(
395398
$openBracket,
396-
$phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent))
399+
$phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
397400
);
398401
}
399402
}
@@ -406,7 +409,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
406409
if ($fix === true) {
407410
$phpcsFile->fixer->addContentBefore(
408411
$closeBracket,
409-
$phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent))
412+
$phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
410413
);
411414
}
412415
}
@@ -477,9 +480,9 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
477480
// Closing brace needs to be indented to the same level
478481
// as the function call.
479482
$inArg = false;
480-
$expectedIndent = $foundFunctionIndent;
483+
$expectedIndent = ($foundFunctionIndent + $adjustment);
481484
} else {
482-
$expectedIndent = ($functionIndent + $this->indent);
485+
$expectedIndent = ($foundFunctionIndent + $this->indent + $adjustment);
483486
}
484487

485488
if ($tokens[$i]['code'] !== T_WHITESPACE
@@ -565,7 +568,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
565568

566569
$phpcsFile->fixer->addContentBefore(
567570
$next,
568-
$phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent))
571+
$phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
569572
);
570573
$phpcsFile->fixer->endChangeset();
571574
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
100100
354 => 1,
101101
355 => 2,
102102
377 => 1,
103+
378 => 1,
104+
379 => 1,
103105
380 => 1,
104106
385 => 1,
107+
386 => 1,
108+
387 => 1,
105109
388 => 1,
106110
393 => 1,
107111
394 => 1,

0 commit comments

Comments
 (0)