cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/PHP ForbiddenFunctionsSniff.php
/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP ForbiddenFunctionsUnitTest.inc ForbiddenFunctionsUnitTest.php

From: Date: Thu, 28 Dec 2006 06:01:18 +0000
Subject: cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/PHP ForbiddenFunctionsSniff.php
/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP ForbiddenFunctionsUnitTest.inc ForbiddenFunctionsUnitTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-43861@lists.php.net to get a copy of this message
squiz Thu Dec 28 06:01:18 2006 UTC Added files: /pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/PHP ForbiddenFunctionsSniff.php /pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP ForbiddenFunctionsUnitTest.inc ForbiddenFunctionsUnitTest.php Log: New generic sniff that allows you to forbid the use of any functions you want. Defaults to forbid the use of alias functions. http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php <?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,v 1.1 2006/12/28 06:01:16 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * 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: @package_version@ * @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', ); /** * 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(); $function = strtolower($tokens[$stackPtr]['content']); if (!in_array($function, array_keys($this->forbiddenFunctions))) { return; } $error = "The use of function $function() is forbidden"; if ($this->forbiddenFunctions[$function] !== null) { $error .= '; use '.$this->forbiddenFunctions[$function].'() instead'; } $phpcsFile->addError($error, $stackPtr); }//end process() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc <?php $size = sizeof($array); $size = count($array); delete($filepath); unset($filepath); ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.php <?php /** * Unit test class for the ForbiddenFunctions sniff. * * 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: ForbiddenFunctionsUnitTest.php,v 1.1 2006/12/28 06:01:17 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the ForbiddenFunctions sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Generic_Tests_PHP_ForbiddenFunctionsUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 2 => 1, 4 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?>

« previous php.pear.cvs (#43861) next »