diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cebe514f7..b1eac49d80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,6 +101,8 @@ The file documents changes to the PHP_CodeSniffer project. - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - The following sniffs have received performance related improvements: - Generic.PHP.LowerCaseType + - PSR12.Files.OpenTag + - Thanks to Juliette Reinders Folmer (@jrfnl) for the patches - The -e (explain) command will now list sniffs in natural order - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Tests using the PHPCS native test framework with multiple test case files will now run the test case files in numeric order. diff --git a/src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php b/src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php index bb8a75d0b5..6c80d812fa 100644 --- a/src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php @@ -44,12 +44,6 @@ public function process(File $phpcsFile, $stackPtr) return $phpcsFile->numTokens; } - $next = $phpcsFile->findNext(T_INLINE_HTML, 0); - if ($next !== false) { - // This rule only applies to PHP-only files. - return $phpcsFile->numTokens; - } - $tokens = $phpcsFile->getTokens(); $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); if ($next === false) { @@ -57,12 +51,21 @@ public function process(File $phpcsFile, $stackPtr) return $phpcsFile->numTokens; } - if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { - $error = 'Opening PHP tag must be on a line by itself'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAlone'); - if ($fix === true) { - $phpcsFile->fixer->addNewline($stackPtr); - } + if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { + // Tag is on a line by itself. + return $phpcsFile->numTokens; + } + + $next = $phpcsFile->findNext(T_INLINE_HTML, 0); + if ($next !== false) { + // This rule only applies to PHP-only files. + return $phpcsFile->numTokens; + } + + $error = 'Opening PHP tag must be on a line by itself'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAlone'); + if ($fix === true) { + $phpcsFile->fixer->addNewline($stackPtr); } return $phpcsFile->numTokens;