Skip to content

Commit

Permalink
Merge branch 'feature/variablenames-efficiency-fix' of https://github…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Mar 16, 2018
2 parents 59c0113 + 52cbc13 commit 78ddbae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 64 deletions.
23 changes: 23 additions & 0 deletions src/Sniffs/AbstractVariableSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 78ddbae

Please sign in to comment.