From 580a85047b5c2baec674b59801bc277bf6481f4c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 1 Mar 2024 15:28:27 +0100 Subject: [PATCH] Variable sniffs: minor performance tweak 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. --- .../NamingConventions/ValidVariableNameSniff.php | 15 +++++++++++++++ .../Sniffs/Classes/PropertyDeclarationSniff.php | 14 ++++++++++++++ .../Sniffs/Commenting/VariableCommentSniff.php | 15 +++++++++++++++ .../Squiz/Sniffs/Scope/MemberVarScopeSniff.php | 15 +++++++++++++++ .../Sniffs/WhiteSpace/MemberVarSpacingSniff.php | 14 ++++++++++++++ 5 files changed, 73 insertions(+) diff --git a/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php index 48674b1e80..8bc9351285 100644 --- a/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -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. * diff --git a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php index 29d7023ea9..04c5535114 100644 --- a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php @@ -11,6 +11,7 @@ use Exception; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; use PHP_CodeSniffer\Util\Tokens; @@ -18,6 +19,19 @@ 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. * diff --git a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php index 76fbc6dca6..050ed88e87 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php @@ -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. * diff --git a/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php b/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php index 3d1c83f019..b15116701b 100644 --- a/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php +++ b/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php @@ -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. * diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php index 773d25d4f5..8840ee0cc0 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php @@ -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; @@ -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. *