Skip to content

Commit f237945

Browse files
authored
Merge pull request #64 from PHPCSStandards/U-3840/feature/psr12-opentag-improve-performance
PSR12/OpenTag: improve performance
2 parents b2464f7 + 9020951 commit f237945

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ The file documents changes to the PHP_CodeSniffer project.
101101
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
102102
- The following sniffs have received performance related improvements:
103103
- Generic.PHP.LowerCaseType
104+
- PSR12.Files.OpenTag
105+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patches
104106
- The -e (explain) command will now list sniffs in natural order
105107
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
106108
- Tests using the PHPCS native test framework with multiple test case files will now run the test case files in numeric order.

src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,28 @@ public function process(File $phpcsFile, $stackPtr)
4444
return $phpcsFile->numTokens;
4545
}
4646

47-
$next = $phpcsFile->findNext(T_INLINE_HTML, 0);
48-
if ($next !== false) {
49-
// This rule only applies to PHP-only files.
50-
return $phpcsFile->numTokens;
51-
}
52-
5347
$tokens = $phpcsFile->getTokens();
5448
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
5549
if ($next === false) {
5650
// Empty file.
5751
return $phpcsFile->numTokens;
5852
}
5953

60-
if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
61-
$error = 'Opening PHP tag must be on a line by itself';
62-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAlone');
63-
if ($fix === true) {
64-
$phpcsFile->fixer->addNewline($stackPtr);
65-
}
54+
if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
55+
// Tag is on a line by itself.
56+
return $phpcsFile->numTokens;
57+
}
58+
59+
$next = $phpcsFile->findNext(T_INLINE_HTML, 0);
60+
if ($next !== false) {
61+
// This rule only applies to PHP-only files.
62+
return $phpcsFile->numTokens;
63+
}
64+
65+
$error = 'Opening PHP tag must be on a line by itself';
66+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAlone');
67+
if ($fix === true) {
68+
$phpcsFile->fixer->addNewline($stackPtr);
6669
}
6770

6871
return $phpcsFile->numTokens;

0 commit comments

Comments
 (0)