Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More ini directives, functions, and forbidden parameter names. #2

Merged
merged 5 commits into from
Mar 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@
class PHP54Compatibility_Sniffs_PHP_DeprecatedFunctionsSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff class PHP54Compatibility_Sniffs_PHP_DeprecatedFunctionsSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff
{ {
/** /**
* A list of forbidden functions with their alternatives. * A list of deprecated functions with their alternatives.
* *
* The value is NULL if no alternative exists. IE, the * The value is NULL if no alternative exists. IE, the
* function should just not be used. * function should just not be used.
* *
* @var array(string => string|null) * @var array(string => string|null)
*/ */
protected $forbiddenFunctions = array( protected $forbiddenFunctions = array(
'get_magic_quotes_gpc' => null,
'get_magic_quotes_runtime' => null,
'set_magic_quotes_runtime' => null,
'import_request_variables' => null, 'import_request_variables' => null,
'session_is_registered' => null,
'session_register' => null,
'session_unregister' => null,
); );


/** /**
* If true, an error will be thrown; otherwise a warning. * If true, an error will be thrown; otherwise a warning.
* *
* @var bool * @var bool
*/ */
public $error = true; public $error = false;


/** /**
* Generates the error or wanrning for this sniff. * Generates the error or wanrning for this sniff.
Expand All @@ -65,13 +65,8 @@ protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
{ {
$data = array($function); $data = array($function);
$error = 'The use of function %s() is '; $error = 'The use of function %s() is ';
if ($this->error === true) { $type = 'Deprecated';
$type = 'Found'; $error .= 'deprecated';
$error .= 'forbidden';
} else {
$type = 'Discouraged';
$error .= 'discouraged';
}


if ($pattern === null) { if ($pattern === null) {
$pattern = $function; $pattern = $function;
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,7 +43,23 @@ class PHP54Compatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff implements PHP_
'highlight.bg', 'highlight.bg',
'register_globals', 'register_globals',
'register_long_arrays', 'register_long_arrays',
'allow_call_time_pass_reference' 'allow_call_time_pass_reference',
);

/**
* A list of removed INI directives
*
* @var array(string)
*/
protected $removedIniDirectives = array(
'define_syslog_variables',
'register_globals',
'register_long_arrays',
'safe_mode',
'safe_mode_exec_dir',
'magic_quotes_gpc',
'magic_quotes_runtime',
'magic_quotes_sybase',
); );


/** /**
Expand Down Expand Up @@ -87,11 +103,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return; return;
} }
$iniToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr, null); $iniToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr, null);
if (in_array(str_replace("'", "", $tokens[$iniToken]['content']), $this->deprecatedIniDirectives) === false) { $iniDirective = str_replace("'", "", $tokens[$iniToken]['content']);
return; if (in_array($iniDirective, $this->deprecatedIniDirectives) === true) {
$error = "[PHP 5.4] INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
$phpcsFile->addWarning($error, $stackPtr);
} else if (in_array($iniDirective, $this->removedIniDirectives) === true) {
$error = "[PHP 5.4] INI directive " . $tokens[$iniToken]['content'] . " is removed.";
$phpcsFile->addError($error, $stackPtr);
} }
$error = "[PHP 5.4] INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";

$phpcsFile->addWarning($error, $stackPtr);
} }
} }
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* PHP54Compatibility_Sniffs_PHP_ForbiddenParameterNameSniff.
*
* PHP version 5.4
*
* @category PHP
* @package PHP54Compatibility
* @author Sebastian Marek <proofek@gmail.com>
* @copyright 2012 Sebastian Marek
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link https://github.com/proofek/PHP54Compatibility
*/

/**
* PHP54Compatibility_Sniffs_PHP_ForbiddenParameterNamesSniff.
*
* Prohibits the use of particular parameter names.
*
* @category PHP
* @package PHP54Compatibility
* @author LB Denker <lb@elblinkin.info>
* @copyright 2012 LB Denker
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link https://github.com/proofek/PHP54Compatibility
*/
class PHP54Compatibility_Sniffs_PHP_ForbiddenParameterNamesSniff implements PHP_CodeSniffer_Sniff
{

/**
* A list of forbidden parameter names.
*
* @var array(string)
*/
public $forbiddenParameterNames = array(
'$GLOBALS',
'$_SERVER',
'$_GET',
'$_SET',
'$_FILES',
'$_COOKIE',
'$_SESSION',
'$_REQUEST',
'$_ENV',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_FUNCTION);
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
if (in_array($param, $this->forbiddenParameterNames) === true) {
$error = "[PHP 5.4] $param is not a valid parameter name.";
$phpcsFile->addError($error, $stackPtr);
}
}
}
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* PHP54Compatibility_Sniffs_PHP_RemovedFunctionsSniff.
*
* This is based on Wim Godden's PHP53Compatibility code sniffs.
* See [blog](http://techblog.wimgodden.be/tag/codesniffer) and
* [github](https://github.com/wimg/PHP53Compat_CodeSniffer).
*
* PHP version 5.4
*
* @category PHP
* @package PHP54Compatibility
* @author Sebastian Marek <proofek@gmail.com>
* @copyright 2012 Sebastian Marek
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link https://github.com/proofek/PHP54Compatibility
*/

/**
* PHP54Compatibility_Sniffs_PHP_RemovedFunctionsSniff.
*
* @category PHP
* @package PHP54Compatibility
* @author Sebastian Marek <proofek@gmail.com>
* @copyright 2012 Sebastian Marek
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link https://github.com/proofek/PHP54Compatibility
*/
class PHP54Compatibility_Sniffs_PHP_RemovedFunctionsSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff
{
/**
* A list of forbidden functions with their alternatives.
*
* The value is NULL if no alternative exists. IE, the
* function should just not be used.
*
* @var array(string => string|null)
*/
protected $forbiddenFunctions = array(
'define_syslog_vairables' => null,
'import_request_variables' => null,
'session_is_registered' => null,
'session_register' => null,
'session_unregister' => null,
'mysqli_bind_param' => null,
'mysqli_bind_result' => null,
'mysqli_client_encoding' => null,
'mysqli_fetch' => null,
'mysqli_param_count' => null,
'mysqli_get_metadata' => null,
'mysqli_send_long_data' => null,
);

/**
* If true, an error will be thrown; otherwise a warning.
*
* @var bool
*/
public $error = true;

/**
* Generates the error or wanrning for this sniff.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the forbidden function
* in the token array.
* @param string $function The name of the forbidden function.
* @param string $pattern The pattern used for the match.
*
* @return void
*/
protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
{
$data = array($function);
$error = 'The use of function %s() is ';
$type = 'Removed';
$error .= 'removed';

if ($pattern === null) {
$pattern = $function;
}

if ($this->forbiddenFunctions[$pattern] !== null) {
$type .= 'WithAlternative';
$data[] = $this->forbiddenFunctions[$pattern];
$error .= '; use %s() instead';
}

if ($this->error === true) {
$phpcsFile->addError("[PHP 5.4] $error", $stackPtr, $type, $data);
} else {
$phpcsFile->addWarning("[PHP 5.4] $error", $stackPtr, $type, $data);
}

}
}