From 87a3919c081178c89ec3d09eba94fabafa2eeb88 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 20 Jul 2016 18:20:09 +0200 Subject: [PATCH] Add ability to whitelist specific functions to the `AbstractFunctionRestrictions` class. This is only useful when used in combination with blocking a function group using a wildcard in the function name. However, it does make it more forward compatible. This allows to still block a complete (PHP) extension , even for new functions being added to the extension, while whitelisting - for instance - a WP native function which was named a bit unfortunately. --- WordPress/AbstractFunctionRestrictionsSniff.php | 4 ++++ WordPress/Sniffs/DB/RestrictedFunctionsSniff.php | 3 +++ WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc | 14 ++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/WordPress/AbstractFunctionRestrictionsSniff.php b/WordPress/AbstractFunctionRestrictionsSniff.php index dca37bd7..4598c1a7 100644 --- a/WordPress/AbstractFunctionRestrictionsSniff.php +++ b/WordPress/AbstractFunctionRestrictionsSniff.php @@ -165,6 +165,10 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { continue; } + if ( isset( $group['whitelist'][ $token['content'] ] ) ) { + continue; + } + if ( preg_match( $group['regex'], $token['content'] ) < 1 ) { continue; } diff --git a/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php b/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php index 72d2cb3f..8fb9735a 100644 --- a/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php +++ b/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php @@ -51,6 +51,9 @@ public function getGroups() { 'mysqlnd_memcache_*', 'maxdb_*', ), + 'whitelist' => array( + 'mysql_to_rfc3339' => true, + ), ), ); diff --git a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc index 5c8c29fc..821abf7c 100644 --- a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc @@ -74,3 +74,17 @@ maxdb_num_fields(); maxdb_prepare(); maxdb_real_query maxdb_stat(); + + +/** + * And these shouldn't give an error. + */ + +// WP Native function which was named a bit unfortunately. +mysql_to_rfc3339(); // Ok. + +// Other WP Native functions which shouldn't give a problem anyway. +mysql2date(); // Ok. +wp_check_mysql_version(); // Ok. +wp_check_php_mysql_version(); // Ok. +WP_Date_Query::build_mysql_datetime(); // Ok.