Skip to content

Commit

Permalink
SlevomatCodingStandard.Classes.RequireAbstractOrFinal: Fixed false po…
Browse files Browse the repository at this point in the history
…sitive for readonly classes
  • Loading branch information
kukulich committed Jan 9, 2023
1 parent ed5b2aa commit 6460e72
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use const T_ABSTRACT;
use const T_CLASS;
use const T_FINAL;
use const T_READONLY;

class RequireAbstractOrFinalSniff implements Sniff
{
Expand All @@ -31,17 +32,21 @@ public function register(): array
*/
public function process(File $phpcsFile, $classPointer): void
{
$tokens = $phpcsFile->getTokens();

$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $classPointer - 1);

$tokens = $phpcsFile->getTokens();
if ($tokens[$previousPointer]['code'] === T_READONLY) {
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $previousPointer - 1);
}

if (in_array($tokens[$previousPointer]['code'], [T_ABSTRACT, T_FINAL], true)) {
return;
}

$fix = $phpcsFile->addFixableError(
'All classes should be declared using either the "abstract" or "final" keyword.',
$classPointer - 1,
$classPointer,
self::CODE_NO_ABSTRACT_OR_FINAL
);

Expand All @@ -50,7 +55,7 @@ public function process(File $phpcsFile, $classPointer): void
}

$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($classPointer - 1, 'final ');
$phpcsFile->fixer->addContentBefore($classPointer, 'final ');
$phpcsFile->fixer->endChangeset();
}

Expand Down
9 changes: 5 additions & 4 deletions tests/Sniffs/Classes/RequireAbstractOrFinalSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireAbstractOrFinalErrors.php');

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

self::assertSniffError($report, 2, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 17, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 38, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 3, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 18, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 39, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 44, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);
self::assertSniffError($report, 53, RequireAbstractOrFinalSniff::CODE_NO_ABSTRACT_OR_FINAL);

self::assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.2

final class C1
{
Expand Down Expand Up @@ -45,3 +45,11 @@ final class C2
{

}

final readonly class G
{
}

readonly final class G2
{
}
10 changes: 9 additions & 1 deletion tests/Sniffs/Classes/data/requireAbstractOrFinalErrors.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.2

class C1
{
Expand Down Expand Up @@ -45,3 +45,11 @@ class C2
{

}

final readonly class G
{
}

readonly class G2
{
}

0 comments on commit 6460e72

Please sign in to comment.