Skip to content

AC-2222: [UCT Autofixing] PHPCompatibility.ForbiddenFinalPrivateMethods #373

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

Merged
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 @@ -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.
Expand All @@ -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;
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
<?php

/*
* OK cross version.
*/
class CrossVersionValid
final class CrossVersionInValid
{
private final function __construct() {}
final public function publicFinal() {}
final protected static function protectedFinal() {}
private function privateNonOverloadable() {}
}

trait CrossVersionValidTrait
{
final private function __CONSTRUCT() {}
final public static function publicFinal() {}
final protected function protectedFinal() {}
private function privateStillOverloadable() {} // Open question in RFC PR https://github.com/php/php-src/pull/5401
}

$anon = new class() {
final private function __Construct() {}
final public static function publicFinal();
final protected function protectedFinal();
private function privateNonOverloadable() {}
};

/*
* PHP 8.0: private methods cannot be final as they are never overridden by other classes.
*/
class CrossVersionInValid
{
final private function privateFinal();
static private final function privateStaticFinal();
static protected final function protectedStaticFinal();
final protected function protectedFinal();
}

$anon = new class() {
private final function __construct() {}
final private function privateFinal();
static final private function privateStaticFinal();
final protected function protectedFinal();
static final protected function protectedStaticFinal();
};

trait CrossVersionInValidTrait
{
private final function __construct() {}
final private function privateFinal();
final protected function protectedFinal();
static private final function privateStaticFinal();
static protected final function protectedStaticFinal();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

final class CrossVersionInValid
{
private final function __construct() {}
private function privateFinal();
static private function privateStaticFinal();
static protected final function protectedStaticFinal();
final protected function protectedFinal();
}

$anon = new class() {
private final function __construct() {}
private function privateFinal();
static private function privateStaticFinal();
final protected function protectedFinal();
static final protected function protectedStaticFinal();
};

trait CrossVersionInValidTrait
{
private final function __construct() {}
private function privateFinal();
final protected function protectedFinal();
static private function privateStaticFinal();
static protected final function protectedStaticFinal();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Magento2\Tests\PHPCompatibility;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Test the ForbiddenFinalPrivateMethods sniff.
*
Expand All @@ -20,84 +22,28 @@
*
* @since 10.0.0
*/
class ForbiddenFinalPrivateMethodsUnitTest extends BaseSniffTest
class ForbiddenFinalPrivateMethodsUnitTest extends AbstractSniffUnitTest
{

/**
* Verify that the sniff throws a warning for non-construct final private methods for PHP 8.0+.
*
* @dataProvider dataForbiddenFinalPrivateMethods
*
* @param int $line The line number where a warning is expected.
*
* @return void
* @inheritdoc
*/
public function testForbiddenFinalPrivateMethods($line)
public function getErrorList()
{
$file = $this->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);
}
}