Skip to content

Sniff for the rule "The use of final keyword is prohibited" #11

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
merged 1 commit into from
Jan 16, 2019
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
36 changes: 36 additions & 0 deletions Magento/Sniffs/PHP/FinalImplementationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\PHP;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

/**
* Magento is a highly extensible and customizable platform. The use of final classes and methods is prohibited.
*/
class FinalImplementationSniff implements Sniff
{
/**
* @inheritdoc
*/
public function register()
{
return [T_FINAL];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$phpcsFile->addError(
// phpcs:ignore Generic.Files.LineLength.TooLong
'Final keyword is prohibited in Magento. It decreases extensibility and is not compatible with plugins and proxies.',
$stackPtr,
'FoundFinal'
);
}
}
13 changes: 13 additions & 0 deletions Magento/Tests/PHP/FinalImplementationUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

final class ImABadExample {}

class ImAlsoABadExample {
final public function badFinalFunction(){}
}

class ImAGoodExample {}

class ImAlsoAGoodExample {
private function emptyFunction(){}
}
33 changes: 33 additions & 0 deletions Magento/Tests/PHP/FinalImplementationUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tests\PHP;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Class FinalImplementationUnitTest
*/
class FinalImplementationUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [
3 => 1,
6 => 1,
];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [];
}
}
3 changes: 3 additions & 0 deletions Magento/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<rule ref="Magento.PHP.DiscouragedFunction">
<severity>8</severity>
</rule>
<rule ref="Magento.PHP.FinalImplementation">
<severity>10</severity>
</rule>
<rule ref="Magento.PHP.Goto">
<severity>10</severity>
</rule>
Expand Down