diff --git a/src/Sniffs/AbstractVariableSniff.php b/src/Sniffs/AbstractVariableSniff.php index 33594fd012..48e31b2791 100644 --- a/src/Sniffs/AbstractVariableSniff.php +++ b/src/Sniffs/AbstractVariableSniff.php @@ -22,6 +22,29 @@ abstract class AbstractVariableSniff extends AbstractScopeSniff { + /** + * List of PHP Reserved variables. + * + * Used by various naming convention sniffs. + * + * @var array + */ + protected $phpReservedVars = [ + '_SERVER' => true, + '_GET' => true, + '_POST' => true, + '_REQUEST' => true, + '_SESSION' => true, + '_ENV' => true, + '_COOKIE' => true, + '_FILES' => true, + 'GLOBALS' => true, + 'http_response_header' => true, + 'HTTP_RAW_POST_DATA' => true, + 'php_errormsg' => true, + ]; + + /** * Constructs an AbstractVariableTest. */ diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php index 42cc8cd1af..fcb59a4c2f 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -31,23 +31,8 @@ protected function processVariable(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $varName = ltrim($tokens[$stackPtr]['content'], '$'); - $phpReservedVars = [ - '_SERVER', - '_GET', - '_POST', - '_REQUEST', - '_SESSION', - '_ENV', - '_COOKIE', - '_FILES', - 'GLOBALS', - 'http_response_header', - 'HTTP_RAW_POST_DATA', - 'php_errormsg', - ]; - // If it's a php reserved var, then its ok. - if (in_array($varName, $phpReservedVars) === true) { + if (isset($this->phpReservedVars[$varName]) === true) { return; } @@ -171,25 +156,10 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $phpReservedVars = [ - '_SERVER', - '_GET', - '_POST', - '_REQUEST', - '_SESSION', - '_ENV', - '_COOKIE', - '_FILES', - 'GLOBALS', - 'http_response_header', - 'HTTP_RAW_POST_DATA', - 'php_errormsg', - ]; - if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) { foreach ($matches[1] as $varName) { // If it's a php reserved var, then its ok. - if (in_array($varName, $phpReservedVars) === true) { + if (isset($this->phpReservedVars[$varName]) === true) { continue; } diff --git a/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php index 3a89901150..360e350531 100644 --- a/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -31,23 +31,8 @@ protected function processVariable(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $varName = ltrim($tokens[$stackPtr]['content'], '$'); - $phpReservedVars = [ - '_SERVER' => true, - '_GET' => true, - '_POST' => true, - '_REQUEST' => true, - '_SESSION' => true, - '_ENV' => true, - '_COOKIE' => true, - '_FILES' => true, - 'GLOBALS' => true, - 'http_response_header' => true, - 'HTTP_RAW_POST_DATA' => true, - 'php_errormsg' => true, - ]; - // If it's a php reserved var, then its ok. - if (isset($phpReservedVars[$varName]) === true) { + if (isset($this->phpReservedVars[$varName]) === true) { return; } @@ -183,25 +168,10 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $phpReservedVars = [ - '_SERVER', - '_GET', - '_POST', - '_REQUEST', - '_SESSION', - '_ENV', - '_COOKIE', - '_FILES', - 'GLOBALS', - 'http_response_header', - 'HTTP_RAW_POST_DATA', - 'php_errormsg', - ]; - if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) { foreach ($matches[1] as $varName) { // If it's a php reserved var, then its ok. - if (in_array($varName, $phpReservedVars) === true) { + if (isset($this->phpReservedVars[$varName]) === true) { continue; }