Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -142,53 +142,54 @@ 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,
);
}

...
}
```

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.

Expand Down
4 changes: 0 additions & 4 deletions WordPress-Core/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,6 @@
Ref: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#regular-expressions
#############################################################################
-->
<!-- Covers rule: Perl compatible regular expressions should be used in preference
to their POSIX counterparts. -->
<rule ref="WordPress.PHP.POSIXFunctions"/>

<!-- Rule: Never use the /e switch, use preg_replace_callback instead.
https://github.com/WordPress/WordPress-Coding-Standards/issues/632 -->

Expand Down
32 changes: 31 additions & 1 deletion WordPress/Sniffs/PHP/POSIXFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace WordPressCS\WordPress\Sniffs\PHP;

use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;

/**
Expand All @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion WordPress/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
<rule ref="WordPress-Core"/>
-->
<rule ref="WordPress-Docs"/>
<rule ref="WordPress-Extra"/>
<rule ref="WordPress-Extra">
<!--
Exclude deprecated sniffs.

Note: While this exclusion has nothing to do with the included Extra ruleset, exclusions must be placed within a "rule"
and as all sniffs from WordPressCS are automatically included, we don't have a "WordPress"
rule in which to place the exclusion, so this will have to do for now.
-->
<exclude name="WordPress.PHP.POSIXFunctions"/>
</rule>

</ruleset>
Loading