Skip to content

Commit

Permalink
DateTime/CurrentTimeTimestamp: rename some local variables
Browse files Browse the repository at this point in the history
... to be more descriptive in the context of named parameters.

Includes defensive coding tweak of making sure the `$content_type` variable is always defined.
  • Loading branch information
jrfnl committed Aug 17, 2023
1 parent 3bca142 commit 782f8fe
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions WordPress/Sniffs/DateTime/CurrentTimeTimestampSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
return;
}

$content_type = '';
for ( $i = $type_param['start']; $i <= $type_param['end']; $i++ ) {
if ( isset( Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) ) {
continue;
}

if ( isset( Tokens::$textStringTokens[ $this->tokens[ $i ]['code'] ] ) ) {
$content_first = trim( TextStrings::stripQuotes( $this->tokens[ $i ]['content'] ) );
if ( 'U' !== $content_first && 'timestamp' !== $content_first ) {
$content_type = trim( TextStrings::stripQuotes( $this->tokens[ $i ]['content'] ) );
if ( 'U' !== $content_type && 'timestamp' !== $content_type ) {
// Most likely valid use of current_time().
return;
}
Expand All @@ -113,10 +114,10 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
*/
$gmt_param = PassedParameters::getParameterFromStack( $parameters, 2, 'gmt' );
if ( is_array( $gmt_param ) ) {
$content_second = '';
$content_gmt = '';
if ( 'true' === $gmt_param['clean'] || '1' === $gmt_param['clean'] ) {
$content_second = $gmt_param['clean'];
$gmt_true = true;
$content_gmt = $gmt_param['clean'];
$gmt_true = true;
}
}

Expand All @@ -140,9 +141,9 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
$error = 'Don\'t use current_time() for retrieving a Unix (UTC) timestamp. Use time() instead. Found: %s';
$error_code = 'RequestedUTC';

$code_snippet = "current_time( '" . $content_first . "'";
if ( isset( $content_second ) ) {
$code_snippet .= ', ' . $content_second;
$code_snippet = "current_time( '" . $content_type . "'";
if ( isset( $content_gmt ) ) {
$code_snippet .= ', ' . $content_gmt;
}
$code_snippet .= ' )';

Expand Down

0 comments on commit 782f8fe

Please sign in to comment.