Skip to content

Commit 80dafb4

Browse files
stlrnzkukulich
authored andcommitted
add 'allowOnSameLine' option to AttributeAndTargetSpacingSniff
1 parent 2963f80 commit 80dafb4

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

SlevomatCodingStandard/Sniffs/Attributes/AttributeAndTargetSpacingSniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class AttributeAndTargetSpacingSniff implements Sniff
2222
/** @var int */
2323
public $linesCount = 0;
2424

25+
/** @var bool */
26+
public $allowOnSameLine = false;
27+
2528
/**
2629
* @return array<int, (int|string)>
2730
*/
@@ -58,6 +61,10 @@ public function process(File $phpcsFile, $attributeOpenerPointer): void
5861
$areOnSameLine = $tokens[$pointerAfter]['line'] === $tokens[$attributeCloserPointer]['line'];
5962

6063
if ($areOnSameLine) {
64+
if ($this->allowOnSameLine) {
65+
return;
66+
}
67+
6168
$errorMessage = $this->linesCount === 1
6269
? 'Expected 1 blank line between attribute and its target, both are on same line.'
6370
: sprintf('Expected %1$d blank lines between attribute and its target, both are on same line.', $this->linesCount);

doc/attributes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Checks lines count between attribute and its target (or target's documentation c
66

77
Sniff provides the following settings:
88

9+
* `allowOnSameLine`: allow attribute and its target to be placed on the same line (default value is false)
910
* `linesCount`: lines count between attribute and its target
1011

1112
#### SlevomatCodingStandard.Attributes.AttributesOrder 🔧

tests/Sniffs/Attributes/AttributeAndTargetSpacingSniffTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ public function testModifiedSettingsErrors(): void
5353
self::assertAllFixedInFile($report);
5454
}
5555

56+
public function testAllowOnSameLineNoErrors(): void
57+
{
58+
$report = self::checkFile(__DIR__ . '/data/attributeAndTargetSpacingAllowOnSameLineNoErrors.php', [
59+
'allowOnSameLine' => true,
60+
]);
61+
self::assertNoSniffErrorInFile($report);
62+
}
63+
64+
public function testAllowOnSameLineErrors(): void
65+
{
66+
$report = self::checkFile(__DIR__ . '/data/attributeAndTargetSpacingAllowOnSameLineErrors.php', [
67+
'allowOnSameLine' => true,
68+
]);
69+
70+
self::assertSame(2, $report->getErrorCount());
71+
72+
self::assertSniffError($report, 11, AttributeAndTargetSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ATTRIBUTE_AND_TARGET);
73+
self::assertSniffError($report, 18, AttributeAndTargetSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ATTRIBUTE_AND_TARGET);
74+
75+
self::assertAllFixedInFile($report);
76+
}
77+
5678
public function testInvalidAttributePriorPhp80(): void
5779
{
5880
$report = self::checkFile(__DIR__ . '/data/attributeAndTargetSpacingInvalidAttribute.php');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.0
2+
3+
/** Class comment */
4+
#[Attribute1, Attribute2('var'), Attribute3(option: PDO::class, option2: true, option3: 'False')]
5+
6+
#[Attribute4(), Attribute5]
7+
#[Attribute6] class Whatever
8+
{
9+
10+
#[Attribute1, Attribute2('var'), Attribute3(option: PDO::class, option2: true, option3: 'False')]
11+
#[Attribute4(), Attribute5]
12+
/**
13+
* Method comment
14+
*/
15+
#[Attribute6] public function method(
16+
#[Attribute1]
17+
$parameter1,
18+
#[Attribute2] $parameter2
19+
)
20+
{
21+
}
22+
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php // lint >= 8.0
2+
3+
/** Class comment */
4+
#[Attribute1, Attribute2('var'), Attribute3(option: PDO::class, option2: true, option3: 'False')]
5+
6+
#[Attribute4(), Attribute5]
7+
#[Attribute6] class Whatever
8+
{
9+
10+
#[Attribute1, Attribute2('var'), Attribute3(option: PDO::class, option2: true, option3: 'False')]
11+
#[Attribute4(), Attribute5]
12+
13+
14+
/**
15+
* Method comment
16+
*/
17+
#[Attribute6] public function method(
18+
#[Attribute1]
19+
20+
21+
22+
$parameter1,
23+
#[Attribute2] $parameter2
24+
)
25+
{
26+
}
27+
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.0
2+
3+
/** Class comment */
4+
#[Attribute1, Attribute2('var'), Attribute3(option: PDO::class, option2: true, option3: 'False')]
5+
6+
#[Attribute4(), Attribute5]
7+
#[Attribute6] class Whatever
8+
{
9+
10+
#[Attribute1, Attribute2('var'), Attribute3(option: PDO::class, option2: true, option3: 'False')]
11+
#[Attribute4(), Attribute5]
12+
/**
13+
* Method comment
14+
*/
15+
#[Attribute6] public function method(
16+
#[Attribute1]
17+
$parameter1,
18+
#[Attribute2] $parameter2
19+
)
20+
{
21+
}
22+
23+
}

0 commit comments

Comments
 (0)