Skip to content

Commit

Permalink
RequireSingleLineCallSniffTest: Fixed fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jul 9, 2021
1 parent 4fd3471 commit 348efa8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
45 changes: 37 additions & 8 deletions SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
use SlevomatCodingStandard\Helpers\IndentationHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_merge;
use function preg_replace;
use function rtrim;
use function sprintf;
use function trim;
use const T_CLOSE_PARENTHESIS;
use const T_COMMA;
use const T_FUNCTION;
use const T_OPEN_PARENTHESIS;
use const T_PARENT;
use const T_SELF;
use const T_STATIC;
use const T_WHITESPACE;

abstract class AbstractLineCall implements Sniff
{
Expand Down Expand Up @@ -52,13 +53,41 @@ protected function getLineStart(File $phpcsFile, int $pointer): string

protected function getCall(File $phpcsFile, int $parenthesisOpenerPointer, int $parenthesisCloserPointer): string
{
$call = TokenHelper::getContent($phpcsFile, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer - 1);
$tokens = $phpcsFile->getTokens();

$pointerBeforeParenthesisCloser = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1);

$endPointer = $tokens[$pointerBeforeParenthesisCloser]['code'] === T_COMMA
? $pointerBeforeParenthesisCloser
: $parenthesisCloserPointer;

$call = '';

for ($i = $parenthesisOpenerPointer + 1; $i < $endPointer; $i++) {
if ($tokens[$i]['code'] === T_COMMA) {
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1);
if ($tokens[$nextPointer]['code'] === T_CLOSE_PARENTHESIS) {
$i = $nextPointer - 1;
continue;
}
}

$call = preg_replace(sprintf('~%s[ \t]*~', $phpcsFile->eolChar), ' ', $call);
$call = preg_replace('~([({[])\s+~', '$1', $call);
$call = preg_replace('~\s+([)}\]])~', '$1', $call);
$call = preg_replace('~,\s*\)~', ')', $call);
$call = preg_replace('~,\s*$~', '', $call);
if ($tokens[$i]['code'] === T_WHITESPACE) {
if ($tokens[$i]['content'] === $phpcsFile->eolChar) {
if ($tokens[$i - 1]['code'] === T_COMMA) {
$call .= ' ';
}

continue;

} if ($tokens[$i]['column'] === 1) {
// Nothing
continue;
}
}

$call .= $tokens[$i]['content'];
}

return trim($call);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ public function process(File $phpcsFile, $openTagPointer): void
&& $namespacePointers === []
) {
$label = sprintf(
$reference->isConstant
? 'Constant %s'
: ($reference->isFunction ? 'Function %s()' : 'Class %s'),
$reference->isConstant ? 'Constant %s' : ($reference->isFunction ? 'Function %s()' : 'Class %s'),
$name
);

Expand Down
8 changes: 7 additions & 1 deletion tests/Sniffs/Functions/RequireSingleLineCallSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testWithComplexParametersEnabledErrors(): void
'ignoreWithComplexParameter' => false,
]);

self::assertSame(4, $report->getErrorCount());
self::assertSame(5, $report->getErrorCount());

self::assertSniffError(
$report,
Expand All @@ -92,6 +92,12 @@ public function testWithComplexParametersEnabledErrors(): void
RequireSingleLineCallSniff::CODE_REQUIRED_SINGLE_LINE_CALL,
'Call of method doWhatever() should be placed on a single line.'
);
self::assertSniffError(
$report,
29,
RequireSingleLineCallSniff::CODE_REQUIRED_SINGLE_LINE_CALL,
'Call of method doNothing() should be placed on a single line.'
);

self::assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public function foo()
return new self([true, false]);

self::doWhatever(self::doAnything(true, false));

$this->doNothing('[test]', '{test}', '(test)');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ function () {
self::doWhatever(
self::doAnything(true, false)
);

$this->doNothing(
'[test]',
'{test}',
'(test)',
);
}
}

0 comments on commit 348efa8

Please sign in to comment.