Skip to content

Commit

Permalink
SlevomatCodingStandard.Classes.ForbiddenPublicProperty: New option "c…
Browse files Browse the repository at this point in the history
…heckPromoted" to enable check of promoted properties
  • Loading branch information
kukulich committed Jun 24, 2022
1 parent 10b01f8 commit bf55f29
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -187,6 +187,10 @@ Disallows late static binding for constants.

Disallows using public properties.

This sniff provides the following setting:

* `checkPromoted`: will check promoted properties too.

#### SlevomatCodingStandard.Classes.RequireAbstractOrFinal 🔧

Requires the class to be declared either as abstract or as final.
Expand Down
Expand Up @@ -20,6 +20,9 @@ final class ForbiddenPublicPropertySniff implements Sniff

public const CODE_FORBIDDEN_PUBLIC_PROPERTY = 'ForbiddenPublicProperty';

/** @var bool */
public $checkPromoted = false;

/**
* @return array<int, (int|string)>
*/
Expand All @@ -34,7 +37,7 @@ public function register(): array
*/
public function process(File $file, $variablePointer): void
{
if (!PropertyHelper::isProperty($file, $variablePointer)) {
if (!PropertyHelper::isProperty($file, $variablePointer, $this->checkPromoted)) {
return;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Sniffs/Classes/ForbiddenPublicPropertySniffTest.php
Expand Up @@ -24,4 +24,25 @@ public function testErrors(): void
self::assertSniffError($report, 7, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}

public function testPromotedNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyPromotedNoErrors.php', [
'checkPromoted' => true,
]);
self::assertNoSniffErrorInFile($report);
}

public function testPromotedErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyPromotedErrors.php', [
'checkPromoted' => true,
]);

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

self::assertSniffError($report, 5, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 13, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 14, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}

}
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.1

function add(int $a, int $b)
{
Expand All @@ -8,6 +8,10 @@ function add(int $a, int $b)
class Test
{
private $one, $two;

public function __construct(public int $promoted)
{
}
}

class TestSniff
Expand Down
@@ -0,0 +1,18 @@
<?php // lint >= 8.1

class Test
{
public function __construct(public int $promoted1, readonly public string $promoted2)
{
}
}

class Test2
{
public function __construct(
public int $promoted3,
readonly public string $promoted4,
)
{
}
}
@@ -0,0 +1,8 @@
<?php // lint >= 8.1

class Test
{
public function __construct(protected int $promoted1, readonly private string $promoted2)
{
}
}

0 comments on commit bf55f29

Please sign in to comment.