From c4d5d33b804cbd29270aa0ad826ad2f4fb51236e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 4 Jan 2013 16:35:46 +0100 Subject: [PATCH] Add Sniff that discourages use of raw PCRE Closes #1054 --- .../WCF/Sniffs/PHP/DiscouragePCRESniff.php | 52 +++++++++++++++++++ CodeSniff/WCF/ruleset.xml | 1 + .../install/files/lib/system/Regex.class.php | 2 + 3 files changed, 55 insertions(+) create mode 100644 CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php diff --git a/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php b/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php new file mode 100644 index 0000000000..5d300e3ecf --- /dev/null +++ b/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php @@ -0,0 +1,52 @@ + + * @package com.woltlab.wcf + * @category Community Framework + */ +class WCF_Sniffs_PHP_DiscouragePCRESniff 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( + 'preg_match_all' => null, + 'preg_match' => null, + 'preg_replace' => null, + 'preg_replace_callback' => null, + 'preg_split' => null + ); + + /** + * Constructor. + */ + public function __construct() { + return; + } + + /** + * Generates the error or warning 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 = 'Use of raw PCRE %s() is discouraged. Please use \wcf\system\Regex which provides OO-access to regex'; + $type = 'Discourage'; + + $phpcsFile->addWarning($error, $stackPtr, $type, $data); + } +} diff --git a/CodeSniff/WCF/ruleset.xml b/CodeSniff/WCF/ruleset.xml index 8d6a7820ce..4c1e73d580 100644 --- a/CodeSniff/WCF/ruleset.xml +++ b/CodeSniff/WCF/ruleset.xml @@ -31,6 +31,7 @@ + diff --git a/wcfsetup/install/files/lib/system/Regex.class.php b/wcfsetup/install/files/lib/system/Regex.class.php index 65acebd0f9..4b5cc409eb 100644 --- a/wcfsetup/install/files/lib/system/Regex.class.php +++ b/wcfsetup/install/files/lib/system/Regex.class.php @@ -125,6 +125,7 @@ final class Regex { } } + // @codingStandardsIgnoreStart /** * Checks whether the regex matches the given string. * @@ -164,6 +165,7 @@ final class Regex { public function split($string) { return $this->checkResult(preg_split($this->regex, $string), 'split'); } + // @codingStandardsIgnoreEnd /** * Checks whether there was success. -- 2.20.1