-
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split off POSIX Functions restrictions to its own sniff.
* Removes similar checks from existing sniffs (PHP.DiscouragedFunctions and VIP.RestrictedFunctions). * Makes sure that *all* functions in the POSIX extension are covered by this check - they previously were not. * Add unit tests for the new sniff covering *all* POSIX functions.
- Loading branch information
Showing
8 changed files
with
165 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* WordPress Coding Standard. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ | ||
*/ | ||
|
||
/** | ||
* Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference | ||
* to their POSIX counterparts. | ||
* | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#regular-expressions | ||
* @link http://php.net/manual/en/ref.regex.php | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
*/ | ||
class WordPress_Sniffs_PHP_POSIXFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { | ||
|
||
/** | ||
* Groups of functions to restrict. | ||
* | ||
* Example: groups => array( | ||
* 'lambda' => array( | ||
* 'type' => 'error' | 'warning', | ||
* 'message' => 'Use anonymous functions instead please!', | ||
* 'functions' => array( 'eval', 'create_function' ), | ||
* ) | ||
* ) | ||
* | ||
* @return array | ||
*/ | ||
public function getGroups() { | ||
return array( | ||
'ereg' => array( | ||
'type' => 'error', | ||
'message' => '%s has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_match() instead.', | ||
'functions' => array( | ||
'ereg', | ||
'eregi', | ||
'sql_regcase', | ||
), | ||
), | ||
|
||
'ereg_replace' => array( | ||
'type' => 'error', | ||
'message' => '%s has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_replace() instead.', | ||
'functions' => array( | ||
'ereg_replace', | ||
'eregi_replace', | ||
), | ||
), | ||
|
||
'split' => array( | ||
'type' => 'error', | ||
'message' => '%s has been deprecated since PHP 5.3 and removed in PHP 7.0, please use explode(), str_split() or preg_split() instead.', | ||
'functions' => array( | ||
'split', | ||
'spliti', | ||
), | ||
), | ||
|
||
); | ||
} // end getGroups() | ||
|
||
} // end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
// These are ok. | ||
$regex = preg_quote( 'cool', '#' ); // Good | ||
$title = preg_match( 'cool', get_the_title() ); // Good | ||
$title = preg_match_all( 'cool', get_the_title(), $matches ); // Good | ||
$title = preg_replace( 'cool', 'not cool', get_the_title() ); // Good | ||
$title = preg_replace_callback( 'cool', 'callback_function', get_the_title() ); // Good | ||
$title = preg_split( 'cool', get_the_title() ); // Good | ||
|
||
|
||
// These should all trigger an error. | ||
if ( ereg( '[A-Za-z]+', $title, $regs ) ) // Bad, ereg deprecated. Use preg_match instead. | ||
die( $regs ); | ||
|
||
if ( eregi( '[a-z]+', $title, $regs ) ) {} // Bad, eregi deprecated. Use preg_match instead. | ||
|
||
$title = ereg_replace( 'cool', 'not cool', get_the_title() ); // Bad, ereg_replace has been deprecated. Use preg_replace instead. | ||
|
||
$title = eregi_replace( 'cool', 'not cool', get_the_title() ); // Bad, eregi_replace also deprecated. Use preg_replace instead. | ||
|
||
list( $year, $month, $day ) = split( ':', $date ); // Bad, split has been deprecated. Use preg_split or explode instead. | ||
|
||
$title_parts = spliti( ' ', get_the_title(), 4 ); // Bad, spliti also deprecated. Use preg_split instead. | ||
|
||
sql_regcase( 'Foo - bar.'); // Bad. Deprecated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Unit test class for WordPress Coding Standard. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ | ||
*/ | ||
|
||
/** | ||
* WordPress_Tests_PHP_POSIXFunctionsUnitTest | ||
* | ||
* 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 Akeda Bagus <[email protected]> | ||
* @author Greg Sherwood <[email protected]> | ||
* @author Marc McIntyre <[email protected]> | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class WordPress_Tests_PHP_POSIXFunctionsUnitTest 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( | ||
13 => 1, | ||
16 => 1, | ||
18 => 1, | ||
20 => 1, | ||
22 => 1, | ||
24 => 1, | ||
26 => 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 |