Skip to content

Commit 162a943

Browse files
committed
RequireMultiLineCallSniff: Don't report lines that will be longer when splitted
1 parent 3c5fd63 commit 162a943

File tree

6 files changed

+73
-8
lines changed

6 files changed

+73
-8
lines changed

SlevomatCodingStandard/Sniffs/Functions/RequireMultiLineCallSniff.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function in_array;
1313
use function sprintf;
1414
use function strlen;
15+
use function trim;
1516
use const T_CLOSE_PARENTHESIS;
1617
use const T_CLOSE_SHORT_ARRAY;
1718
use const T_COMMA;
@@ -110,7 +111,17 @@ public function process(File $phpcsFile, $stringPointer): void
110111
$lineLength = strlen($lineStart . $lineEnd);
111112
}
112113

113-
if (!$this->shouldReportError($lineLength)) {
114+
$firstNonWhitespaceOnLine = TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $stringPointer);
115+
$indentation = IndentationHelper::getIndentation($phpcsFile, $firstNonWhitespaceOnLine);
116+
$oneIndentation = IndentationHelper::getOneIndentationLevel($indentation);
117+
118+
if (!$this->shouldReportError(
119+
$lineLength,
120+
$lineStart,
121+
$lineEnd,
122+
count($parametersPointers),
123+
strlen(IndentationHelper::convertTabsToSpaces($phpcsFile, $oneIndentation))
124+
)) {
114125
return;
115126
}
116127

@@ -130,10 +141,7 @@ public function process(File $phpcsFile, $stringPointer): void
130141
return;
131142
}
132143

133-
$firstNonWhitespaceOnLine = TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $stringPointer);
134-
$indentation = IndentationHelper::getIndentation($phpcsFile, $firstNonWhitespaceOnLine);
135144
$parametersIndentation = IndentationHelper::addIndentation($indentation);
136-
$oneIndentation = IndentationHelper::getOneIndentationLevel($indentation);
137145

138146
$phpcsFile->fixer->beginChangeset();
139147

@@ -192,15 +200,29 @@ private function shouldBeSkipped(File $phpcsFile, int $stringPointer, int $paren
192200
return false;
193201
}
194202

195-
private function shouldReportError(int $lineLength): bool
203+
private function shouldReportError(
204+
int $lineLength,
205+
string $lineStart,
206+
string $lineEnd,
207+
int $parametersCount,
208+
int $indentationLength
209+
): bool
196210
{
197211
$minLineLength = SniffSettingsHelper::normalizeInteger($this->minLineLength);
198212

199213
if ($minLineLength === 0) {
200214
return true;
201215
}
202216

203-
return $lineLength >= $minLineLength;
217+
if ($lineLength < $minLineLength) {
218+
return false;
219+
}
220+
221+
if ($parametersCount > 1) {
222+
return true;
223+
}
224+
225+
return strlen(trim($lineStart) . trim($lineEnd)) > $indentationLength;
204226
}
205227

206228
}

tests/Sniffs/Functions/RequireMultiLineCallSniffTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/requireMultiLineCallErrors.php');
1919

20-
self::assertSame(13, $report->getErrorCount());
20+
self::assertSame(15, $report->getErrorCount());
2121

2222
self::assertSniffError(
2323
$report,
@@ -97,6 +97,18 @@ public function testErrors(): void
9797
RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL,
9898
'Call of method sendDelayedMessage() should be splitted to more lines.'
9999
);
100+
self::assertSniffError(
101+
$report,
102+
51,
103+
RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL,
104+
'Call of function sprintf() should be splitted to more lines.'
105+
);
106+
self::assertSniffError(
107+
$report,
108+
54,
109+
RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL,
110+
'Call of function _() should be splitted to more lines.'
111+
);
100112

101113
self::assertAllFixedInFile($report);
102114
}
@@ -115,9 +127,10 @@ public function testAllCallsErrors(): void
115127
'minLineLength' => 0,
116128
]);
117129

118-
self::assertSame(1, $report->getErrorCount());
130+
self::assertSame(2, $report->getErrorCount());
119131

120132
self::assertSniffError($report, 7, RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL);
133+
self::assertSniffError($report, 12, RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL);
121134

122135
self::assertAllFixedInFile($report);
123136
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ public function __construct()
1010
);
1111
}
1212
}
13+
14+
function ($text) {
15+
return sprintf(
16+
_(
17+
'very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong parameter'
18+
),
19+
$text
20+
);
21+
};

tests/Sniffs/Functions/data/requireMultiLineCallAllCallsErrors.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ public function __construct()
77
$this->doAnything('true', false);
88
}
99
}
10+
11+
function ($text) {
12+
return sprintf(_('very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong parameter'), $text);
13+
};

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ function () use ($message, $originalQueueName, $delayedQueueName, $nextAttemptTi
9090
);
9191
}
9292
}
93+
94+
function ($text) {
95+
return sprintf(
96+
_('very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong parameter'),
97+
$text
98+
);
99+
};
100+
101+
$a = _(
102+
'very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong parameter'
103+
);

tests/Sniffs/Functions/data/requireMultiLineCallErrors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ public function commit($message, $originalQueueName, $delayedQueueName, $nextAtt
4646
});
4747
}
4848
}
49+
50+
function ($text) {
51+
return sprintf(_('very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong parameter'), $text);
52+
};
53+
54+
$a = _('very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong parameter');

0 commit comments

Comments
 (0)