From 81d9dd4cf985280e3d6eb7211ab228ede67b512d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 16 Sep 2025 19:26:50 +0200 Subject: [PATCH 1/2] PHP/POSIXFunctions: deprecate the sniff The `WordPress.PHP.POSIXFunctions` sniff is dated and no longer relevant. With this in mind, this commit deprecates the sniff. Additionally, the sniff is removed/excluded from the WPCS rulesets in a way that users who do not directly reference the sniff will not see any deprecation notices. Users who reference the sniff from within their own PHPCS rulesets, will see the deprecation notice. Fixes 2612 Related to WordPress/wpcs-docs 153 --- WordPress-Core/ruleset.xml | 4 --- WordPress/Sniffs/PHP/POSIXFunctionsSniff.php | 32 +++++++++++++++++++- WordPress/ruleset.xml | 11 ++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/WordPress-Core/ruleset.xml b/WordPress-Core/ruleset.xml index f97bd73d90..c8f1bedfae 100644 --- a/WordPress-Core/ruleset.xml +++ b/WordPress-Core/ruleset.xml @@ -827,10 +827,6 @@ Ref: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#regular-expressions ############################################################################# --> - - - diff --git a/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php b/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php index 72f77ad551..a6fedf78a9 100644 --- a/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php +++ b/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php @@ -9,6 +9,7 @@ namespace WordPressCS\WordPress\Sniffs\PHP; +use PHP_CodeSniffer\Sniffs\DeprecatedSniff; use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff; /** @@ -22,8 +23,37 @@ * `WordPress.VIP.RestrictedFunctions` and the * `WordPress.PHP.DiscouragedPHPFunctions` sniffs. * @since 0.13.0 Class name changed: this class is now namespaced. + * + * @deprecated 3.3.0 Use the PHPCompatibility standard instead. */ -final class POSIXFunctionsSniff extends AbstractFunctionRestrictionsSniff { +final class POSIXFunctionsSniff extends AbstractFunctionRestrictionsSniff implements DeprecatedSniff { + + /** + * Provide the version number in which the sniff was deprecated. + * + * @return string + */ + public function getDeprecationVersion() { + return 'WordPressCS v3.3.0'; + } + + /** + * Provide the version number in which the sniff will be removed. + * + * @return string + */ + public function getRemovalVersion() { + return 'WordPressCS v4.0.0'; + } + + /** + * Provide a custom message to display with the deprecation. + * + * @return string + */ + public function getDeprecationMessage() { + return 'To scan for PHP cross-version compatibility issues, use the PHPCompatibility standard instead.'; + } /** * Groups of functions to restrict. diff --git a/WordPress/ruleset.xml b/WordPress/ruleset.xml index 6eb7ad8d9e..dc3f56a88b 100644 --- a/WordPress/ruleset.xml +++ b/WordPress/ruleset.xml @@ -8,6 +8,15 @@ --> - + + + + From be1fabaf6150c57085edb7ed16d2b8c53f0eef12 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 16 Sep 2025 19:27:41 +0200 Subject: [PATCH 2/2] CONTRIBUTING: update the test writing tutorial ... to no longer use the `POSIXFunctions` sniff as an example as that sniff will be removed in WPCS 4.0.0 (and is now deprecated). --- .github/CONTRIBUTING.md | 73 +++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 53bdd051fd..27df074f87 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -124,16 +124,16 @@ OK (60 tests, 6 assertions) ### Unit Testing conventions -If you look inside the `WordPress/Tests` subdirectory, you'll see the structure mimics the `WordPress/Sniffs` subdirectory structure. For example, the `WordPress/Sniffs/PHP/POSIXFunctionsSniff.php` sniff has its unit test class defined in `WordPress/Tests/PHP/POSIXFunctionsUnitTest.php` which checks the `WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc` test case file. See the file naming convention? +If you look inside the `WordPress/Tests` subdirectory, you'll see the structure mimics the `WordPress/Sniffs` subdirectory structure. For example, the `WordPress/Sniffs/PHP/TypeCastsSniff.php` sniff has its unit test class defined in `WordPress/Tests/PHP/TypeCastsUnitTest.php` which checks the `WordPress/Tests/PHP/TypeCastsUnitTest.inc` test case file. See the file naming convention? -Lets take a look at what's inside `POSIXFunctionsUnitTest.php`: +Lets take a look at what's inside `TypeCastsUnitTest.php`: ```php namespace WordPressCS\WordPress\Tests\PHP; use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; -final class POSIXFunctionsUnitTest extends AbstractSniffUnitTest { +final class TypeCastsUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. @@ -142,13 +142,12 @@ final class POSIXFunctionsUnitTest extends AbstractSniffUnitTest { */ public function getErrorList() { return array( + 10 => 1, + 11 => 1, 13 => 1, - 16 => 1, - 18 => 1, - 20 => 1, - 22 => 1, - 24 => 1, 26 => 1, + 27 => 1, + 28 => 1, ); } @@ -156,39 +155,41 @@ final class POSIXFunctionsUnitTest extends AbstractSniffUnitTest { } ``` -Also note the class name convention. The method `getErrorList()` MUST return an array of line numbers indicating errors (when running `phpcs`) found in `WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc`. Similarly, the `getWarningList()` method must return an array of line numbers with the number of expected warnings. +Also note the class name convention. The method `getErrorList()` MUST return an array of line numbers indicating errors (when running `phpcs`) found in `WordPress/Tests/PHP/TypeCastsUnitTest.inc`. Similarly, the `getWarningList()` method must return an array of line numbers with the number of expected warnings. If you run the following from the root directory of your WordPressCS clone: ```sh -$ "vendor/bin/phpcs" --standard=Wordpress -s ./WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc --sniffs=WordPress.PHP.POSIXFunctions -... --------------------------------------------------------------------------------- -FOUND 7 ERRORS AFFECTING 7 LINES --------------------------------------------------------------------------------- - 13 | ERROR | ereg() has been deprecated since PHP 5.3 and removed in PHP 7.0, - | | please use preg_match() instead. - | | (WordPress.PHP.POSIXFunctions.ereg_ereg) - 16 | ERROR | eregi() has been deprecated since PHP 5.3 and removed in PHP 7.0, - | | please use preg_match() instead. - | | (WordPress.PHP.POSIXFunctions.ereg_eregi) - 18 | ERROR | ereg_replace() has been deprecated since PHP 5.3 and removed in - | | PHP 7.0, please use preg_replace() instead. - | | (WordPress.PHP.POSIXFunctions.ereg_replace_ereg_replace) - 20 | ERROR | eregi_replace() has been deprecated since PHP 5.3 and removed in - | | PHP 7.0, please use preg_replace() instead. - | | (WordPress.PHP.POSIXFunctions.ereg_replace_eregi_replace) - 22 | ERROR | split() has been deprecated since PHP 5.3 and removed in PHP 7.0, - | | please use explode(), str_split() or preg_split() instead. - | | (WordPress.PHP.POSIXFunctions.split_split) - 24 | ERROR | spliti() has been deprecated since PHP 5.3 and removed in PHP - | | 7.0, please use explode(), str_split() or preg_split() - | | instead. (WordPress.PHP.POSIXFunctions.split_spliti) - 26 | ERROR | sql_regcase() has been deprecated since PHP 5.3 and removed in - | | PHP 7.0, please use preg_match() instead. - | | (WordPress.PHP.POSIXFunctions.ereg_sql_regcase) --------------------------------------------------------------------------------- +$ "vendor/bin/phpcs" --standard=Wordpress -s ./WordPress/Tests/PHP/TypeCastsUnitTest.inc --sniffs=WordPress.PHP.TypeCasts ... +---------------------------------------------------------------------------------------------------- +FOUND 6 ERRORS AND 4 WARNINGS AFFECTING 10 LINES +---------------------------------------------------------------------------------------------------- + 10 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(double)" + | | (WordPress.PHP.TypeCasts.DoubleRealFound) + 11 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(real)" + | | (WordPress.PHP.TypeCasts.DoubleRealFound) + 13 | ERROR | [ ] Using the "(unset)" cast is forbidden as the type cast is removed in PHP 8.0. + | | Use the "unset()" language construct instead. + | | (WordPress.PHP.TypeCasts.UnsetFound) + 15 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "(binary)" + | | (WordPress.PHP.TypeCasts.BinaryFound) + 16 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "b" + | | (WordPress.PHP.TypeCasts.BinaryFound) + 17 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "b" + | | (WordPress.PHP.TypeCasts.BinaryFound) + 26 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(double)" + | | (WordPress.PHP.TypeCasts.DoubleRealFound) + 27 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(real)" + | | (WordPress.PHP.TypeCasts.DoubleRealFound) + 28 | ERROR | [ ] Using the "(unset)" cast is forbidden as the type cast is removed in PHP 8.0. + | | Use the "unset()" language construct instead. + | | (WordPress.PHP.TypeCasts.UnsetFound) + 29 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "(binary)" + | | (WordPress.PHP.TypeCasts.BinaryFound) +---------------------------------------------------------------------------------------------------- +PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY +---------------------------------------------------------------------------------------------------- ``` You'll see the line number and number of ERRORs we need to return in the `getErrorList()` method.