Skip to content

Commit 4663def

Browse files
committed
Avoid incompatibility between two sniffs
1 parent 1419b6c commit 4663def

10 files changed

+174
-39
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-24
Original file line numberDiff line numberDiff line change
@@ -375,28 +375,16 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
375375
// at a tab stop. Without this, the function will be indented a further
376376
// $indent spaces to the right.
377377
$functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
378-
$adjustment = 0;
378+
$adjustment = ($functionIndent - $foundFunctionIndent);
379379

380380
if ($foundFunctionIndent !== $functionIndent) {
381-
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
382-
$data = [
381+
$this->complainOpenStatementWrongIndent(
382+
$phpcsFile,
383+
$first,
384+
$tokens,
383385
$functionIndent,
384-
$foundFunctionIndent,
385-
];
386-
387-
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
388-
if ($fix === true) {
389-
$adjustment = ($functionIndent - $foundFunctionIndent);
390-
$padding = str_repeat(' ', $functionIndent);
391-
if ($foundFunctionIndent === 0) {
392-
$phpcsFile->fixer->addContentBefore($first, $padding);
393-
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
394-
$newContent = $padding.ltrim($tokens[$first]['content']);
395-
$phpcsFile->fixer->replaceToken($first, $newContent);
396-
} else {
397-
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
398-
}
399-
}
386+
$foundFunctionIndent
387+
);
400388
}//end if
401389

402390
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
@@ -462,7 +450,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
462450
$i--;
463451
}
464452

465-
for ($i; $i < $closeBracket; $i++) {
453+
for (; $i < $closeBracket; $i++) {
466454
if ($i > $argStart && $i < $argEnd) {
467455
$inArg = true;
468456
} else {
@@ -542,10 +530,34 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
542530
$foundIndent = $tokens[$i]['length'];
543531
}
544532

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

630642

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

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

+2-8
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

+24-2
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
{
@@ -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)