Skip to content

Commit bf55f29

Browse files
committed
SlevomatCodingStandard.Classes.ForbiddenPublicProperty: New option "checkPromoted" to enable check of promoted properties
1 parent 10b01f8 commit bf55f29

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ Disallows late static binding for constants.
187187

188188
Disallows using public properties.
189189

190+
This sniff provides the following setting:
191+
192+
* `checkPromoted`: will check promoted properties too.
193+
190194
#### SlevomatCodingStandard.Classes.RequireAbstractOrFinal 🔧
191195

192196
Requires the class to be declared either as abstract or as final.

SlevomatCodingStandard/Sniffs/Classes/ForbiddenPublicPropertySniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ final class ForbiddenPublicPropertySniff implements Sniff
2020

2121
public const CODE_FORBIDDEN_PUBLIC_PROPERTY = 'ForbiddenPublicProperty';
2222

23+
/** @var bool */
24+
public $checkPromoted = false;
25+
2326
/**
2427
* @return array<int, (int|string)>
2528
*/
@@ -34,7 +37,7 @@ public function register(): array
3437
*/
3538
public function process(File $file, $variablePointer): void
3639
{
37-
if (!PropertyHelper::isProperty($file, $variablePointer)) {
40+
if (!PropertyHelper::isProperty($file, $variablePointer, $this->checkPromoted)) {
3841
return;
3942
}
4043

tests/Sniffs/Classes/ForbiddenPublicPropertySniffTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,25 @@ public function testErrors(): void
2424
self::assertSniffError($report, 7, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
2525
}
2626

27+
public function testPromotedNoErrors(): void
28+
{
29+
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyPromotedNoErrors.php', [
30+
'checkPromoted' => true,
31+
]);
32+
self::assertNoSniffErrorInFile($report);
33+
}
34+
35+
public function testPromotedErrors(): void
36+
{
37+
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyPromotedErrors.php', [
38+
'checkPromoted' => true,
39+
]);
40+
41+
self::assertSame(4, $report->getErrorCount());
42+
43+
self::assertSniffError($report, 5, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
44+
self::assertSniffError($report, 13, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
45+
self::assertSniffError($report, 14, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
46+
}
47+
2748
}

tests/Sniffs/Classes/data/forbiddenPublicPropertyNoErrors.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.1
22

33
function add(int $a, int $b)
44
{
@@ -8,6 +8,10 @@ function add(int $a, int $b)
88
class Test
99
{
1010
private $one, $two;
11+
12+
public function __construct(public int $promoted)
13+
{
14+
}
1115
}
1216

1317
class TestSniff
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php // lint >= 8.1
2+
3+
class Test
4+
{
5+
public function __construct(public int $promoted1, readonly public string $promoted2)
6+
{
7+
}
8+
}
9+
10+
class Test2
11+
{
12+
public function __construct(
13+
public int $promoted3,
14+
readonly public string $promoted4,
15+
)
16+
{
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php // lint >= 8.1
2+
3+
class Test
4+
{
5+
public function __construct(protected int $promoted1, readonly private string $promoted2)
6+
{
7+
}
8+
}

0 commit comments

Comments
 (0)