Source for file ForbiddenFunctionsSniff.php
Documentation is available at ForbiddenFunctionsSniff.php
* Discourages the use of alias functions.
* Alias functions are kept in PHP for compatibility
* with older versions. Can be used to forbid the use of any function.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class ForbiddenFunctionsSniff implements Sniff
* 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>
public $forbiddenFunctions = array (
* A cache of forbidden function names, for faster lookups.
protected $forbiddenFunctionNames = array ();
* If true, forbidden functions will be considered regular expressions.
protected $patternMatch = false;
* If true, an error will be thrown; otherwise a warning.
* Returns an array of tokens this test wants to listen for.
public function register ()
// Everyone has had a chance to figure out what forbidden functions
// they want to check for, so now we can cache out the list.
$this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
if ($this->patternMatch === true ) {
foreach ($this->forbiddenFunctionNames as $i => $name) {
$this->forbiddenFunctionNames[$i] = '/'. $name. '/i';
// If we are not pattern matching, we need to work out what
foreach ($this->forbiddenFunctionNames as $name) {
foreach ($tokens as $token) {
$this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
$this->forbiddenFunctions = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
* Processes this test, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
T_OBJECT_OPERATOR => true ,
$prevToken = $phpcsFile->findPrevious (T_WHITESPACE , ($stackPtr - 1 ), null , true );
// If function call is directly preceded by a NS_SEPARATOR it points to the
// global namespace, so we should still catch it.
if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR ) {
$prevToken = $phpcsFile->findPrevious (T_WHITESPACE , ($prevToken - 1 ), null , true );
if ($tokens[$prevToken]['code'] === T_STRING ) {
// Not in the global namespace.
if (isset ($ignore[$tokens[$prevToken]['code']]) === true ) {
// Not a call to a PHP function.
$nextToken = $phpcsFile->findNext (T_WHITESPACE , ($stackPtr + 1 ), null , true );
if (isset ($ignore[$tokens[$nextToken]['code']]) === true ) {
// Not a call to a PHP function.
if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
// Not a call to a PHP function.
$function = strtolower($tokens[$stackPtr]['content']);
if ($this->patternMatch === true ) {
$this->forbiddenFunctionNames,
$this->forbiddenFunctionNames,
// Remove the pattern delimiters and modifier.
$pattern = substr($pattern, 1 , -2 );
if (in_array($function, $this->forbiddenFunctionNames) === false ) {
$this->addError ($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
* Generates the error or warning for this sniff.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the forbidden function
* @param string $function The name of the forbidden function.
* @param string $pattern The pattern used for the match.
protected function addError ($phpcsFile, $stackPtr, $function, $pattern=null )
$data = array ($function);
$error = 'The use of function %s() is ';
if ($this->error === true ) {
if ($this->forbiddenFunctions[$pattern] !== null
&& $this->forbiddenFunctions[$pattern] !== 'null'
$type .= 'WithAlternative';
$data[] = $this->forbiddenFunctions[$pattern];
$error .= '; use %s() instead';
if ($this->error === true ) {
$phpcsFile->addError ($error, $stackPtr, $type, $data);
$phpcsFile->addWarning ($error, $stackPtr, $type, $data);
Documentation generated on Mon, 11 Mar 2019 15:27:29 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|