Skip to content

Commit b4f9f02

Browse files
vojtech-dobeskukulich
authored andcommitted
SlevomatCodingStandard.Classes.PropertySpacing and SlevomatCodingStandard.Classes.ConstantSpacing: New options "minLinesCountBeforeMultiline" and "maxLinesCountBeforeMultiline"
1 parent f533cef commit b4f9f02

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

SlevomatCodingStandard/Sniffs/Classes/AbstractPropertyConstantAndEnumCaseSpacing.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use SlevomatCodingStandard\Helpers\TokenHelper;
1111
use function assert;
1212
use function in_array;
13+
use function max;
14+
use function min;
1315
use function str_repeat;
1416
use const T_ATTRIBUTE;
1517
use const T_COMMENT;
@@ -36,6 +38,10 @@ abstract class AbstractPropertyConstantAndEnumCaseSpacing implements Sniff
3638

3739
public int $maxLinesCountBeforeWithoutComment = 1;
3840

41+
public ?int $minLinesCountBeforeMultiline = null;
42+
43+
public ?int $maxLinesCountBeforeMultiline = null;
44+
3945
abstract protected function isNextMemberValid(File $phpcsFile, int $pointer): bool;
4046

4147
abstract protected function addError(File $phpcsFile, int $pointer, int $min, int $max, int $found): bool;
@@ -50,6 +56,8 @@ public function process(File $phpcsFile, $pointer): int
5056
$this->maxLinesCountBeforeWithComment = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithComment);
5157
$this->minLinesCountBeforeWithoutComment = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithoutComment);
5258
$this->maxLinesCountBeforeWithoutComment = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithoutComment);
59+
$this->minLinesCountBeforeMultiline = SniffSettingsHelper::normalizeNullableInteger($this->minLinesCountBeforeMultiline);
60+
$this->maxLinesCountBeforeMultiline = SniffSettingsHelper::normalizeNullableInteger($this->maxLinesCountBeforeMultiline);
5361

5462
$tokens = $phpcsFile->getTokens();
5563

@@ -89,6 +97,23 @@ public function process(File $phpcsFile, $pointer): int
8997
$maxExpectedLines = $this->maxLinesCountBeforeWithoutComment;
9098
}
9199

100+
if (
101+
$this->minLinesCountBeforeMultiline !== null
102+
&& !$this instanceof EnumCaseSpacingSniff
103+
&& $tokens[$pointer]['line'] !== $tokens[$endPointer]['line']
104+
) {
105+
$minExpectedLines = max($minExpectedLines, $this->minLinesCountBeforeMultiline);
106+
$maxExpectedLines = max($minExpectedLines, $maxExpectedLines);
107+
}
108+
109+
if (
110+
$this->maxLinesCountBeforeMultiline !== null
111+
&& !$this instanceof EnumCaseSpacingSniff
112+
&& $tokens[$pointer]['line'] !== $tokens[$endPointer]['line']
113+
) {
114+
$maxExpectedLines = max($minExpectedLines, min($maxExpectedLines, $this->maxLinesCountBeforeMultiline));
115+
}
116+
92117
if ($linesBetween >= $minExpectedLines && $linesBetween <= $maxExpectedLines) {
93118
return $firstOnLinePointer;
94119
}

doc/classes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Sniff provides the following settings:
113113
* `maxLinesCountBeforeWithComment`: maximum number of lines before constant with a documentation comment or attribute
114114
* `minLinesCountBeforeWithoutComment`: minimum number of lines before constant without a documentation comment or attribute
115115
* `maxLinesCountBeforeWithoutComment`: maximum number of lines before constant without a documentation comment or attribute
116+
* `minLinesCountBeforeMultiline` (default: `null`): minimum number of lines before multiline constant
117+
* `maxLinesCountBeforeMultiline` (default: `null`): maximum number of lines before multiline constant
116118

117119
#### SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion
118120

@@ -216,6 +218,8 @@ Sniff provides the following settings:
216218
* `maxLinesCountBeforeWithComment`: maximum number of lines before property with a documentation comment or attribute
217219
* `minLinesCountBeforeWithoutComment`: minimum number of lines before property without a documentation comment or attribute
218220
* `maxLinesCountBeforeWithoutComment`: maximum number of lines before property without a documentation comment or attribute
221+
* `minLinesCountBeforeMultiline` (default: `null`): minimum number of lines before multiline property
222+
* `maxLinesCountBeforeMultiline` (default: `null`): maximum number of lines before multiline property
219223

220224
#### SlevomatCodingStandard.Classes.RequireAbstractOrFinal 🔧
221225

tests/Sniffs/Classes/ConstantSpacingSniffTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public function testErrorsWithModifiedLinesCount(): void
4444
self::assertSniffError($report, 26, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
4545
}
4646

47+
public function testErrorsBasedOnMultiline(): void
48+
{
49+
$report = self::checkFile(__DIR__ . '/data/constantSpacingMultilineErrors.php', [
50+
'minLinesCountBeforeMultiline' => 1,
51+
'minLinesCountBeforeWithComment' => 2,
52+
'maxLinesCountBeforeWithComment' => 2,
53+
]);
54+
55+
self::assertSame(2, $report->getErrorCount());
56+
57+
self::assertSniffError($report, 4, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
58+
self::assertSniffError($report, 9, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
59+
60+
self::assertAllFixedInFile($report);
61+
}
62+
4763
public function testInGlobalNamespaceNoErrors(): void
4864
{
4965
$report = self::checkFile(__DIR__ . '/data/constantSpacingInGlobalNamespaceNoErrors.php');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.2
2+
3+
abstract class Foo {
4+
const ItemsA = [
5+
'A',
6+
'B',
7+
'C',
8+
];
9+
10+
const ItemsB = [
11+
'A',
12+
'B',
13+
'C',
14+
];
15+
16+
17+
/** @var array<string> */
18+
const ItemsC = [
19+
'A',
20+
'B',
21+
'C',
22+
];
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php // lint >= 8.2
2+
3+
abstract class Foo {
4+
const ItemsA = [
5+
'A',
6+
'B',
7+
'C',
8+
];
9+
const ItemsB = [
10+
'A',
11+
'B',
12+
'C',
13+
];
14+
/** @var array<string> */
15+
const ItemsC = [
16+
'A',
17+
'B',
18+
'C',
19+
];
20+
}

0 commit comments

Comments
 (0)