*/ public function register() { return [\T_CATCH]; } /** * 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::shouldRunOnOrBelow('7.4') === false) { return; } $tokens = $phpcsFile->getTokens(); $token = $tokens[$stackPtr]; // Bow out during live coding. if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) { return; } $lastNonEmptyToken = $phpcsFile->findPrevious( Tokens::$emptyTokens, ($token['parenthesis_closer'] - 1), $token['parenthesis_opener'], true ); if ($tokens[$lastNonEmptyToken]['code'] === \T_VARIABLE) { return; } $phpcsFile->addError( 'Catching exceptions without capturing them to a variable is not supported in PHP 7.4 or earlier.', $stackPtr, 'Found' ); } }