Skip to content

Commit 539c056

Browse files
janedbalkukulich
authored andcommitted
SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature: New option "minParameterCount"
1 parent c49f80c commit 539c056

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

SlevomatCodingStandard/Sniffs/Classes/RequireMultiLineMethodSignatureSniff.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SlevomatCodingStandard\Helpers\IndentationHelper;
1010
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
1111
use SlevomatCodingStandard\Helpers\TokenHelper;
12+
use UnexpectedValueException;
1213
use function count;
1314
use function preg_match;
1415
use function sprintf;
@@ -19,9 +20,13 @@ class RequireMultiLineMethodSignatureSniff extends AbstractMethodSignature
1920
{
2021

2122
public const CODE_REQUIRED_MULTI_LINE_SIGNATURE = 'RequiredMultiLineSignature';
23+
private const DEFAULT_MIN_LINE_LENGTH = 121;
2224

23-
/** @var int */
24-
public $minLineLength = 121;
25+
/** @var int|null */
26+
public $minLineLength = null;
27+
28+
/** @var int|null */
29+
public $minParametersCount = null;
2530

2631
/** @var string[] */
2732
public $includedMethodPatterns = [];
@@ -41,7 +46,17 @@ class RequireMultiLineMethodSignatureSniff extends AbstractMethodSignature
4146
*/
4247
public function process(File $phpcsFile, $methodPointer): void
4348
{
44-
$this->minLineLength = SniffSettingsHelper::normalizeInteger($this->minLineLength);
49+
$this->minLineLength = SniffSettingsHelper::normalizeNullableInteger($this->minLineLength);
50+
$this->minParametersCount = SniffSettingsHelper::normalizeNullableInteger($this->minParametersCount);
51+
52+
if ($this->minLineLength !== null && $this->minParametersCount !== null) {
53+
throw new UnexpectedValueException('Either minLineLength or minParametersCount can be set.');
54+
}
55+
56+
// Maintain backward compatibility if no configuration provided
57+
if ($this->minLineLength === null && $this->minParametersCount === null) {
58+
$this->minLineLength = self::DEFAULT_MIN_LINE_LENGTH;
59+
}
4560

4661
if (!FunctionHelper::isMethod($phpcsFile, $methodPointer)) {
4762
return;
@@ -56,7 +71,8 @@ public function process(File $phpcsFile, $methodPointer): void
5671
}
5772

5873
$parameters = $phpcsFile->getMethodParameters($methodPointer);
59-
if (count($parameters) === 0) {
74+
$parametersCount = count($parameters);
75+
if ($parametersCount === 0) {
6076
return;
6177
}
6278

@@ -78,7 +94,11 @@ public function process(File $phpcsFile, $methodPointer): void
7894
return;
7995
}
8096

81-
if ($this->minLineLength !== 0 && strlen($signatureWithoutTabIndentation) < $this->minLineLength) {
97+
if ($this->minLineLength !== null && $this->minLineLength !== 0 && strlen($signatureWithoutTabIndentation) < $this->minLineLength) {
98+
return;
99+
}
100+
101+
if ($this->minParametersCount !== null && $parametersCount < $this->minParametersCount) {
82102
return;
83103
}
84104

doc/classes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Enforces method signature to be split to more lines so each parameter is on its
213213
Sniff provides the following settings:
214214

215215
* `minLineLength`: specifies min line length to enforce signature to be split. Use 0 value to enforce for all methods, regardless of length.
216+
* `minParametersCount`: specifies min parameters count to enforce signature to be split.
216217

217218
* `includedMethodPatterns`: allows to configure which methods are included in sniff detection. This is an array of regular expressions (PCRE) with delimiters. You should not use this with `excludedMethodPatterns`, as it will not work properly.
218219

tests/Sniffs/Classes/RequireMultiLineMethodSignatureSniffTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public function testThrowExceptionForInvalidPattern(): void
2323
);
2424
}
2525

26+
public function testThrowExceptionOnInvalidSetup(): void
27+
{
28+
$this->expectException(Throwable::class);
29+
30+
self::checkFile(
31+
__DIR__ . '/data/requireMultiLineMethodSignatureNoErrors.php',
32+
['minLineLength' => 100, 'minParametersCount' => 2]
33+
);
34+
}
35+
2636
public function testNoErrors(): void
2737
{
2838
$report = self::checkFile(__DIR__ . '/data/requireMultiLineMethodSignatureNoErrors.php');
@@ -42,6 +52,22 @@ public function testErrors(): void
4252
self::assertAllFixedInFile($report);
4353
}
4454

55+
public function testErrorsBasedOnParamCount(): void
56+
{
57+
$report = self::checkFile(
58+
__DIR__ . '/data/requireMultiLineMethodSignatureBasedOnParamCountErrors.php',
59+
[
60+
'minParametersCount' => 2,
61+
]
62+
);
63+
self::assertSame(2, $report->getErrorCount());
64+
65+
self::assertSniffError($report, 8, RequireMultiLineMethodSignatureSniff::CODE_REQUIRED_MULTI_LINE_SIGNATURE);
66+
self::assertSniffError($report, 14, RequireMultiLineMethodSignatureSniff::CODE_REQUIRED_MULTI_LINE_SIGNATURE);
67+
68+
self::assertAllFixedInFile($report);
69+
}
70+
4571
public function testForAllMethods(): void
4672
{
4773
$report = self::checkFile(__DIR__ . '/data/requireMultiLineMethodSignatureAllMethodsErrors.php', ['minLineLength' => 0]);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php // lint >= 8.0
2+
3+
class A
4+
{
5+
public function __construct(public int $single) {
6+
}
7+
8+
public function tooManyParams(
9+
int $one,
10+
string $two
11+
) {
12+
}
13+
}
14+
15+
interface B
16+
{
17+
public function tooManyParams(
18+
int $one,
19+
string $two,
20+
string $three
21+
) : void;
22+
23+
public function noParam();
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php // lint >= 8.0
2+
3+
class A
4+
{
5+
public function __construct(public int $single) {
6+
}
7+
8+
public function tooManyParams(int $one, string $two) {
9+
}
10+
}
11+
12+
interface B
13+
{
14+
public function tooManyParams(int $one, string $two, string $three) : void;
15+
16+
public function noParam();
17+
}

0 commit comments

Comments
 (0)