*/ public function register() { return [\T_STRING]; } /** * Processes this test, when one of its tokens is encountered. * * @since 7.1.4 * * @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(); if (\strtolower($tokens[$stackPtr]['content']) !== 'class') { return; } if (Context::inAttribute($phpcsFile, $stackPtr) === true) { // If the syntax is used within an attribute, it will be interpreted as a comment on PHP < 8.0, so not an issue. return; } $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); if ($nextToken !== false && $tokens[$nextToken]['code'] === \T_OPEN_PARENTHESIS) { // Function call or declaration for a function called "class". return; } $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); if ($tokens[$prevToken]['code'] !== \T_DOUBLE_COLON) { return; } $subjectPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true); if ($subjectPtr === false) { // Shouldn't be possible. return; // @codeCoverageIgnore } $preSubjectPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($subjectPtr - 1), null, true); if (isset(Collections::ooHierarchyKeywords()[$tokens[$subjectPtr]['code']]) === true || ($tokens[$subjectPtr]['code'] === \T_STRING && isset(Collections::objectOperators()[$tokens[$preSubjectPtr]['code']]) === false) ) { // This is a syntax which is supported on PHP 5.5 and higher. if (ScannedCode::shouldRunOnOrBelow('5.4') === true) { $phpcsFile->addError( 'The magic class constant ClassName::class was not available in PHP 5.4 or earlier', $stackPtr, 'Found' ); } return; } /* * This syntax was not supported until PHP 8.0. * * Includes throwing an error for syntaxes which are still not supported, as differentiating * between them would be hard, if not impossible, and cause more overhead than it's worth. */ $phpcsFile->addError( 'Using the magic class constant ::class with dynamic class names is not supported in PHP 7.4 or earlier', $stackPtr, 'UsedOnObject' ); } }