Skip to content

Commit

Permalink
Merge pull request #245 from PHPCSStandards/universal/finalmethodsint…
Browse files Browse the repository at this point in the history
…raits-change-property-to-errorcode

Universal/RequireFinalMethodsInTraits: change category + remove property, use errorcode instead
  • Loading branch information
jrfnl committed Jun 18, 2023
2 parents 1d70047 + 787f3f9 commit 7b19d08
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<standard>
<![CDATA[
Requires the use of the `final` keyword for non-abstract, non-private methods in traits.
By default, magic methods are exempt from this check. Magic methods can be enforced to be `final` too by setting the `includeMagicMethods` property to `true`.
]]>
</standard>
<code_comparison>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @link https://github.com/PHPCSStandards/PHPCSExtra
*/

namespace PHPCSExtra\Universal\Sniffs\OOStructures;
namespace PHPCSExtra\Universal\Sniffs\FunctionDeclarations;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
Expand All @@ -33,17 +33,6 @@ final class RequireFinalMethodsInTraitsSniff implements Sniff
*/
const METRIC_NAME = 'Non-private method in trait is abstract or final ?';

/**
* Whether or not this rule applies to magic methods.
*
* Defaults to `false`.
*
* @since 1.1.0
*
* @var bool
*/
public $includeMagicMethods = false;

/**
* Returns an array of tokens this test wants to listen for.
*
Expand Down Expand Up @@ -81,14 +70,6 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$methodName = FunctionDeclarations::getName($phpcsFile, $stackPtr);
if ($this->includeMagicMethods === false
&& FunctionDeclarations::isMagicMethodName($methodName) === true
) {
// Magic methods are excluded. Bow out.
return;
}

$methodProps = FunctionDeclarations::getProperties($phpcsFile, $stackPtr);
if ($methodProps['scope'] === 'private') {
// Private methods can't be final.
Expand All @@ -109,16 +90,26 @@ public function process(File $phpcsFile, $stackPtr)

$phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'not abstract, not final');

$methodName = FunctionDeclarations::getName($phpcsFile, $stackPtr);
$magic = '';
$code = 'NonFinalMethodFound';
if (FunctionDeclarations::isMagicMethodName($methodName) === true) {
// Use separate error code for magic methods.
$magic = 'magic ';
$code = 'NonFinalMagicMethodFound';
}

$data = [
$methodProps['scope'],
$magic,
$methodName,
ObjectDeclarations::getName($phpcsFile, $scopePtr),
];

$fix = $phpcsFile->addFixableError(
'The non-abstract, %s method "%s()" in trait %s should be declared as final.',
'The non-abstract, %s %smethod "%s()" in trait %s should be declared as final.',
$stackPtr,
'NonFinalMethodFound',
$code,
$data
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,6 @@ trait FinalMagicMethodsAreNotFlagged {
final public function __unserialize($data) {}
}

trait MagicMethodsAreNotFlaggedByDefault {
public function __construct() {}
public function __destruct() {}
public function __clone() {}
public function __debugInfo() {}
public function __invoke() {}
public function __get($name) {}
public function __set($name, $value) {}
public function __isset($name) {}
public function __unset($name) {}
public function __call($name, $arguments) {}
public static function __callStatic($name, $arguments) {}
public function __sleep() {}
public function __toString() {}
public static function __set_state($properties) {}
public function __serialize() {}
public function __unserialize($data) {}
}


/*
* Bad.
Expand All @@ -96,8 +77,7 @@ trait FixMe {
/*comment*/ protected function withCommentBeforeKeyword() {}
}

// phpcs:set Universal.OOStructures.RequireFinalMethodsInTraits includeMagicMethods true
trait MagicMethodsAreFlaggedOnRequest {
trait MagicMethodsAreAlsoFlagged {
public function __construct() {}
public function __destruct() {}
public function __clone() {}
Expand All @@ -115,6 +95,3 @@ trait MagicMethodsAreFlaggedOnRequest {
public function __serialize() {}
public function __unserialize($data) {}
}

// Reset property to default value.
// phpcs:set Universal.OOStructures.RequireFinalMethodsInTraits includeMagicMethods false
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,6 @@ trait FinalMagicMethodsAreNotFlagged {
final public function __unserialize($data) {}
}

trait MagicMethodsAreNotFlaggedByDefault {
public function __construct() {}
public function __destruct() {}
public function __clone() {}
public function __debugInfo() {}
public function __invoke() {}
public function __get($name) {}
public function __set($name, $value) {}
public function __isset($name) {}
public function __unset($name) {}
public function __call($name, $arguments) {}
public static function __callStatic($name, $arguments) {}
public function __sleep() {}
public function __toString() {}
public static function __set_state($properties) {}
public function __serialize() {}
public function __unserialize($data) {}
}


/*
* Bad.
Expand All @@ -96,8 +77,7 @@ trait FixMe {
/*comment*/ protected final function withCommentBeforeKeyword() {}
}

// phpcs:set Universal.OOStructures.RequireFinalMethodsInTraits includeMagicMethods true
trait MagicMethodsAreFlaggedOnRequest {
trait MagicMethodsAreAlsoFlagged {
public final function __construct() {}
public final function __destruct() {}
public final function __clone() {}
Expand All @@ -115,6 +95,3 @@ trait MagicMethodsAreFlaggedOnRequest {
public final function __serialize() {}
public final function __unserialize($data) {}
}

// Reset property to default value.
// phpcs:set Universal.OOStructures.RequireFinalMethodsInTraits includeMagicMethods false
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
* @link https://github.com/PHPCSStandards/PHPCSExtra
*/

namespace PHPCSExtra\Universal\Tests\OOStructures;
namespace PHPCSExtra\Universal\Tests\FunctionDeclarations;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the RequireFinalMethodsInTraits sniff.
*
* @covers PHPCSExtra\Universal\Sniffs\OOStructures\RequireFinalMethodsInTraitsSniff
* @covers PHPCSExtra\Universal\Sniffs\FunctionDeclarations\RequireFinalMethodsInTraitsSniff
*
* @since 1.1.0
*/
Expand All @@ -30,30 +30,30 @@ final class RequireFinalMethodsInTraitsUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
81 => 1,
82 => 1,
84 => 1,
85 => 1,
87 => 1,
92 => 1,
94 => 1,
96 => 1,
101 => 1,
102 => 1,
103 => 1,
104 => 1,
105 => 1,
106 => 1,
107 => 1,
108 => 1,
109 => 1,
110 => 1,
111 => 1,
112 => 1,
113 => 1,
114 => 1,
115 => 1,
116 => 1,
62 => 1,
63 => 1,
65 => 1,
66 => 1,
68 => 1,
73 => 1,
75 => 1,
77 => 1,
81 => 1,
82 => 1,
83 => 1,
84 => 1,
85 => 1,
86 => 1,
87 => 1,
88 => 1,
89 => 1,
90 => 1,
91 => 1,
92 => 1,
93 => 1,
94 => 1,
95 => 1,
96 => 1,
];
}

Expand Down

0 comments on commit 7b19d08

Please sign in to comment.