Due to how common the usage of `final private function __construct` is and given that * > the same results cannot be achieved with a protected visibility, an exception to this rule * > is made for constructors. * * PHP version 8.0 * * @link https://wiki.php.net/rfc/inheritance_private_methods * * @since 10.0.0 */ class ForbiddenFinalPrivateMethodsSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. * * @since 10.0.0 * * @return array */ public function register() { return [\T_FUNCTION]; } /** * Processes this test, when one of its tokens is encountered. * * @since 10.0.0 * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function process(File $phpcsFile, $stackPtr) { if (ScannedCode::shouldRunOnOrAbove('8.0') === false) { return; } if (Scopes::isOOMethod($phpcsFile, $stackPtr) === false) { // Function, not method. return; } $name = FunctionDeclarations::getName($phpcsFile, $stackPtr); if (empty($name) === true) { // Parse error or live coding. return; } if (\strtolower($name) === '__construct') { // The rule does not apply to constructors. Bow out. return; } $properties = FunctionDeclarations::getProperties($phpcsFile, $stackPtr); if ($properties['scope'] !== 'private' || $properties['is_final'] === false) { // Not an private final method. return; } $phpcsFile->addWarning( 'Private methods should not be declared as final since PHP 8.0', $stackPtr, 'Found' ); } }