Skip to content

Commit c403015

Browse files
committed
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.
1 parent b63cedf commit c403015

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@
1111

1212
use PHP_CodeSniffer\Exceptions\RuntimeException;
1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1415
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
16+
use PHP_CodeSniffer\Util\Tokens;
1517

1618
class ValidVariableNameSniff extends AbstractVariableSniff
1719
{
1820

1921

22+
/**
23+
* Only listen to variables within OO scopes.
24+
*/
25+
public function __construct()
26+
{
27+
AbstractScopeSniff::__construct(Tokens::$ooScopeTokens, [T_VARIABLE], false);
28+
29+
}//end __construct()
30+
31+
2032
/**
2133
* Processes class member variables.
2234
*

src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@
1111

1212
use Exception;
1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1415
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
1516
use PHP_CodeSniffer\Util\Tokens;
1617

1718
class PropertyDeclarationSniff extends AbstractVariableSniff
1819
{
1920

2021

22+
/**
23+
* Only listen to variables within OO scopes.
24+
*/
25+
public function __construct()
26+
{
27+
AbstractScopeSniff::__construct(Tokens::$ooScopeTokens, [T_VARIABLE], false);
28+
29+
}//end __construct()
30+
31+
2132
/**
2233
* Processes the function tokens within the class.
2334
*

src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@
1010
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1314
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
1415
use PHP_CodeSniffer\Util\Common;
16+
use PHP_CodeSniffer\Util\Tokens;
1517

1618
class VariableCommentSniff extends AbstractVariableSniff
1719
{
1820

1921

22+
/**
23+
* Only listen to variables within OO scopes.
24+
*/
25+
public function __construct()
26+
{
27+
AbstractScopeSniff::__construct(Tokens::$ooScopeTokens, [T_VARIABLE], false);
28+
29+
}//end __construct()
30+
31+
2032
/**
2133
* Called to process class member vars.
2234
*

src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@
1111

1212
use PHP_CodeSniffer\Exceptions\RuntimeException;
1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1415
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
16+
use PHP_CodeSniffer\Util\Tokens;
1517

1618
class MemberVarScopeSniff extends AbstractVariableSniff
1719
{
1820

1921

22+
/**
23+
* Only listen to variables within OO scopes.
24+
*/
25+
public function __construct()
26+
{
27+
AbstractScopeSniff::__construct(Tokens::$ooScopeTokens, [T_VARIABLE], false);
28+
29+
}//end __construct()
30+
31+
2032
/**
2133
* Processes the function tokens within the class.
2234
*

src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1314
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
1415
use PHP_CodeSniffer\Util\Tokens;
1516

@@ -31,6 +32,16 @@ class MemberVarSpacingSniff extends AbstractVariableSniff
3132
public $spacingBeforeFirst = 1;
3233

3334

35+
/**
36+
* Only listen to variables within OO scopes.
37+
*/
38+
public function __construct()
39+
{
40+
AbstractScopeSniff::__construct(Tokens::$ooScopeTokens, [T_VARIABLE], false);
41+
42+
}//end __construct()
43+
44+
3445
/**
3546
* Processes the function tokens within the class.
3647
*

0 commit comments

Comments
 (0)