Skip to content

Commit ff4cafd

Browse files
committed
SlevomatCodingStandard.Classes.PropertyDeclaration: New option "checkPromoted" to enable check of promoted properties
1 parent 433c093 commit ff4cafd

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ Sniff provides the following settings:
547547
Sniff provides the following settings:
548548

549549
* `modifiersOrder`: allows to configurure order of modifiers.
550+
* `checkPromoted`: will check promoted properties too.
550551

551552
#### SlevomatCodingStandard.Classes.PropertySpacing 🔧
552553

SlevomatCodingStandard/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class PropertyDeclarationSniff implements Sniff
5252
/** @var string[]|null */
5353
public $modifiersOrder = [];
5454

55+
/** @var bool */
56+
public $checkPromoted = false;
57+
5558
/** @var array<int, array<int, (int|string)>>|null */
5659
private $normalizedModifiersOrder = null;
5760

@@ -88,7 +91,7 @@ public function process(File $phpcsFile, $modifierPointer): void
8891
return;
8992
}
9093

91-
if (!PropertyHelper::isProperty($phpcsFile, $propertyPointer)) {
94+
if (!PropertyHelper::isProperty($phpcsFile, $propertyPointer, $this->checkPromoted)) {
9295
return;
9396
}
9497

tests/Sniffs/Classes/PropertyDeclarationSniffTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ public function testErrors(): void
4141
self::assertAllFixedInFile($report);
4242
}
4343

44+
public function testPromotedNoErrors(): void
45+
{
46+
$report = self::checkFile(__DIR__ . '/data/propertyDeclarationPromotedNoErrors.php', [
47+
'checkPromoted' => true,
48+
]);
49+
self::assertNoSniffErrorInFile($report);
50+
}
51+
52+
public function testPromotedErrors(): void
53+
{
54+
$report = self::checkFile(__DIR__ . '/data/propertyDeclarationPromotedErrors.php', [
55+
'checkPromoted' => true,
56+
]);
57+
58+
self::assertSame(7, $report->getErrorCount());
59+
60+
self::assertSniffError($report, 6, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT);
61+
self::assertSniffError($report, 6, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BETWEEN_TYPE_HINT_AND_PROPERTY);
62+
self::assertSniffError($report, 6, PropertyDeclarationSniff::CODE_INCORRECT_ORDER_OF_MODIFIERS);
63+
self::assertSniffError($report, 16, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT);
64+
self::assertSniffError($report, 16, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BETWEEN_TYPE_HINT_AND_PROPERTY);
65+
self::assertSniffError($report, 17, PropertyDeclarationSniff::CODE_INCORRECT_ORDER_OF_MODIFIERS);
66+
67+
self::assertAllFixedInFile($report);
68+
}
69+
4470
public function testModifiedModifiersOrderNoErrors(): void
4571
{
4672
$report = self::checkFile(__DIR__ . '/data/propertyDeclarationModifiedModifiersOrderNoErrors.php', [
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.1
2+
3+
class Whatever
4+
{
5+
6+
public function __construct(private int $promotion1, public readonly int $promotion2)
7+
{
8+
}
9+
10+
}
11+
12+
class Anything
13+
{
14+
15+
public function __construct(
16+
private int $promotion3,
17+
protected readonly Foo|Bar|null $promotion4,
18+
)
19+
{
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.1
2+
3+
class Whatever
4+
{
5+
6+
public function __construct(private int $promotion1, readonly public int $promotion2)
7+
{
8+
}
9+
10+
}
11+
12+
class Anything
13+
{
14+
15+
public function __construct(
16+
private int $promotion3,
17+
readonly protected Foo|Bar|null $promotion4,
18+
)
19+
{
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.1
2+
3+
class Whatever
4+
{
5+
6+
public function __construct(private int $promotion1, public readonly int $promotion2)
7+
{
8+
}
9+
10+
}
11+
12+
class Anything
13+
{
14+
15+
public function __construct(
16+
private int $promotion3,
17+
public readonly Foo|Bar|null $promotion4,
18+
)
19+
{
20+
}
21+
22+
}

0 commit comments

Comments
 (0)