Skip to content

Commit

Permalink
Generic/One*PerFile sniffs: efficiency tweak
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Dec 12, 2021
1 parent e40edd8 commit e2bbbce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/Standards/Generic/Sniffs/Files/OneClassPerFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
$tokens = $phpcsFile->getTokens();
$start = ($stackPtr + 1);
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
}

$nextClass = $phpcsFile->findNext($this->register(), $start);
if ($nextClass !== false) {
$error = 'Only one class is allowed in a file';
$phpcsFile->addError($error, $nextClass, 'MultipleFound');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$nextInterface = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
$tokens = $phpcsFile->getTokens();
$start = ($stackPtr + 1);
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
}

$nextInterface = $phpcsFile->findNext($this->register(), $start);
if ($nextInterface !== false) {
$error = 'Only one interface is allowed in a file';
$phpcsFile->addError($error, $nextInterface, 'MultipleFound');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
$tokens = $phpcsFile->getTokens();
$start = ($stackPtr + 1);
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
}

$nextClass = $phpcsFile->findNext($this->register(), $start);
if ($nextClass !== false) {
$error = 'Only one object structure is allowed in a file';
$phpcsFile->addError($error, $nextClass, 'MultipleFound');
Expand Down
8 changes: 7 additions & 1 deletion src/Standards/Generic/Sniffs/Files/OneTraitPerFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
$tokens = $phpcsFile->getTokens();
$start = ($stackPtr + 1);
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
$start = ($tokens[$stackPtr]['scope_closer'] + 1);
}

$nextClass = $phpcsFile->findNext($this->register(), $start);
if ($nextClass !== false) {
$error = 'Only one trait is allowed in a file';
$phpcsFile->addError($error, $nextClass, 'MultipleFound');
Expand Down

0 comments on commit e2bbbce

Please sign in to comment.