Skip to content

Commit 4c8716c

Browse files
authored
Merge pull request #156 from magento-commerce/imported-svera-magento-coding-standard-373
[Imported] AC-2222: [UCT Autofixing] PHPCompatibility.ForbiddenFinalPrivateMethods
2 parents 9b40157 + 6d9a5c9 commit 4c8716c

File tree

4 files changed

+62
-108
lines changed

4 files changed

+62
-108
lines changed

Magento2/Sniffs/PHPCompatibility/ForbiddenFinalPrivateMethodsSniff.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*/
3636
class ForbiddenFinalPrivateMethodsSniff extends Sniff
3737
{
38+
private const MESSAGE_FINAL = 'Private methods should not be declared as final since PHP 8.0';
3839

3940
/**
4041
* Returns an array of tokens this test wants to listen for.
@@ -61,10 +62,6 @@ public function register()
6162
*/
6263
public function process(File $phpcsFile, $stackPtr)
6364
{
64-
if ($this->supportsAbove('8.0') === false) {
65-
return;
66-
}
67-
6865
if (Scopes::isOOMethod($phpcsFile, $stackPtr) === false) {
6966
// Function, not method.
7067
return;
@@ -83,14 +80,18 @@ public function process(File $phpcsFile, $stackPtr)
8380

8481
$properties = FunctionDeclarations::getProperties($phpcsFile, $stackPtr);
8582
if ($properties['scope'] !== 'private' || $properties['is_final'] === false) {
86-
// Not an private final method.
83+
// Not a private final method.
8784
return;
8885
}
8986

90-
$phpcsFile->addWarning(
91-
'Private methods should not be declared as final since PHP 8.0',
92-
$stackPtr,
93-
'Found'
94-
);
87+
if ($phpcsFile->addFixableWarning(self::MESSAGE_FINAL, $stackPtr, 'Found') === true) {
88+
$phpcsFile->fixer->beginChangeset();
89+
$prev = $phpcsFile->findPrevious(\T_FINAL, ($stackPtr - 1));
90+
$phpcsFile->fixer->replaceToken($prev, null);
91+
// Remove extra space left out by previous replacement
92+
$next = $phpcsFile->findNext(\T_WHITESPACE, $prev);
93+
$phpcsFile->fixer->replaceToken($next, null);
94+
$phpcsFile->fixer->endChangeset();
95+
}
9596
}
9697
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
11
<?php
22

3-
/*
4-
* OK cross version.
5-
*/
6-
class CrossVersionValid
3+
final class CrossVersionInValid
74
{
85
private final function __construct() {}
9-
final public function publicFinal() {}
10-
final protected static function protectedFinal() {}
11-
private function privateNonOverloadable() {}
12-
}
13-
14-
trait CrossVersionValidTrait
15-
{
16-
final private function __CONSTRUCT() {}
17-
final public static function publicFinal() {}
18-
final protected function protectedFinal() {}
19-
private function privateStillOverloadable() {} // Open question in RFC PR https://github.com/php/php-src/pull/5401
20-
}
21-
22-
$anon = new class() {
23-
final private function __Construct() {}
24-
final public static function publicFinal();
25-
final protected function protectedFinal();
26-
private function privateNonOverloadable() {}
27-
};
28-
29-
/*
30-
* PHP 8.0: private methods cannot be final as they are never overridden by other classes.
31-
*/
32-
class CrossVersionInValid
33-
{
346
final private function privateFinal();
357
static private final function privateStaticFinal();
8+
static protected final function protectedStaticFinal();
9+
final protected function protectedFinal();
3610
}
3711

3812
$anon = new class() {
13+
private final function __construct() {}
3914
final private function privateFinal();
4015
static final private function privateStaticFinal();
16+
final protected function protectedFinal();
17+
static final protected function protectedStaticFinal();
4118
};
4219

4320
trait CrossVersionInValidTrait
4421
{
22+
private final function __construct() {}
4523
final private function privateFinal();
24+
final protected function protectedFinal();
4625
static private final function privateStaticFinal();
26+
static protected final function protectedStaticFinal();
4727
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
final class CrossVersionInValid
4+
{
5+
private final function __construct() {}
6+
private function privateFinal();
7+
static private function privateStaticFinal();
8+
static protected final function protectedStaticFinal();
9+
final protected function protectedFinal();
10+
}
11+
12+
$anon = new class() {
13+
private final function __construct() {}
14+
private function privateFinal();
15+
static private function privateStaticFinal();
16+
final protected function protectedFinal();
17+
static final protected function protectedStaticFinal();
18+
};
19+
20+
trait CrossVersionInValidTrait
21+
{
22+
private final function __construct() {}
23+
private function privateFinal();
24+
final protected function protectedFinal();
25+
static private function privateStaticFinal();
26+
static protected final function protectedStaticFinal();
27+
}

Magento2/Tests/PHPCompatibility/ForbiddenFinalPrivateMethodsUnitTest.php

+15-69
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Magento2\Tests\PHPCompatibility;
1212

13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
14+
1315
/**
1416
* Test the ForbiddenFinalPrivateMethods sniff.
1517
*
@@ -20,84 +22,28 @@
2022
*
2123
* @since 10.0.0
2224
*/
23-
class ForbiddenFinalPrivateMethodsUnitTest extends BaseSniffTest
25+
class ForbiddenFinalPrivateMethodsUnitTest extends AbstractSniffUnitTest
2426
{
25-
2627
/**
27-
* Verify that the sniff throws a warning for non-construct final private methods for PHP 8.0+.
28-
*
29-
* @dataProvider dataForbiddenFinalPrivateMethods
30-
*
31-
* @param int $line The line number where a warning is expected.
32-
*
33-
* @return void
28+
* @inheritdoc
3429
*/
35-
public function testForbiddenFinalPrivateMethods($line)
30+
public function getErrorList()
3631
{
37-
$file = $this->sniffFile(__FILE__, '8.0');
38-
$this->assertWarning($file, $line, 'Private methods should not be declared as final since PHP 8.0');
32+
return [];
3933
}
4034

4135
/**
42-
* Data provider.
43-
*
44-
* @see testForbiddenFinalPrivateMethods()
45-
*
46-
* @return array
36+
* @inheritdoc
4737
*/
48-
public function dataForbiddenFinalPrivateMethods()
38+
public function getWarningList()
4939
{
50-
return [
51-
[34],
52-
[35],
53-
[39],
54-
[40],
55-
[45],
56-
[46],
40+
return [
41+
6 => 1,
42+
7 => 1,
43+
14 => 1,
44+
15 => 1,
45+
23 => 1,
46+
25 => 1,
5747
];
5848
}
59-
60-
/**
61-
* Verify the sniff does not throw false positives for valid code.
62-
*
63-
* @dataProvider dataNoFalsePositives
64-
*
65-
* @param int $line The line number.
66-
*
67-
* @return void
68-
*/
69-
public function testNoFalsePositives($line)
70-
{
71-
$file = $this->sniffFile(__FILE__, '8.0');
72-
$this->assertNoViolation($file, $line);
73-
}
74-
75-
/**
76-
* Data provider.
77-
*
78-
* @see testNoFalsePositives()
79-
*
80-
* @return array
81-
*/
82-
public function dataNoFalsePositives()
83-
{
84-
$cases = [];
85-
// No errors expected on the first 28 lines.
86-
for ($line = 1; $line <= 28; $line++) {
87-
$cases[] = [$line];
88-
}
89-
90-
return $cases;
91-
}
92-
93-
/**
94-
* Verify no notices are thrown at all.
95-
*
96-
* @return void
97-
*/
98-
public function testNoViolationsInFileOnValidVersion()
99-
{
100-
$file = $this->sniffFile(__FILE__, '7.4');
101-
$this->assertNoViolation($file);
102-
}
10349
}

0 commit comments

Comments
 (0)