Skip to content

Commit 2b5301e

Browse files
committed
Merge pull request #2 from elblinkin/master
More ini directives, functions, and forbidden parameter names.
2 parents 8fa19f8 + 37ed819 commit 2b5301e

File tree

4 files changed

+201
-18
lines changed

4 files changed

+201
-18
lines changed

Diff for: src/PHP/CodeSniffer/Standards/PHP54Compatibility/Sniffs/PHP/DeprecatedFunctionsSniff.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@
2929
class PHP54Compatibility_Sniffs_PHP_DeprecatedFunctionsSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff
3030
{
3131
/**
32-
* A list of forbidden functions with their alternatives.
32+
* A list of deprecated functions with their alternatives.
3333
*
3434
* The value is NULL if no alternative exists. IE, the
3535
* function should just not be used.
3636
*
3737
* @var array(string => string|null)
3838
*/
3939
protected $forbiddenFunctions = array(
40+
'get_magic_quotes_gpc' => null,
41+
'get_magic_quotes_runtime' => null,
42+
'set_magic_quotes_runtime' => null,
4043
'import_request_variables' => null,
41-
'session_is_registered' => null,
42-
'session_register' => null,
43-
'session_unregister' => null,
4444
);
4545

4646
/**
4747
* If true, an error will be thrown; otherwise a warning.
4848
*
4949
* @var bool
5050
*/
51-
public $error = true;
51+
public $error = false;
5252

5353
/**
5454
* Generates the error or wanrning for this sniff.
@@ -65,13 +65,8 @@ protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
6565
{
6666
$data = array($function);
6767
$error = 'The use of function %s() is ';
68-
if ($this->error === true) {
69-
$type = 'Found';
70-
$error .= 'forbidden';
71-
} else {
72-
$type = 'Discouraged';
73-
$error .= 'discouraged';
74-
}
68+
$type = 'Deprecated';
69+
$error .= 'deprecated';
7570

7671
if ($pattern === null) {
7772
$pattern = $function;

Diff for: src/PHP/CodeSniffer/Standards/PHP54Compatibility/Sniffs/PHP/DeprecatedIniDirectivesSniff.php

+24-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,23 @@ class PHP54Compatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff implements PHP_
4343
'highlight.bg',
4444
'register_globals',
4545
'register_long_arrays',
46-
'allow_call_time_pass_reference'
46+
'allow_call_time_pass_reference',
47+
);
48+
49+
/**
50+
* A list of removed INI directives
51+
*
52+
* @var array(string)
53+
*/
54+
protected $removedIniDirectives = array(
55+
'define_syslog_variables',
56+
'register_globals',
57+
'register_long_arrays',
58+
'safe_mode',
59+
'safe_mode_exec_dir',
60+
'magic_quotes_gpc',
61+
'magic_quotes_runtime',
62+
'magic_quotes_sybase',
4763
);
4864

4965
/**
@@ -87,11 +103,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
87103
return;
88104
}
89105
$iniToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr, null);
90-
if (in_array(str_replace("'", "", $tokens[$iniToken]['content']), $this->deprecatedIniDirectives) === false) {
91-
return;
106+
$iniDirective = str_replace("'", "", $tokens[$iniToken]['content']);
107+
if (in_array($iniDirective, $this->deprecatedIniDirectives) === true) {
108+
$error = "[PHP 5.4] INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
109+
$phpcsFile->addWarning($error, $stackPtr);
110+
} else if (in_array($iniDirective, $this->removedIniDirectives) === true) {
111+
$error = "[PHP 5.4] INI directive " . $tokens[$iniToken]['content'] . " is removed.";
112+
$phpcsFile->addError($error, $stackPtr);
92113
}
93-
$error = "[PHP 5.4] INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
94-
95-
$phpcsFile->addWarning($error, $stackPtr);
96114
}
97115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* PHP54Compatibility_Sniffs_PHP_ForbiddenParameterNameSniff.
4+
*
5+
* PHP version 5.4
6+
*
7+
* @category PHP
8+
* @package PHP54Compatibility
9+
* @author Sebastian Marek <[email protected]>
10+
* @copyright 2012 Sebastian Marek
11+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
12+
* @link https://github.com/proofek/PHP54Compatibility
13+
*/
14+
15+
/**
16+
* PHP54Compatibility_Sniffs_PHP_ForbiddenParameterNamesSniff.
17+
*
18+
* Prohibits the use of particular parameter names.
19+
*
20+
* @category PHP
21+
* @package PHP54Compatibility
22+
* @author LB Denker <[email protected]>
23+
* @copyright 2012 LB Denker
24+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
25+
* @link https://github.com/proofek/PHP54Compatibility
26+
*/
27+
class PHP54Compatibility_Sniffs_PHP_ForbiddenParameterNamesSniff implements PHP_CodeSniffer_Sniff
28+
{
29+
30+
/**
31+
* A list of forbidden parameter names.
32+
*
33+
* @var array(string)
34+
*/
35+
public $forbiddenParameterNames = array(
36+
'$GLOBALS',
37+
'$_SERVER',
38+
'$_GET',
39+
'$_SET',
40+
'$_FILES',
41+
'$_COOKIE',
42+
'$_SESSION',
43+
'$_REQUEST',
44+
'$_ENV',
45+
);
46+
/**
47+
* Returns an array of tokens this test wants to listen for.
48+
*
49+
* @return array
50+
*/
51+
public function register()
52+
{
53+
return array(T_FUNCTION);
54+
}
55+
56+
/**
57+
* Processes this test, when one of its tokens is encountered.
58+
*
59+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
60+
* @param int $stackPtr The position of the current token in the
61+
* stack passed in $tokens.
62+
*
63+
* @return void
64+
*/
65+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
66+
{
67+
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
68+
if (in_array($param, $this->forbiddenParameterNames) === true) {
69+
$error = "[PHP 5.4] $param is not a valid parameter name.";
70+
$phpcsFile->addError($error, $stackPtr);
71+
}
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* PHP54Compatibility_Sniffs_PHP_RemovedFunctionsSniff.
4+
*
5+
* This is based on Wim Godden's PHP53Compatibility code sniffs.
6+
* See [blog](http://techblog.wimgodden.be/tag/codesniffer) and
7+
* [github](https://github.com/wimg/PHP53Compat_CodeSniffer).
8+
*
9+
* PHP version 5.4
10+
*
11+
* @category PHP
12+
* @package PHP54Compatibility
13+
* @author Sebastian Marek <[email protected]>
14+
* @copyright 2012 Sebastian Marek
15+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
16+
* @link https://github.com/proofek/PHP54Compatibility
17+
*/
18+
19+
/**
20+
* PHP54Compatibility_Sniffs_PHP_RemovedFunctionsSniff.
21+
*
22+
* @category PHP
23+
* @package PHP54Compatibility
24+
* @author Sebastian Marek <[email protected]>
25+
* @copyright 2012 Sebastian Marek
26+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
27+
* @link https://github.com/proofek/PHP54Compatibility
28+
*/
29+
class PHP54Compatibility_Sniffs_PHP_RemovedFunctionsSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff
30+
{
31+
/**
32+
* A list of forbidden functions with their alternatives.
33+
*
34+
* The value is NULL if no alternative exists. IE, the
35+
* function should just not be used.
36+
*
37+
* @var array(string => string|null)
38+
*/
39+
protected $forbiddenFunctions = array(
40+
'define_syslog_vairables' => null,
41+
'import_request_variables' => null,
42+
'session_is_registered' => null,
43+
'session_register' => null,
44+
'session_unregister' => null,
45+
'mysqli_bind_param' => null,
46+
'mysqli_bind_result' => null,
47+
'mysqli_client_encoding' => null,
48+
'mysqli_fetch' => null,
49+
'mysqli_param_count' => null,
50+
'mysqli_get_metadata' => null,
51+
'mysqli_send_long_data' => null,
52+
);
53+
54+
/**
55+
* If true, an error will be thrown; otherwise a warning.
56+
*
57+
* @var bool
58+
*/
59+
public $error = true;
60+
61+
/**
62+
* Generates the error or wanrning for this sniff.
63+
*
64+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
65+
* @param int $stackPtr The position of the forbidden function
66+
* in the token array.
67+
* @param string $function The name of the forbidden function.
68+
* @param string $pattern The pattern used for the match.
69+
*
70+
* @return void
71+
*/
72+
protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
73+
{
74+
$data = array($function);
75+
$error = 'The use of function %s() is ';
76+
$type = 'Removed';
77+
$error .= 'removed';
78+
79+
if ($pattern === null) {
80+
$pattern = $function;
81+
}
82+
83+
if ($this->forbiddenFunctions[$pattern] !== null) {
84+
$type .= 'WithAlternative';
85+
$data[] = $this->forbiddenFunctions[$pattern];
86+
$error .= '; use %s() instead';
87+
}
88+
89+
if ($this->error === true) {
90+
$phpcsFile->addError("[PHP 5.4] $error", $stackPtr, $type, $data);
91+
} else {
92+
$phpcsFile->addWarning("[PHP 5.4] $error", $stackPtr, $type, $data);
93+
}
94+
95+
}
96+
}

0 commit comments

Comments
 (0)