Skip to content

Commit e2bbbce

Browse files
committed
Generic/One*PerFile sniffs: efficiency tweak
Each of these sniffs would start walking the rest of the file from the token next to the OO keyword to the end of the file in search of the next (class/interface/trait) keyword. As nested OO structure declarations (with the exception of anonymous classes, but those are outside the realm of these sniffs) are not supported in PHP, we can make these sniffs a lot faster by starting the search _after_ the scope closer for the current OO structure.
1 parent e40edd8 commit e2bbbce

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

src/Standards/Generic/Sniffs/Files/OneClassPerFileSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public function register()
3939
*/
4040
public function process(File $phpcsFile, $stackPtr)
4141
{
42-
$nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
42+
$tokens = $phpcsFile->getTokens();
43+
$start = ($stackPtr + 1);
44+
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
45+
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
46+
}
47+
48+
$nextClass = $phpcsFile->findNext($this->register(), $start);
4349
if ($nextClass !== false) {
4450
$error = 'Only one class is allowed in a file';
4551
$phpcsFile->addError($error, $nextClass, 'MultipleFound');

src/Standards/Generic/Sniffs/Files/OneInterfacePerFileSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public function register()
3939
*/
4040
public function process(File $phpcsFile, $stackPtr)
4141
{
42-
$nextInterface = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
42+
$tokens = $phpcsFile->getTokens();
43+
$start = ($stackPtr + 1);
44+
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
45+
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
46+
}
47+
48+
$nextInterface = $phpcsFile->findNext($this->register(), $start);
4349
if ($nextInterface !== false) {
4450
$error = 'Only one interface is allowed in a file';
4551
$phpcsFile->addError($error, $nextInterface, 'MultipleFound');

src/Standards/Generic/Sniffs/Files/OneObjectStructurePerFileSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ public function register()
4343
*/
4444
public function process(File $phpcsFile, $stackPtr)
4545
{
46-
$nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
46+
$tokens = $phpcsFile->getTokens();
47+
$start = ($stackPtr + 1);
48+
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
49+
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
50+
}
51+
52+
$nextClass = $phpcsFile->findNext($this->register(), $start);
4753
if ($nextClass !== false) {
4854
$error = 'Only one object structure is allowed in a file';
4955
$phpcsFile->addError($error, $nextClass, 'MultipleFound');

src/Standards/Generic/Sniffs/Files/OneTraitPerFileSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public function register()
3939
*/
4040
public function process(File $phpcsFile, $stackPtr)
4141
{
42-
$nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
42+
$tokens = $phpcsFile->getTokens();
43+
$start = ($stackPtr + 1);
44+
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
45+
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
46+
}
47+
48+
$nextClass = $phpcsFile->findNext($this->register(), $start);
4349
if ($nextClass !== false) {
4450
$error = 'Only one trait is allowed in a file';
4551
$phpcsFile->addError($error, $nextClass, 'MultipleFound');

0 commit comments

Comments
 (0)