Blame | Letzte Änderung | Log anzeigen | RSS feed
<?php/*** Generic_Sniffs_PHP_ForbiddenFunctionsSniff.** PHP version 5** @category PHP* @package PHP_CodeSniffer* @author Greg Sherwood <gsherwood@squiz.net>* @author Marc McIntyre <mmcintyre@squiz.net>* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence* @version CVS: $Id: ForbiddenFunctionsSniff.php 290853 2009-11-17 03:17:17Z squiz $* @link http://pear.php.net/package/PHP_CodeSniffer*//*** Generic_Sniffs_PHP_ForbiddenFunctionsSniff.** Discourages the use of alias functions that are kept in PHP for compatibility* with older versions. Can be used to forbid the use of any function.** @category PHP* @package PHP_CodeSniffer* @author Greg Sherwood <gsherwood@squiz.net>* @author Marc McIntyre <mmcintyre@squiz.net>* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence* @version Release: 1.2.1* @link http://pear.php.net/package/PHP_CodeSniffer*/class Generic_Sniffs_PHP_ForbiddenFunctionsSniff implements PHP_CodeSniffer_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)*/protected $forbiddenFunctions = array('sizeof' => 'count','delete' => 'unset',);/*** If true, an error will be thrown; otherwise a warning.** @var bool*/protected $error = true;/*** Returns an array of tokens this test wants to listen for.** @return array*/public function register(){return array(T_STRING);}//end register()/*** 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){$tokens = $phpcsFile->getTokens();$ignore = array(T_DOUBLE_COLON,T_OBJECT_OPERATOR,T_FUNCTION,T_CONST,);$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);if (in_array($tokens[$prevToken]['code'], $ignore) === true) {// Not a call to a PHP function.return;}$function = strtolower($tokens[$stackPtr]['content']);if (in_array($function, array_keys($this->forbiddenFunctions)) === false) {return;}$error = "The use of function $function() is ";if ($this->error === true) {$error .= 'forbidden';} else {$error .= 'discouraged';}if ($this->forbiddenFunctions[$function] !== null) {$error .= '; use '.$this->forbiddenFunctions[$function].'() instead';}if ($this->error === true) {$phpcsFile->addError($error, $stackPtr);} else {$phpcsFile->addWarning($error, $stackPtr);}}//end process()}//end class?>