Skip to content

Allow targeting multiline elements in AbstractPropertyConstantAndEnumCaseSpacing #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use SlevomatCodingStandard\Helpers\TokenHelper;
use function assert;
use function in_array;
use function max;
use function min;
use function str_repeat;
use const T_ATTRIBUTE;
use const T_COMMENT;
Expand All @@ -28,6 +30,10 @@
abstract class AbstractPropertyConstantAndEnumCaseSpacing implements Sniff
{

public int $minLinesCountBeforeMultiline = -1;

public int $maxLinesCountBeforeMultiline = -1;

public int $minLinesCountBeforeWithComment = 1;

public int $maxLinesCountBeforeWithComment = 1;
Expand All @@ -46,6 +52,8 @@ abstract protected function addError(File $phpcsFile, int $pointer, int $min, in
*/
public function process(File $phpcsFile, $pointer): int
{
$this->minLinesCountBeforeMultiline = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeMultiline);
$this->maxLinesCountBeforeMultiline = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeMultiline);
$this->minLinesCountBeforeWithComment = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithComment);
$this->maxLinesCountBeforeWithComment = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithComment);
$this->minLinesCountBeforeWithoutComment = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithoutComment);
Expand Down Expand Up @@ -89,6 +97,21 @@ public function process(File $phpcsFile, $pointer): int
$maxExpectedLines = $this->maxLinesCountBeforeWithoutComment;
}

if (
$this->minLinesCountBeforeMultiline !== -1
&& $tokens[$pointer]['line'] !== $tokens[$endPointer]['line']
) {
$minExpectedLines = max($minExpectedLines, $this->minLinesCountBeforeMultiline);
$maxExpectedLines = max($minExpectedLines, $maxExpectedLines);
}

if (
$this->maxLinesCountBeforeMultiline !== -1
&& $tokens[$pointer]['line'] !== $tokens[$endPointer]['line']
) {
$maxExpectedLines = max($minExpectedLines, min($maxExpectedLines, $this->maxLinesCountBeforeMultiline));
}

if ($linesBetween >= $minExpectedLines && $linesBetween <= $maxExpectedLines) {
return $firstOnLinePointer;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Sniffs/Classes/ConstantSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ public function testErrorsWithModifiedLinesCount(): void
self::assertSniffError($report, 26, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
}

public function testErrorsBasedOnMultiline(): void
{
$report = self::checkFile(__DIR__ . '/data/constantSpacingMultilineErrors.php', [
'minLinesCountBeforeMultiline' => 1,
'minLinesCountBeforeWithComment' => 2,
'maxLinesCountBeforeWithComment' => 2,
]);

self::assertSame(2, $report->getErrorCount());

self::assertSniffError($report, 4, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
self::assertSniffError($report, 9, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);

self::assertAllFixedInFile($report);
}

public function testInGlobalNamespaceNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/constantSpacingInGlobalNamespaceNoErrors.php');
Expand Down
23 changes: 23 additions & 0 deletions tests/Sniffs/Classes/data/constantSpacingMultilineErrors.fixed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php // lint >= 8.2

abstract class Foo {
const ItemsA = [
'A',
'B',
'C',
];

const ItemsB = [
'A',
'B',
'C',
];


/** @var array<string> */
const ItemsC = [
'A',
'B',
'C',
];
}
20 changes: 20 additions & 0 deletions tests/Sniffs/Classes/data/constantSpacingMultilineErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.2

abstract class Foo {
const ItemsA = [
'A',
'B',
'C',
];
const ItemsB = [
'A',
'B',
'C',
];
/** @var array<string> */
const ItemsC = [
'A',
'B',
'C',
];
}