Skip to content

Commit 33a4d33

Browse files
committed
Avoid incompatibility between two sniffs
1 parent d415500 commit 33a4d33

File tree

10 files changed

+106
-24
lines changed

10 files changed

+106
-24
lines changed

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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Ruleset;
1314
use PHP_CodeSniffer\Sniffs\Sniff;
1415
use PHP_CodeSniffer\Util\Tokens;
1516

@@ -375,9 +376,13 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
375376
// at a tab stop. Without this, the function will be indented a further
376377
// $indent spaces to the right.
377378
$functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
378-
$adjustment = 0;
379+
$adjustment = ($functionIndent - $foundFunctionIndent);
379380

380-
if ($foundFunctionIndent !== $functionIndent) {
381+
// This rule doesn't apply to PSR2, but this sniff is used there. We can
382+
// silence the complaint in the PSR2 ruleset.xml, but the unit tests
383+
// don't seem to respect that. If there is a way to make the unit tests
384+
// respect ruleset.xml, then we can remove the `strpos()` call here.
385+
if ($foundFunctionIndent !== $functionIndent && strpos(get_class($this), '\\PSR2\\') === false) {
381386
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
382387
$data = [
383388
$functionIndent,
@@ -386,8 +391,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
386391

387392
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
388393
if ($fix === true) {
389-
$adjustment = ($functionIndent - $foundFunctionIndent);
390-
$padding = str_repeat(' ', $functionIndent);
394+
$padding = str_repeat(' ', $functionIndent);
391395
if ($foundFunctionIndent === 0) {
392396
$phpcsFile->fixer->addContentBefore($first, $padding);
393397
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
@@ -462,7 +466,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
462466
$i--;
463467
}
464468

465-
for ($i; $i < $closeBracket; $i++) {
469+
for (; $i < $closeBracket; $i++) {
466470
if ($i > $argStart && $i < $argEnd) {
467471
$inArg = true;
468472
} else {
@@ -542,10 +546,34 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
542546
$foundIndent = $tokens[$i]['length'];
543547
}
544548

545-
if ($foundIndent < $expectedIndent
546-
|| ($inArg === false
547-
&& $expectedIndent !== $foundIndent)
548-
) {
549+
$indentCorrect = true;
550+
551+
if ($foundIndent < $expectedIndent) {
552+
$indentCorrect = false;
553+
} else if ($inArg === false && $expectedIndent !== $foundIndent) {
554+
$indentCorrect = false;
555+
556+
// It is permitted to indent chains further than one tab stop to
557+
// align vertically with the previous method call.
558+
if ($i === ($closeBracket - 1)) {
559+
if ($foundIndent === $foundFunctionIndent) {
560+
// This is the closing paren; it lines up vertically with the opening paren.
561+
$indentCorrect = true;
562+
}
563+
} else {
564+
if ($foundIndent === ($tokens[$openBracket]['column'] - 1)) {
565+
// This is a parameter; it lines up vertically with the opening paren.
566+
$indentCorrect = true;
567+
}
568+
569+
if ($foundIndent === ($foundFunctionIndent + ($this->indent))) {
570+
// This is a parameter; it is indented one more step than the function call around it.
571+
$indentCorrect = true;
572+
}
573+
}
574+
}//end if
575+
576+
if ($indentCorrect === false) {
549577
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
550578
$data = [
551579
$expectedIndent,

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

Lines changed: 2 additions & 8 deletions
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,

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

Lines changed: 2 additions & 2 deletions
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
@@ -35,7 +35,7 @@ class FunctionCallSignatureSniff extends PEARFunctionCallSignatureSniff
3535
* @param array $tokens The stack of tokens that make up
3636
* the file.
3737
*
38-
* @return void
38+
* @return bool
3939
*/
4040
public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens)
4141
{

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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 3 deletions
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)