-
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New "DateTime.RestrictedFunctions" sniff
This introduces a new `WordPress.DateTime.RestrictedFunctions` sniff which initially includes two groups: * `timezone_change` - moved from the `WordPress.WP.TimezoneChange` sniff * `date` - moved from the `WordPress.PHP.RestrictedPHPFunctions` sniff (group not yet in a released WPCS version yet) The `WordPress.WP.TimezoneChange` sniff is now deprecated. * The sniff is no longer included in the WPCS rulesets. * If the sniff is explicitly included via a custom ruleset, deprecation notices will be thrown. * If the `exclude` property is set from with a custom ruleset, a deprecation notice will be thrown. The new sniff is now included in the `Core` ruleset. Note: once WP Core upgrades, the one instance of using `date_default_timezone_set()` in WP Core (in `wp-settings.php`) will need to be whitelisted inline. There are a few more occurrences in the unit tests, but those can be ignored via file based excludes. Fixes 1805
- Loading branch information
Showing
12 changed files
with
187 additions
and
36 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,62 @@ | ||
<?php | ||
/** | ||
* WordPress Coding Standard. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||
* @license https://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
namespace WordPressCS\WordPress\Sniffs\DateTime; | ||
|
||
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff; | ||
|
||
/** | ||
* Forbids the use of various native DateTime related PHP/WP functions and suggests alternatives. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* | ||
* @since 2.2.0 | ||
*/ | ||
class RestrictedFunctionsSniff extends AbstractFunctionRestrictionsSniff { | ||
|
||
/** | ||
* Groups of functions to restrict. | ||
* | ||
* @return array | ||
*/ | ||
public function getGroups() { | ||
return array( | ||
|
||
/* | ||
* Disallow the changing the timezone. | ||
* | ||
* @link https://vip.wordpress.com/documentation/vip-go/code-review-blockers-warnings-notices/#manipulating-the-timezone-server-side | ||
*/ | ||
'timezone_change' => array( | ||
'type' => 'error', | ||
'message' => 'Using %s() and similar isn\'t allowed, instead use WP internal timezone support.', | ||
'functions' => array( | ||
'date_default_timezone_set', | ||
), | ||
), | ||
|
||
/* | ||
* Use gmdate(), not date(). | ||
* Don't rely on the current PHP time zone as it might have been changed by third party code. | ||
* | ||
* @link https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/ | ||
* @link https://core.trac.wordpress.org/ticket/46438 | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards/issues/1713 | ||
*/ | ||
'date' => array( | ||
'type' => 'error', | ||
'message' => '%s() is affected by runtime timezone changes which can cause date/time to be incorrectly displayed. Use gmdate() instead.', | ||
'functions' => array( | ||
'date', | ||
), | ||
), | ||
); | ||
} | ||
|
||
} |
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,9 @@ | ||
<?php | ||
|
||
date_default_timezone_set( 'Foo/Bar' ); // Bad. | ||
|
||
$date = new DateTime(); | ||
$date->setTimezone( new DateTimeZone( 'America/Toronto' ) ); // Yay! | ||
|
||
$post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), date( __( 'g:i a' ), $now ) ); // Error. | ||
$post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), gmdate( __( 'F j, Y' ), $now ), gmdate( __( 'g:i a' ), $now ) ); // OK. |
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,44 @@ | ||
<?php | ||
/** | ||
* Unit test class for WordPress Coding Standard. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||
* @license https://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
namespace WordPressCS\WordPress\Tests\DateTime; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
/** | ||
* Unit test class for the DateTime.RestrictedFunctions sniff. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* | ||
* @since 2.2.0 | ||
*/ | ||
class RestrictedFunctionsUnitTest extends AbstractSniffUnitTest { | ||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* @return array <int line number> => <int number of errors> | ||
*/ | ||
public function getErrorList() { | ||
return array( | ||
3 => 1, | ||
8 => 2, | ||
); | ||
} | ||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* @return array <int line number> => <int number of warnings> | ||
*/ | ||
public function getWarningList() { | ||
return array(); | ||
} | ||
|
||
} |
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
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