Skip to content

Commit 8e5fb89

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 36d019c commit 8e5fb89

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@
1010
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1314
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
15+
use PHP_CodeSniffer\Util\Tokens;
1416

1517
class ValidVariableNameSniff extends AbstractVariableSniff
1618
{
1719

1820

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

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

+14
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@
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+
$scopes = Tokens::$ooScopeTokens;
28+
$listen = [T_VARIABLE];
29+
30+
AbstractScopeSniff::__construct($scopes, $listen, false);
31+
32+
}//end __construct()
33+
34+
2135
/**
2236
* Processes the function tokens within the class.
2337
*

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

+15
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@
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+
$scopes = Tokens::$ooScopeTokens;
28+
$listen = [T_VARIABLE];
29+
30+
AbstractScopeSniff::__construct($scopes, $listen, false);
31+
32+
}//end __construct()
33+
34+
2035
/**
2136
* Called to process class member vars.
2237
*

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

+15
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@
1010
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
1314
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
15+
use PHP_CodeSniffer\Util\Tokens;
1416

1517
class MemberVarScopeSniff extends AbstractVariableSniff
1618
{
1719

1820

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

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

+14
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,19 @@ 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+
$scopes = Tokens::$ooScopeTokens;
41+
$listen = [T_VARIABLE];
42+
43+
AbstractScopeSniff::__construct($scopes, $listen, false);
44+
45+
}//end __construct()
46+
47+
3448
/**
3549
* Processes the function tokens within the class.
3650
*

0 commit comments

Comments
 (0)