Skip to content

Commit

Permalink
Avoid incompatibility between two sniffs
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Jul 11, 2023
1 parent d415500 commit 33a4d33
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 24 deletions.
15 changes: 15 additions & 0 deletions src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.3.inc
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ if ($foo) {
$this->foo()
->bar()
->baz();

// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
// See also PSR2.Methods.FunctionCallSignature
$repository->foo()
->bar(
function () {
return true;
}
);
$repository->foo()
->bar(
function () {
return true;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ if ($foo) {
$this->foo()
->bar()
->baz();

// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
// See also PSR2.Methods.FunctionCallSignature
$repository->foo()
->bar(
function () {
return true;
}
);
$repository->foo()
->bar(
function () {
return true;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
6 => 1,
7 => 1,
10 => 1,
40 => 1,
41 => 1,
42 => 1,
];
}

Expand Down
46 changes: 37 additions & 9 deletions src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

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

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

$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
if ($fix === true) {
$adjustment = ($functionIndent - $foundFunctionIndent);
$padding = str_repeat(' ', $functionIndent);
$padding = str_repeat(' ', $functionIndent);
if ($foundFunctionIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
Expand Down Expand Up @@ -462,7 +466,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
$i--;
}

for ($i; $i < $closeBracket; $i++) {
for (; $i < $closeBracket; $i++) {
if ($i > $argStart && $i < $argEnd) {
$inArg = true;
} else {
Expand Down Expand Up @@ -542,10 +546,34 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
$foundIndent = $tokens[$i]['length'];
}

if ($foundIndent < $expectedIndent
|| ($inArg === false
&& $expectedIndent !== $foundIndent)
) {
$indentCorrect = true;

if ($foundIndent < $expectedIndent) {
$indentCorrect = false;
} else if ($inArg === false && $expectedIndent !== $foundIndent) {
$indentCorrect = false;

// It is permitted to indent chains further than one tab stop to
// align vertically with the previous method call.
if ($i === ($closeBracket - 1)) {
if ($foundIndent === $foundFunctionIndent) {
// This is the closing paren; it lines up vertically with the opening paren.
$indentCorrect = true;
}
} else {
if ($foundIndent === ($tokens[$openBracket]['column'] - 1)) {
// This is a parameter; it lines up vertically with the opening paren.
$indentCorrect = true;
}

if ($foundIndent === ($foundFunctionIndent + ($this->indent))) {
// This is a parameter; it is indented one more step than the function call around it.
$indentCorrect = true;
}
}
}//end if

if ($indentCorrect === false) {
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
$data = [
$expectedIndent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
82 => 1,
93 => 1,
100 => 1,
106 => 2,
106 => 1,
119 => 1,
120 => 1,
129 => 1,
Expand Down Expand Up @@ -98,15 +98,9 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
346 => 2,
353 => 1,
354 => 1,
355 => 2,
355 => 1,
377 => 1,
378 => 1,
379 => 1,
380 => 1,
385 => 1,
386 => 1,
387 => 1,
388 => 1,
393 => 1,
394 => 1,
395 => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FunctionCallSignatureSniff extends PEARFunctionCallSignatureSniff


/**
* Processes single-line calls.
* Determine if this is a multi-line function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
Expand All @@ -35,7 +35,7 @@ class FunctionCallSignatureSniff extends PEARFunctionCallSignatureSniff
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return void
* @return bool
*/
public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens)
{
Expand Down
15 changes: 15 additions & 0 deletions src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,18 @@ array_fill_keys(
), value: true,
);
// phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false

// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
// This sniff should accept both of these styles. Generic.WhiteSpace.ScopeIndent can complain & fix this if enabled.
$repository->foo()
->bar(
function () {
return true;
}
);
$repository->foo()
->bar(
function () {
return true;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,18 @@ array_fill_keys(
), value: true,
);
// phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false

// https://github.com/squizlabs/PHP_CodeSniffer/issues/2078#issuecomment-401641650
// This sniff should accept both of these styles. Generic.WhiteSpace.ScopeIndent can complain & fix this if enabled.
$repository->foo()
->bar(
function () {
return true;
}
);
$repository->foo()
->bar(
function () {
return true;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public function getErrorList()
187 => 1,
194 => 3,
199 => 1,
200 => 2,
200 => 1,
202 => 1,
203 => 1,
210 => 2,
211 => 1,
212 => 2,
212 => 1,
217 => 1,
218 => 1,
227 => 1,
Expand Down
3 changes: 0 additions & 3 deletions src/Standards/PSR2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@
<rule ref="PSR2.Methods.FunctionCallSignature.SpaceAfterCloseBracket">
<severity>0</severity>
</rule>
<rule ref="PSR2.Methods.FunctionCallSignature.OpeningIndent">
<severity>0</severity>
</rule>

<!-- 5. Control Structures -->

Expand Down

0 comments on commit 33a4d33

Please sign in to comment.