Skip to content

PSR12/OpenTag: improve performance #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 15 additions & 12 deletions src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,28 @@ 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) {
// Empty file.
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;
Expand Down