diff --git a/composer.json b/composer.json index 5027228..a70c535 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,11 @@ "symplify/easy-coding-standard": "^10.0.6", "nikic/php-parser": "^4.0" }, + "autoload": { + "psr-4": { + "JumpTwentyFour\\PhpCodingStandards\\": "src" + } + }, "prefer-stable": true, "minimum-stability": "dev", "config": { diff --git a/ecs.php b/ecs.php index 9eefda8..05d6ee7 100644 --- a/ecs.php +++ b/ecs.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use JumpTwentyFour\PhpCodingStandards\Sniffs\FinalControllerSniff; use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UnusedFunctionParameterSniff; use PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff; use PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff; @@ -152,5 +153,6 @@ $services->set(MethodSpacingSniff::class); $services->set(BackedEnumTypeSpacingSniff::class); $services->set(UnusedFunctionParameterSniff::class); + $services->set(FinalControllerSniff::class); $services->set(ValidVariableNameSniff::class); }; diff --git a/src/Sniffs/FinalControllerSniff.php b/src/Sniffs/FinalControllerSniff.php new file mode 100644 index 0000000..395e6e5 --- /dev/null +++ b/src/Sniffs/FinalControllerSniff.php @@ -0,0 +1,60 @@ +getTokens(); + + $className = $phpcsFile->getDeclarationName($classPointer); + + if ($className === 'Controller') { + return; + } + + if (str_contains($className, 'Test')) { + return; + } + + if (str_contains($className, 'Controller') === false) { + return; + } + + if ($tokens[$previousPointer]['code'] === T_FINAL) { + return; + } + + $fix = $phpcsFile->addFixableError( + 'All controllers should be declared using "final" keyword.', + $classPointer - 1, + self::CONTROLLER_NOT_FINAL + ); + + if ($fix === false) { + return; + } + + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->addContent($classPointer - 1, 'final '); + $phpcsFile->fixer->endChangeset(); + } +}