Skip to content

Commit 6ff2dd3

Browse files
committed
Avoid incompatibility between two sniffs
1 parent 2d9a90d commit 6ff2dd3

10 files changed

+173
-41
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ if ($foo) {
2626
$this->foo()
2727
->bar()
2828
->baz();
29+
30+
// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
31+
// See also PSR2.Methods.FunctionCallSignature
32+
$repository->foo()
33+
->bar(
34+
function () {
35+
return true;
36+
}
37+
);
38+
$repository->foo()
39+
->bar(
40+
function () {
41+
return true;
42+
}
43+
);

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

+15
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ if ($foo) {
2626
$this->foo()
2727
->bar()
2828
->baz();
29+
30+
// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
31+
// See also PSR2.Methods.FunctionCallSignature
32+
$repository->foo()
33+
->bar(
34+
function () {
35+
return true;
36+
}
37+
);
38+
$repository->foo()
39+
->bar(
40+
function () {
41+
return true;
42+
}
43+
);

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

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
7979
6 => 1,
8080
7 => 1,
8181
10 => 1,
82+
40 => 1,
83+
41 => 1,
84+
42 => 1,
8285
];
8386
}
8487

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

+83-26
Original file line numberDiff line numberDiff line change
@@ -376,30 +376,16 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
376376
// at a tab stop. Without this, the function will be indented a further
377377
// $indent spaces to the right.
378378
$functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
379-
$adjustment = 0;
379+
$adjustment = ($functionIndent - $foundFunctionIndent);
380380

381381
if ($foundFunctionIndent !== $functionIndent) {
382-
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
383-
$data = [
382+
$this->complainOpenStatementWrongIndent(
383+
$phpcsFile,
384+
$first,
385+
$tokens,
384386
$functionIndent,
385-
$foundFunctionIndent,
386-
];
387-
388-
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
389-
if ($fix === true) {
390-
// Set adjustment for use later to determine whether argument indentation is correct when fixing.
391-
$adjustment = ($functionIndent - $foundFunctionIndent);
392-
393-
$padding = str_repeat(' ', $functionIndent);
394-
if ($foundFunctionIndent === 0) {
395-
$phpcsFile->fixer->addContentBefore($first, $padding);
396-
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
397-
$newContent = $padding.ltrim($tokens[$first]['content']);
398-
$phpcsFile->fixer->replaceToken($first, $newContent);
399-
} else {
400-
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
401-
}
402-
}
387+
$foundFunctionIndent
388+
);
403389
}//end if
404390

405391
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
@@ -465,7 +451,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
465451
$i--;
466452
}
467453

468-
for ($i; $i < $closeBracket; $i++) {
454+
for (; $i < $closeBracket; $i++) {
469455
if ($i > $argStart && $i < $argEnd) {
470456
$inArg = true;
471457
} else {
@@ -545,10 +531,34 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
545531
$foundIndent = $tokens[$i]['length'];
546532
}
547533

548-
if ($foundIndent < $expectedIndent
549-
|| ($inArg === false
550-
&& $expectedIndent !== $foundIndent)
551-
) {
534+
$indentCorrect = true;
535+
536+
if ($foundIndent < $expectedIndent) {
537+
$indentCorrect = false;
538+
} else if ($inArg === false && $expectedIndent !== $foundIndent) {
539+
$indentCorrect = false;
540+
541+
// It is permitted to indent chains further than one tab stop to
542+
// align vertically with the previous method call.
543+
if ($i === ($closeBracket - 1)) {
544+
if ($foundIndent === $foundFunctionIndent) {
545+
// This is the closing paren; it lines up vertically with the opening paren.
546+
$indentCorrect = true;
547+
}
548+
} else {
549+
if ($foundIndent === ($tokens[$openBracket]['column'] - 1)) {
550+
// This is a parameter; it lines up vertically with the opening paren.
551+
$indentCorrect = true;
552+
}
553+
554+
if ($foundIndent === ($foundFunctionIndent + ($this->indent))) {
555+
// This is a parameter; it is indented one more step than the function call around it.
556+
$indentCorrect = true;
557+
}
558+
}
559+
}//end if
560+
561+
if ($indentCorrect === false) {
552562
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
553563
$data = [
554564
$expectedIndent,
@@ -631,4 +641,51 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
631641
}//end processMultiLineCall()
632642

633643

644+
/**
645+
* Add a complaint (and auto-fix) if the function indent is 'wrong'.
646+
*
647+
* @param File $phpcsFile The file being scanned.
648+
* @param int $first Pointer to the first empty token on this line.
649+
* @param array $tokens The stack of tokens that make up the file.
650+
* @param int $functionIndent The expected indent for this function definition.
651+
* @param int $foundFunctionIndent The actual indent for this function definition.
652+
*
653+
* @return void
654+
*/
655+
protected function complainOpenStatementWrongIndent(
656+
$phpcsFile,
657+
$first,
658+
$tokens,
659+
$functionIndent,
660+
$foundFunctionIndent
661+
) {
662+
if ($foundFunctionIndent === $functionIndent) {
663+
return;
664+
}
665+
666+
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
667+
$data = [
668+
$functionIndent,
669+
$foundFunctionIndent,
670+
];
671+
672+
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
673+
if ($fix !== true) {
674+
return;
675+
}
676+
677+
$padding = str_repeat(' ', $functionIndent);
678+
679+
if ($foundFunctionIndent === 0) {
680+
$phpcsFile->fixer->addContentBefore($first, $padding);
681+
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
682+
$newContent = $padding.ltrim($tokens[$first]['content']);
683+
$phpcsFile->fixer->replaceToken($first, $newContent);
684+
} else {
685+
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
686+
}
687+
688+
}//end complainOpenStatementWrongIndent()
689+
690+
634691
}//end class

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

+2-9
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
6969
82 => 1,
7070
93 => 1,
7171
100 => 1,
72-
106 => 2,
72+
106 => 1,
7373
119 => 1,
7474
120 => 1,
7575
129 => 1,
@@ -98,15 +98,9 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
9898
346 => 2,
9999
353 => 1,
100100
354 => 1,
101-
355 => 2,
101+
355 => 1,
102102
377 => 1,
103-
378 => 1,
104-
379 => 1,
105-
380 => 1,
106103
385 => 1,
107-
386 => 1,
108-
387 => 1,
109-
388 => 1,
110104
393 => 1,
111105
394 => 1,
112106
395 => 1,
@@ -135,7 +129,6 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
135129
567 => 1,
136130
568 => 1,
137131
573 => 1,
138-
574 => 1,
139132
];
140133

141134
}//end getErrorList()

src/Standards/PSR2/Sniffs/Methods/FunctionCallSignatureSniff.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FunctionCallSignatureSniff extends PEARFunctionCallSignatureSniff
2525

2626

2727
/**
28-
* Processes single-line calls.
28+
* Determine if this is a multi-line function call.
2929
*
3030
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
3131
* @param int $stackPtr The position of the current token
@@ -76,4 +76,26 @@ public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $token
7676
}//end isMultiLineCall()
7777

7878

79+
/**
80+
* No-op; this rule does not apply to PSR-2.
81+
*
82+
* @param File $phpcsFile The file being scanned.
83+
* @param int $first Pointer to the first empty token on this line.
84+
* @param array $tokens The stack of tokens that make up the file.
85+
* @param int $functionIndent The expected indent for this function definition.
86+
* @param int $foundFunctionIndent The actual indent for this function definition.
87+
*
88+
* @return void
89+
*/
90+
protected function complainOpenStatementWrongIndent(
91+
$phpcsFile,
92+
$first,
93+
$tokens,
94+
$functionIndent,
95+
$foundFunctionIndent
96+
) {
97+
98+
}//end complainOpenStatementWrongIndent()
99+
100+
79101
}//end class

src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc

+15
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,18 @@ array_fill_keys(
265265
), value: true,
266266
);
267267
// phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false
268+
269+
// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
270+
// This sniff should accept both of these styles. Generic.WhiteSpace.ScopeIndent can complain & fix this if enabled.
271+
$repository->foo()
272+
->bar(
273+
function () {
274+
return true;
275+
}
276+
);
277+
$repository->foo()
278+
->bar(
279+
function () {
280+
return true;
281+
}
282+
);

src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed

+15
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,18 @@ array_fill_keys(
281281
), value: true,
282282
);
283283
// phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false
284+
285+
// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
286+
// This sniff should accept both of these styles. Generic.WhiteSpace.ScopeIndent can complain & fix this if enabled.
287+
$repository->foo()
288+
->bar(
289+
function () {
290+
return true;
291+
}
292+
);
293+
$repository->foo()
294+
->bar(
295+
function () {
296+
return true;
297+
}
298+
);

src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public function getErrorList()
5252
187 => 1,
5353
194 => 3,
5454
199 => 1,
55-
200 => 2,
55+
200 => 1,
5656
202 => 1,
5757
203 => 1,
5858
210 => 2,
5959
211 => 1,
60-
212 => 2,
60+
212 => 1,
6161
217 => 1,
6262
218 => 1,
6363
227 => 1,

src/Standards/PSR2/ruleset.xml

-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@
151151
<rule ref="PSR2.Methods.FunctionCallSignature.SpaceAfterCloseBracket">
152152
<severity>0</severity>
153153
</rule>
154-
<rule ref="PSR2.Methods.FunctionCallSignature.OpeningIndent">
155-
<severity>0</severity>
156-
</rule>
157154

158155
<!-- 5. Control Structures -->
159156

0 commit comments

Comments
 (0)