Skip to content

Commit fad79b8

Browse files
vranakukulich
authored andcommitted
SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature: New option "withPromotedProperties"
1 parent 61db21f commit fad79b8

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

SlevomatCodingStandard/Sniffs/Classes/RequireMultiLineMethodSignatureSniff.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class RequireMultiLineMethodSignatureSniff extends AbstractMethodSignature
2626

2727
public ?int $minParametersCount = null;
2828

29+
public bool $withPromotedProperties = false;
30+
2931
/** @var list<string> */
3032
public array $includedMethodPatterns = [];
3133

@@ -91,12 +93,24 @@ public function process(File $phpcsFile, $methodPointer): void
9193
return;
9294
}
9395

94-
if ($this->minLineLength !== null && $this->minLineLength !== 0 && strlen($signature) < $this->minLineLength) {
95-
return;
96+
$splitPromotedProperties = false;
97+
if ($this->withPromotedProperties) {
98+
foreach ($parameters as $parameter) {
99+
if (isset($parameter['property_visibility'])) {
100+
$splitPromotedProperties = true;
101+
break;
102+
}
103+
}
96104
}
97105

98-
if ($this->minParametersCount !== null && $parametersCount < $this->minParametersCount) {
99-
return;
106+
if (!$splitPromotedProperties) {
107+
if ($this->minLineLength !== null && $this->minLineLength !== 0 && strlen($signature) < $this->minLineLength) {
108+
return;
109+
}
110+
111+
if ($this->minParametersCount !== null && $parametersCount < $this->minParametersCount) {
112+
return;
113+
}
100114
}
101115

102116
$error = sprintf('Signature of method "%s" should be split to more lines so each parameter is on its own line.', $methodName);

doc/classes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ Sniff provides the following settings:
246246

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

249+
* `withPromotedProperties`: always require multiline signatures for methods with promoted properties.
250+
249251
#### SlevomatCodingStandard.Classes.RequireSelfReference 🔧
250252

251253
Requires `self` for local reference.

tests/Sniffs/Classes/RequireMultiLineMethodSignatureSniffTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ public function testErrorsBasedOnParamCount(): void
6868
self::assertAllFixedInFile($report);
6969
}
7070

71+
public function testErrorsBasedOnPromotedProperties(): void
72+
{
73+
$report = self::checkFile(
74+
__DIR__ . '/data/requireMultiLineMethodSignatureBasedOnPromotedPropertiesErrors.php',
75+
['withPromotedProperties' => true],
76+
);
77+
self::assertSame(1, $report->getErrorCount());
78+
79+
self::assertSniffError($report, 5, RequireMultiLineMethodSignatureSniff::CODE_REQUIRED_MULTI_LINE_SIGNATURE);
80+
81+
self::assertAllFixedInFile($report);
82+
}
83+
7184
public function testForAllMethods(): void
7285
{
7386
$report = self::checkFile(__DIR__ . '/data/requireMultiLineMethodSignatureAllMethodsErrors.php', ['minLineLength' => 0]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php // lint >= 8.0
2+
3+
class A
4+
{
5+
public function __construct(
6+
public int $single
7+
) {
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php // lint >= 8.0
2+
3+
class A
4+
{
5+
public function __construct(public int $single) {
6+
}
7+
}

0 commit comments

Comments
 (0)