Skip to content

Commit 48fb2bb

Browse files
parth391kukulich
authored andcommitted
SlevomatCodingStandard.Functions.RequireMultiLineCall: Add "minParametersCount" option
1 parent 90ed304 commit 48fb2bb

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

SlevomatCodingStandard/Sniffs/Functions/RequireMultiLineCallSniff.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SlevomatCodingStandard\Helpers\IndentationHelper;
88
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
99
use SlevomatCodingStandard\Helpers\TokenHelper;
10+
use UnexpectedValueException;
1011
use function array_unique;
1112
use function count;
1213
use function in_array;
@@ -27,12 +28,25 @@ class RequireMultiLineCallSniff extends AbstractLineCall
2728
{
2829

2930
public const CODE_REQUIRED_MULTI_LINE_CALL = 'RequiredMultiLineCall';
31+
private const DEFAULT_MIN_LINE_LENGTH = 121;
3032

31-
public int $minLineLength = 121;
33+
public ?int $minLineLength = null;
34+
35+
public ?int $minParametersCount = null;
3236

3337
public function process(File $phpcsFile, int $stringPointer): void
3438
{
35-
$this->minLineLength = SniffSettingsHelper::normalizeInteger($this->minLineLength);
39+
$this->minLineLength = SniffSettingsHelper::normalizeNullableInteger($this->minLineLength);
40+
$this->minParametersCount = SniffSettingsHelper::normalizeNullableInteger($this->minParametersCount);
41+
42+
if ($this->minLineLength !== null && $this->minParametersCount !== null) {
43+
throw new UnexpectedValueException('Either minLineLength or minParametersCount can be set.');
44+
}
45+
46+
// Backward compatibility if no configuration provided
47+
if ($this->minLineLength === null && $this->minParametersCount === null) {
48+
$this->minLineLength = self::DEFAULT_MIN_LINE_LENGTH;
49+
}
3650

3751
if (!$this->isCall($phpcsFile, $stringPointer)) {
3852
return;
@@ -228,7 +242,11 @@ private function shouldReportError(
228242
return true;
229243
}
230244

231-
if ($lineLength < $this->minLineLength) {
245+
if ($this->minLineLength !== null && $lineLength < $this->minLineLength) {
246+
return false;
247+
}
248+
249+
if ($this->minParametersCount !== null && $parametersCount < $this->minParametersCount) {
232250
return false;
233251
}
234252

doc/functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Enforces function call to be split to more lines so each parameter is on its own
4343
Sniff provides the following settings:
4444

4545
* `minLineLength`: specifies min line length to enforce call to be split. Use 0 value to enforce for all calls, regardless of length.
46+
* `minParametersCount`: specifies min parameters count to enforce call to be split.
4647

4748
#### SlevomatCodingStandard.Functions.RequireSingleLineCall 🔧
4849

tests/Sniffs/Functions/RequireMultiLineCallSniffTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SlevomatCodingStandard\Sniffs\Functions;
44

55
use SlevomatCodingStandard\Sniffs\TestCase;
6+
use Throwable;
67

78
class RequireMultiLineCallSniffTest extends TestCase
89
{
@@ -135,4 +136,30 @@ public function testAllCallsErrors(): void
135136
self::assertAllFixedInFile($report);
136137
}
137138

139+
public function testThrowExceptionOnInvalidSetup(): void
140+
{
141+
$this->expectException(Throwable::class);
142+
143+
self::checkFile(
144+
__DIR__ . '/data/requireMultiLineCallAllCallsNoErrors.php',
145+
['minLineLength' => 100, 'minParametersCount' => 2],
146+
);
147+
}
148+
149+
public function testErrorsBasedOnParamCount(): void
150+
{
151+
$report = self::checkFile(
152+
__DIR__ . '/data/requireMultiLineCallParamCountErrors.php',
153+
[
154+
'minParametersCount' => 2,
155+
],
156+
);
157+
self::assertSame(2, $report->getErrorCount());
158+
159+
self::assertSniffError($report, 8, RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL);
160+
self::assertSniffError($report, 14, RequireMultiLineCallSniff::CODE_REQUIRED_MULTI_LINE_CALL);
161+
162+
self::assertAllFixedInFile($report);
163+
}
164+
138165
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
class Whatever
4+
{
5+
public function __construct()
6+
{
7+
$this->doAnything('false');
8+
$this->doAnything(
9+
'true',
10+
false
11+
);
12+
}
13+
}
14+
15+
function ($text) {
16+
sprintf(_('one parameter'));
17+
return sprintf(
18+
_('one parameter'),
19+
$text
20+
);
21+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class Whatever
4+
{
5+
public function __construct()
6+
{
7+
$this->doAnything('false');
8+
$this->doAnything('true', false);
9+
}
10+
}
11+
12+
function ($text) {
13+
sprintf(_('one parameter'));
14+
return sprintf(_('one parameter'), $text);
15+
};

0 commit comments

Comments
 (0)