*/ public function register() { return Tokens::$ooScopeTokens; } /** * Processes this test, when one of its tokens is encountered. * * @since 9.2.0 * @since 10.0.0 New error message for abstract private methods in traits. * * @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('5.1') === false) { return; } $ooMethods = ObjectDeclarations::getDeclaredMethods($phpcsFile, $stackPtr); if (empty($ooMethods)) { // No methods declared in the OO construct at all. Bow out. return; } $tokens = $phpcsFile->getTokens(); $shouldRunOnOrBelow74 = ScannedCode::shouldRunOnOrBelow('7.4'); foreach ($ooMethods as $name => $functionPtr) { $properties = FunctionDeclarations::getProperties($phpcsFile, $functionPtr); if ($properties['scope'] !== 'private' || $properties['is_abstract'] !== true) { // Not an abstract private method. continue; } if ($tokens[$stackPtr]['code'] === \T_TRAIT) { if ($shouldRunOnOrBelow74 === true) { $phpcsFile->addError( 'Traits cannot declare "abstract private" methods in PHP 7.4 or below', $functionPtr, 'InTrait' ); } continue; } // Not a trait. $phpcsFile->addError( 'Abstract methods cannot be declared as private since PHP 5.1', $functionPtr, 'Found' ); } } }