Skip to content

Commit

Permalink
Variable sniffs: minor performance tweak
Browse files Browse the repository at this point in the history
The `AbstractVariableSniff` by default listens to all `T_VARIABLE`, `T_DOUBLE_QUOTED_STRING` and `T_HEREDOC` tokens.

The majority of the sniffs extending the `AbstractVariableSniff`, however, only handle `T_VARIABLE` tokens and in particular, only handle `T_VARIABLE` tokens when in an OO scope and nothing more.

This small tweak means these sniffs will no longer "listen" to `T_DOUBLE_QUOTED_STRING` and `T_HEREDOC` tokens, which should make them marginally faster, in particular for code bases containing lots of `T_DOUBLE_QUOTED_STRING` and `T_HEREDOC` tokens.

It also means that these sniff will no longer be triggered for `T_VARIABLE` tokens outside of an OO context.
  • Loading branch information
jrfnl committed Mar 1, 2024
1 parent 36d019c commit 8e5fb89
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;

class ValidVariableNameSniff extends AbstractVariableSniff
{


/**
* Only listen to variables within OO scopes.
*/
public function __construct()
{
$scopes = Tokens::$ooScopeTokens;
$listen = [T_VARIABLE];

AbstractScopeSniff::__construct($scopes, $listen, false);

}//end __construct()


/**
* Processes class member variables.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@

use Exception;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;

class PropertyDeclarationSniff extends AbstractVariableSniff
{


/**
* Only listen to variables within OO scopes.
*/
public function __construct()
{
$scopes = Tokens::$ooScopeTokens;
$listen = [T_VARIABLE];

AbstractScopeSniff::__construct($scopes, $listen, false);

}//end __construct()


/**
* Processes the function tokens within the class.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;

class VariableCommentSniff extends AbstractVariableSniff
{


/**
* Only listen to variables within OO scopes.
*/
public function __construct()
{
$scopes = Tokens::$ooScopeTokens;
$listen = [T_VARIABLE];

AbstractScopeSniff::__construct($scopes, $listen, false);

}//end __construct()


/**
* Called to process class member vars.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;

class MemberVarScopeSniff extends AbstractVariableSniff
{


/**
* Only listen to variables within OO scopes.
*/
public function __construct()
{
$scopes = Tokens::$ooScopeTokens;
$listen = [T_VARIABLE];

AbstractScopeSniff::__construct($scopes, $listen, false);

}//end __construct()


/**
* Processes the function tokens within the class.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;

Expand All @@ -31,6 +32,19 @@ class MemberVarSpacingSniff extends AbstractVariableSniff
public $spacingBeforeFirst = 1;


/**
* Only listen to variables within OO scopes.
*/
public function __construct()
{
$scopes = Tokens::$ooScopeTokens;
$listen = [T_VARIABLE];

AbstractScopeSniff::__construct($scopes, $listen, false);

}//end __construct()


/**
* Processes the function tokens within the class.
*
Expand Down

0 comments on commit 8e5fb89

Please sign in to comment.