Skip to content

Commit 348efa8

Browse files
committed
RequireSingleLineCallSniffTest: Fixed fixer
1 parent 4fd3471 commit 348efa8

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
use SlevomatCodingStandard\Helpers\IndentationHelper;
88
use SlevomatCodingStandard\Helpers\TokenHelper;
99
use function array_merge;
10-
use function preg_replace;
1110
use function rtrim;
12-
use function sprintf;
1311
use function trim;
12+
use const T_CLOSE_PARENTHESIS;
13+
use const T_COMMA;
1414
use const T_FUNCTION;
1515
use const T_OPEN_PARENTHESIS;
1616
use const T_PARENT;
1717
use const T_SELF;
1818
use const T_STATIC;
19+
use const T_WHITESPACE;
1920

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

5354
protected function getCall(File $phpcsFile, int $parenthesisOpenerPointer, int $parenthesisCloserPointer): string
5455
{
55-
$call = TokenHelper::getContent($phpcsFile, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer - 1);
56+
$tokens = $phpcsFile->getTokens();
57+
58+
$pointerBeforeParenthesisCloser = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1);
59+
60+
$endPointer = $tokens[$pointerBeforeParenthesisCloser]['code'] === T_COMMA
61+
? $pointerBeforeParenthesisCloser
62+
: $parenthesisCloserPointer;
63+
64+
$call = '';
65+
66+
for ($i = $parenthesisOpenerPointer + 1; $i < $endPointer; $i++) {
67+
if ($tokens[$i]['code'] === T_COMMA) {
68+
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1);
69+
if ($tokens[$nextPointer]['code'] === T_CLOSE_PARENTHESIS) {
70+
$i = $nextPointer - 1;
71+
continue;
72+
}
73+
}
5674

57-
$call = preg_replace(sprintf('~%s[ \t]*~', $phpcsFile->eolChar), ' ', $call);
58-
$call = preg_replace('~([({[])\s+~', '$1', $call);
59-
$call = preg_replace('~\s+([)}\]])~', '$1', $call);
60-
$call = preg_replace('~,\s*\)~', ')', $call);
61-
$call = preg_replace('~,\s*$~', '', $call);
75+
if ($tokens[$i]['code'] === T_WHITESPACE) {
76+
if ($tokens[$i]['content'] === $phpcsFile->eolChar) {
77+
if ($tokens[$i - 1]['code'] === T_COMMA) {
78+
$call .= ' ';
79+
}
80+
81+
continue;
82+
83+
} if ($tokens[$i]['column'] === 1) {
84+
// Nothing
85+
continue;
86+
}
87+
}
88+
89+
$call .= $tokens[$i]['content'];
90+
}
6291

6392
return trim($call);
6493
}

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ public function process(File $phpcsFile, $openTagPointer): void
262262
&& $namespacePointers === []
263263
) {
264264
$label = sprintf(
265-
$reference->isConstant
266-
? 'Constant %s'
267-
: ($reference->isFunction ? 'Function %s()' : 'Class %s'),
265+
$reference->isConstant ? 'Constant %s' : ($reference->isFunction ? 'Function %s()' : 'Class %s'),
268266
$name
269267
);
270268

tests/Sniffs/Functions/RequireSingleLineCallSniffTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testWithComplexParametersEnabledErrors(): void
6666
'ignoreWithComplexParameter' => false,
6767
]);
6868

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

7171
self::assertSniffError(
7272
$report,
@@ -92,6 +92,12 @@ public function testWithComplexParametersEnabledErrors(): void
9292
RequireSingleLineCallSniff::CODE_REQUIRED_SINGLE_LINE_CALL,
9393
'Call of method doWhatever() should be placed on a single line.'
9494
);
95+
self::assertSniffError(
96+
$report,
97+
29,
98+
RequireSingleLineCallSniff::CODE_REQUIRED_SINGLE_LINE_CALL,
99+
'Call of method doNothing() should be placed on a single line.'
100+
);
95101

96102
self::assertAllFixedInFile($report);
97103
}

tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.fixed.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public function foo()
1111
return new self([true, false]);
1212

1313
self::doWhatever(self::doAnything(true, false));
14+
15+
$this->doNothing('[test]', '{test}', '(test)');
1416
}
1517
}

tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ function () {
2525
self::doWhatever(
2626
self::doAnything(true, false)
2727
);
28+
29+
$this->doNothing(
30+
'[test]',
31+
'{test}',
32+
'(test)',
33+
);
2834
}
2935
}

0 commit comments

Comments
 (0)