Skip to content

Commit

Permalink
Merge pull request #164 from PHPCSStandards/universal/requirefinalcla…
Browse files Browse the repository at this point in the history
…ss-various-tweaks

Universal/RequireFinalClass: various tweaks
  • Loading branch information
jrfnl committed Dec 1, 2022
2 parents 9feee1f + 8bf00e3 commit f1aa289
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
22 changes: 16 additions & 6 deletions Universal/Sniffs/Classes/RequireFinalClassSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\GetTokensAsString;
use PHPCSUtils\Utils\ObjectDeclarations;

Expand Down Expand Up @@ -71,20 +72,29 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
$phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'not abstract, not final');

$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextNonEmpty === false) {
// Live coding or parse error.
return;
}

$phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'not abstract, not final');
$snippetEnd = $nextNonEmpty;
$classCloser = '';

$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
$snippetEnd = $tokens[$stackPtr]['scope_opener'];
$classCloser = '}';
}

$snippet = GetTokensAsString::compact($phpcsFile, $stackPtr, $tokens[$stackPtr]['scope_opener'], true);
$snippet = GetTokensAsString::compact($phpcsFile, $stackPtr, $snippetEnd, true);
$fix = $phpcsFile->addFixableError(
'A non-abstract class should be declared as final. Found: %s}',
'A non-abstract class should be declared as final. Found: %s%s',
$stackPtr,
'NonFinalClassFound',
[$snippet]
[$snippet, $classCloser]
);

if ($fix === true) {
Expand Down
13 changes: 8 additions & 5 deletions Universal/Tests/Classes/RequireFinalClassUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ abstract class AbstractBarC implements MyInterface {}
$a = new MyClass() {}

// Parse error. Not our concern.
final abstract class BazD {}
final abstract class FinalAbstractClass {}

/*
* Bad.
*/
class BazA {}
class PlainClass {}

class CheckHandlingOfIndentation extends Something {}

class BazC implements MyInterface {}
class ClassImplementing implements MyInterface {}

readonly class BazD {}
readonly class ReadonlyClass {}

// Live coding. Ignore. This must be the last test in the file.
// Live coding/parse error, but not one which concerns us. Add the final keyword.
class LiveCoding

// Live coding. Ignore. This must be the last test in the file.
class
13 changes: 8 additions & 5 deletions Universal/Tests/Classes/RequireFinalClassUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ abstract class AbstractBarC implements MyInterface {}
$a = new MyClass() {}

// Parse error. Not our concern.
final abstract class BazD {}
final abstract class FinalAbstractClass {}

/*
* Bad.
*/
final class BazA {}
final class PlainClass {}

final class CheckHandlingOfIndentation extends Something {}

final class BazC implements MyInterface {}
final class ClassImplementing implements MyInterface {}

readonly final class BazD {}
readonly final class ReadonlyClass {}

// Live coding/parse error, but not one which concerns us. Add the final keyword.
final class LiveCoding

// Live coding. Ignore. This must be the last test in the file.
class LiveCoding
class
1 change: 1 addition & 0 deletions Universal/Tests/Classes/RequireFinalClassUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function getErrorList()
26 => 1,
28 => 1,
30 => 1,
33 => 1,
];
}

Expand Down

0 comments on commit f1aa289

Please sign in to comment.