Skip to content
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

Add support for allowing public readonly properties #1704

Open
wants to merge 2 commits 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 @@ -12,6 +12,8 @@
use function array_merge;
use const T_PRIVATE;
use const T_PROTECTED;
use const T_READONLY;
use const T_SEMICOLON;
use const T_VAR;
use const T_VARIABLE;

Expand All @@ -20,6 +22,9 @@ final class ForbiddenPublicPropertySniff implements Sniff

public const CODE_FORBIDDEN_PUBLIC_PROPERTY = 'ForbiddenPublicProperty';

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

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

Expand Down Expand Up @@ -51,6 +56,10 @@ public function process(File $file, $variablePointer): void
return;
}

if ($this->allowReadonly && $this->isReadonlyProperty($file, $variablePointer)) {
return;
}

$errorMessage = 'Do not use public properties. Use method access instead.';
$file->addError($errorMessage, $variablePointer, self::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}
Expand All @@ -74,4 +83,16 @@ private function getPropertyScopeModifier(File $file, int $position): array
return $file->getTokens()[$scopeModifierPosition];
}

private function isReadonlyProperty(File $file, int $position): bool
{
$readonlyPosition = TokenHelper::findPrevious($file, [T_READONLY], $position - 1);
if ($readonlyPosition === null) {
return false;
}

$semicolonPosition = TokenHelper::findNext($file, [T_SEMICOLON], $readonlyPosition + 1, $position - 1);

return $semicolonPosition === null;
}

}
52 changes: 36 additions & 16 deletions tests/Sniffs/Classes/ForbiddenPublicPropertySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,62 @@
class ForbiddenPublicPropertySniffTest extends TestCase
{

public function testNoErrors(): void
public function testDefault(): void
{
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyNoErrors.php');
self::assertNoSniffErrorInFile($report);
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicProperty.php');

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

public function testErrors(): void
public function testReadonly(): void
{
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyErrors.php');

self::assertSame(3, $report->getErrorCount());
$report = self::checkFile(__DIR__ . '/data/forbiddenPublicPropertyReadonly.php');

self::assertSniffError($report, 5, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 6, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 7, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 8, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 9, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}

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

self::assertSniffError($report, 5, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 6, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 7, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 8, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 9, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 12, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 13, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}

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

self::assertSame(4, $report->getErrorCount());
self::assertSniffError($report, 5, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 6, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 9, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}

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

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);
self::assertSniffError($report, 6, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 9, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
self::assertSniffError($report, 12, ForbiddenPublicPropertySniff::CODE_FORBIDDEN_PUBLIC_PROPERTY);
}

}
15 changes: 15 additions & 0 deletions tests/Sniffs/Classes/data/forbiddenPublicProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class Test
{
var $var;
public $pub;
protected $prot;
private $priv;
}

class TestSniff
{
var $var;
public $pub;
}
8 changes: 0 additions & 8 deletions tests/Sniffs/Classes/data/forbiddenPublicPropertyErrors.php

This file was deleted.

20 changes: 0 additions & 20 deletions tests/Sniffs/Classes/data/forbiddenPublicPropertyNoErrors.php

This file was deleted.

This file was deleted.

This file was deleted.

24 changes: 24 additions & 0 deletions tests/Sniffs/Classes/data/forbiddenPublicPropertyReadonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 8.1

class Test
{
var $var;
public $pub;
readonly public string $rps;
public readonly string $prs;
public $pub2;

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

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