Skip to content

Commit 1ad070f

Browse files
committed
Sniff for the rule "The use of final keyword is prohibited"
1 parent 38094be commit 1ad070f

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\PHP;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Magento is a highly extensible and customizable platform. The use of final classes and methods is prohibited.
13+
*/
14+
class FinalImplementationSniff implements Sniff
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function register()
20+
{
21+
return [T_FINAL];
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function process(File $phpcsFile, $stackPtr)
28+
{
29+
$phpcsFile->addError(
30+
// phpcs:ignore Generic.Files.LineLength.TooLong
31+
'Final keyword is prohibited in Magento. It decreases extensibility and is not compatible with plugins and proxies.',
32+
$stackPtr,
33+
'FoundFinal'
34+
);
35+
}
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
final class ImABadExample {}
4+
5+
class ImAlsoABadExample {
6+
final public function badFinalFunction(){}
7+
}
8+
9+
class ImAGoodExample {}
10+
11+
class ImAlsoAGoodExample {
12+
private function emptyFunction(){}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tests\PHP;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class FinalImplementationUnitTest
12+
*/
13+
class FinalImplementationUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [
21+
3 => 1,
22+
6 => 1,
23+
];
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function getWarningList()
30+
{
31+
return [];
32+
}
33+
}

Magento/ruleset.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
<rule ref="Magento.PHP.DiscouragedFunction">
105105
<severity>8</severity>
106106
</rule>
107+
<rule ref="Magento.PHP.FinalImplementation">
108+
<severity>10</severity>
109+
</rule>
107110
<rule ref="Magento.PHP.Goto">
108111
<severity>10</severity>
109112
</rule>

0 commit comments

Comments
 (0)