diff --git a/Magento2/Sniffs/PHPCompatibility/ForbiddenFinalPrivateMethodsSniff.php b/Magento2/Sniffs/PHPCompatibility/ForbiddenFinalPrivateMethodsSniff.php index 51fa3d69..85b2fe11 100644 --- a/Magento2/Sniffs/PHPCompatibility/ForbiddenFinalPrivateMethodsSniff.php +++ b/Magento2/Sniffs/PHPCompatibility/ForbiddenFinalPrivateMethodsSniff.php @@ -35,6 +35,7 @@ */ class ForbiddenFinalPrivateMethodsSniff extends Sniff { + private const MESSAGE_FINAL = 'Private methods should not be declared as final since PHP 8.0'; /** * Returns an array of tokens this test wants to listen for. @@ -61,10 +62,6 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - if ($this->supportsAbove('8.0') === false) { - return; - } - if (Scopes::isOOMethod($phpcsFile, $stackPtr) === false) { // Function, not method. return; @@ -83,14 +80,18 @@ public function process(File $phpcsFile, $stackPtr) $properties = FunctionDeclarations::getProperties($phpcsFile, $stackPtr); if ($properties['scope'] !== 'private' || $properties['is_final'] === false) { - // Not an private final method. + // Not a private final method. return; } - $phpcsFile->addWarning( - 'Private methods should not be declared as final since PHP 8.0', - $stackPtr, - 'Found' - ); + if ($phpcsFile->addFixableWarning(self::MESSAGE_FINAL, $stackPtr, 'Found') === true) { + $phpcsFile->fixer->beginChangeset(); + $prev = $phpcsFile->findPrevious(\T_FINAL, ($stackPtr - 1)); + $phpcsFile->fixer->replaceToken($prev, null); + // Remove extra space left out by previous replacement + $next = $phpcsFile->findNext(\T_WHITESPACE, $prev); + $phpcsFile->fixer->replaceToken($next, null); + $phpcsFile->fixer->endChangeset(); + } } } diff --git a/Magento2/Tests/PHPCompatibility/ForbiddenFinalPrivateMethodsUnitTest.inc b/Magento2/Tests/PHPCompatibility/ForbiddenFinalPrivateMethodsUnitTest.inc index 486d0584..660c6b57 100644 --- a/Magento2/Tests/PHPCompatibility/ForbiddenFinalPrivateMethodsUnitTest.inc +++ b/Magento2/Tests/PHPCompatibility/ForbiddenFinalPrivateMethodsUnitTest.inc @@ -1,47 +1,27 @@ sniffFile(__FILE__, '8.0'); - $this->assertWarning($file, $line, 'Private methods should not be declared as final since PHP 8.0'); + return []; } /** - * Data provider. - * - * @see testForbiddenFinalPrivateMethods() - * - * @return array + * @inheritdoc */ - public function dataForbiddenFinalPrivateMethods() + public function getWarningList() { - return [ - [34], - [35], - [39], - [40], - [45], - [46], + return [ + 6 => 1, + 7 => 1, + 14 => 1, + 15 => 1, + 23 => 1, + 25 => 1, ]; } - - /** - * Verify the sniff does not throw false positives for valid code. - * - * @dataProvider dataNoFalsePositives - * - * @param int $line The line number. - * - * @return void - */ - public function testNoFalsePositives($line) - { - $file = $this->sniffFile(__FILE__, '8.0'); - $this->assertNoViolation($file, $line); - } - - /** - * Data provider. - * - * @see testNoFalsePositives() - * - * @return array - */ - public function dataNoFalsePositives() - { - $cases = []; - // No errors expected on the first 28 lines. - for ($line = 1; $line <= 28; $line++) { - $cases[] = [$line]; - } - - return $cases; - } - - /** - * Verify no notices are thrown at all. - * - * @return void - */ - public function testNoViolationsInFileOnValidVersion() - { - $file = $this->sniffFile(__FILE__, '7.4'); - $this->assertNoViolation($file); - } }