Skip to content

Commit

Permalink
Merge pull request #354 from jrfnl/feature/merge-long-arrays-removed-…
Browse files Browse the repository at this point in the history
…global-vars

Merge RemovedGlobalVariables sniff with the LongArraysSniff
  • Loading branch information
wimg committed Feb 26, 2017
2 parents 09aa6ee + eb73f2a commit 9c62e59
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 294 deletions.
114 changes: 0 additions & 114 deletions Sniffs/PHP/LongArraysSniff.php

This file was deleted.

71 changes: 70 additions & 1 deletion Sniffs/PHP/RemovedGlobalVariablesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,46 @@ class PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff extends PHPCompati
* @var array(string|null)
*/
protected $removedGlobalVariables = array(
'HTTP_POST_VARS' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_POST',
),
'HTTP_GET_VARS' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_GET',
),
'HTTP_ENV_VARS' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_ENV',
),
'HTTP_SERVER_VARS' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_SERVER',
),
'HTTP_COOKIE_VARS' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_COOKIE',
),
'HTTP_SESSION_VARS' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_SESSION',
),
'HTTP_POST_FILES' => array(
'5.3' => false,
'5.4' => true,
'alternative' => '$_FILES',
),

'HTTP_RAW_POST_DATA' => array(
'5.6' => false,
'7.0' => true,
'alternative' => 'php://input'
'alternative' => 'php://input',
),
);

Expand All @@ -57,13 +93,46 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if ($this->supportsAbove('5.3') === false) {
return;
}

$tokens = $phpcsFile->getTokens();
$varName = substr($tokens[$stackPtr]['content'], 1);

if (isset($this->removedGlobalVariables[$varName]) === false) {
return;
}

if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) {
/*
* Check for class property definitions.
*/
$properties = array();
try {
$properties = $phpcsFile->getMemberProperties($stackPtr);
} catch ( PHP_CodeSniffer_Exception $e) {
// If it's not an expected exception, throw it.
if ($e->getMessage() !== '$stackPtr is not a class member var') {
throw $e;
}
}

if (isset($properties['scope'])) {
// Ok, so this was a class property declaration, not our concern.
return;
}

/*
* Check for static usage of class properties shadowing the removed global variables.
*/
$prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
if ($prevToken !== false && $tokens[$prevToken]['code'] === T_DOUBLE_COLON) {
return;
}
}

// Still here, so throw an error/warning.
$itemInfo = array(
'name' => $varName,
);
Expand Down
121 changes: 0 additions & 121 deletions Tests/Sniffs/PHP/LongArraysSniffTest.php

This file was deleted.

0 comments on commit 9c62e59

Please sign in to comment.