Skip to content

Commit df8e16f

Browse files
committed
PSR12/OpenTag: improve performance
No functional changes at all, just improving performance of the sniff by changing the order of certain checks. Some benchmarks for this change run using the Performance report (PR 3810): Command: `phpcs -ps . --extensions=php --ignore=/vendor/ --report=performance --standard=psr12` Output for the `PSR12.Files.OpenTag` sniff: Result | PHPCS itself | Set of Projects A | Set of Projects B | Set of Projects C | ------ | ------------------ | ------------------ | ------------------ | ----------------- | Nr of Files Scanned | 614 | 4115 | 25546 | 2250 | Before | 0.077045 ( 2.3 %) | 0.982014 ( 1.9 %) | 3.267655 ( 2.1 %) | 0.179564 ( 2.1 %) After | 0.000928 ( 0.0 %) | 0.013066 ( 0.0 %) | 0.091816 ( 0.1 %) | 0.109021 ( 1.3 %) With what the sniff does, the impact is biggest for files/codebases which already comply with the expectations of this sniff.
1 parent 4ac5785 commit df8e16f

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

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

Lines changed: 16 additions & 13 deletions
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.
57-
return;
51+
return $phpcsFile->numTokens;
52+
}
53+
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;
5863
}
5964

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-
}
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)