Skip to content

Commit

Permalink
Merge pull request #46 from thrijith/feature/use-phpcs
Browse files Browse the repository at this point in the history
Implement CS checking based on the `WP_CLI_CS` ruleset
  • Loading branch information
schlessera committed Apr 19, 2019
2 parents 1d00df4 + 9f7e116 commit dfff746
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.travis.yml
behat.yml
circle.yml
phpcs.xml.dist
phpunit.xml.dist
bin/
features/
utils/
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ vendor/
*.tar.gz
composer.lock
*.log
phpunit.xml
phpcs.xml
.phpcs.xml
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require-dev": {
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/wp-cli-tests": "^2.0.11"
"wp-cli/wp-cli-tests": "^2.1"
},
"config": {
"process-timeout": 7200,
Expand Down
6 changes: 3 additions & 3 deletions cron-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
return;
}

$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( file_exists( $autoload ) ) {
require_once $autoload;
$wpcli_cron_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( file_exists( $wpcli_cron_autoloader ) ) {
require_once $wpcli_cron_autoloader;
}

WP_CLI::add_command( 'cron', 'Cron_Command' );
Expand Down
59 changes: 59 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0"?>
<ruleset name="WP-CLI-cron">
<description>Custom ruleset for WP-CLI cron-command</description>

<!--
#############################################################################
COMMAND LINE ARGUMENTS
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
#############################################################################
-->

<!-- What to scan. -->
<file>.</file>

<!-- Show progress. -->
<arg value="p"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>

<!--
#############################################################################
USE THE WP_CLI_CS RULESET
#############################################################################
-->

<rule ref="WP_CLI_CS"/>

<!--
#############################################################################
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
#############################################################################
-->

<!-- For help understanding the `testVersion` configuration setting:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/>

<!-- Verify that everything in the global namespace is either namespaced or prefixed.
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="WP_CLI\Cron"/><!-- Namespaces. -->
<element value="wpcli_cron"/><!-- Global variables and such. -->
</property>
</properties>
</rule>

<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
<exclude-pattern>*/src/Cron_(Event_|Schedule_)?Command\.php$</exclude-pattern>
</rule>

</ruleset>
12 changes: 8 additions & 4 deletions src/Cron_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ protected static function get_cron_spawn() {
$sslverify = \WP_CLI\Utils\wp_version_compare( 4.0, '<' );
$doing_wp_cron = sprintf( '%.22F', microtime( true ) );

$cron_request = apply_filters( 'cron_request', array(
$cron_request_array = array(
'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
'key' => $doing_wp_cron,
'args' => array(
'timeout' => 3,
'blocking' => true,
'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify )
)
) );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ),
),
);

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
$cron_request = apply_filters( 'cron_request', $cron_request_array );

# Enforce a blocking request in case something that's hooked onto the 'cron_request' filter sets it to false
$cron_request['args']['blocking'] = true;
Expand Down
77 changes: 42 additions & 35 deletions src/Cron_Event_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use WP_CLI\Utils;

/**
* Schedules, runs, and deletes WP-Cron events.
*
Expand Down Expand Up @@ -31,6 +33,7 @@ class Cron_Event_Command extends WP_CLI_Command {
'next_run_relative',
'recurrence',
);

private static $time_format = 'Y-m-d H:i:s';

/**
Expand Down Expand Up @@ -112,7 +115,7 @@ public function list_( $args, $assoc_args ) {
}
}

if ( 'ids' == $formatter->format ) {
if ( 'ids' === $formatter->format ) {
echo implode( ' ', wp_list_pluck( $events, 'hook' ) );
} else {
$formatter->display_items( $events );
Expand Down Expand Up @@ -153,13 +156,13 @@ public function list_( $args, $assoc_args ) {
*/
public function schedule( $args, $assoc_args ) {

$hook = $args[0];
$next_run = \WP_CLI\Utils\get_flag_value( $args, 1, 'now' );
$recurrence = \WP_CLI\Utils\get_flag_value( $args, 2, false );
$hook = $args[0];
$next_run = Utils\get_flag_value( $args, 1, 'now' );
$recurrence = Utils\get_flag_value( $args, 2, false );

if ( empty( $next_run ) ) {
$timestamp = time();
} else if ( is_numeric( $next_run ) ) {
} elseif ( is_numeric( $next_run ) ) {
$timestamp = absint( $next_run );
} else {
$timestamp = strtotime( $next_run );
Expand All @@ -173,7 +176,7 @@ public function schedule( $args, $assoc_args ) {

$schedules = wp_get_schedules();

if ( ! isset( $schedules[$recurrence] ) ) {
if ( ! isset( $schedules[ $recurrence ] ) ) {
WP_CLI::error( sprintf( "'%s' is not a valid schedule name for recurrence.", $recurrence ) );
}

Expand Down Expand Up @@ -215,7 +218,7 @@ public function schedule( $args, $assoc_args ) {
*/
public function run( $args, $assoc_args ) {

if ( empty( $args ) && ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'due-now' ) && ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
if ( empty( $args ) && ! Utils\get_flag_value( $assoc_args, 'due-now' ) && ! Utils\get_flag_value( $assoc_args, 'all' ) ) {
WP_CLI::error( 'Please specify one or more cron events, or use --due-now/--all.' );
}

Expand All @@ -226,15 +229,15 @@ public function run( $args, $assoc_args ) {
}

$hooks = wp_list_pluck( $events, 'hook' );
foreach( $args as $hook ) {
foreach ( $args as $hook ) {
if ( ! in_array( $hook, $hooks, true ) ) {
WP_CLI::error( sprintf( "Invalid cron event '%s'", $hook ) );
}
}

if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'due-now' ) ) {
if ( Utils\get_flag_value( $assoc_args, 'due-now' ) ) {
$due_events = array();
foreach( $events as $event ) {
foreach ( $events as $event ) {
if ( ! empty( $args ) && ! in_array( $event->hook, $args, true ) ) {
continue;
}
Expand All @@ -243,10 +246,10 @@ public function run( $args, $assoc_args ) {
}
}
$events = $due_events;
} else if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
} elseif ( ! Utils\get_flag_value( $assoc_args, 'all' ) ) {
$due_events = array();
foreach( $events as $event ) {
if ( in_array( $event->hook, $args ) ) {
foreach ( $events as $event ) {
if ( in_array( $event->hook, $args, true ) ) {
$due_events[] = $event;
}
}
Expand All @@ -255,9 +258,9 @@ public function run( $args, $assoc_args ) {

$executed = 0;
foreach ( $events as $event ) {
$start = microtime( true );
$start = microtime( true );
$result = self::run_event( $event );
$total = round( microtime( true ) - $start, 3 );
$total = round( microtime( true ) - $start, 3 );
$executed++;
WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) );
}
Expand All @@ -275,16 +278,18 @@ public function run( $args, $assoc_args ) {
protected static function run_event( stdClass $event ) {

if ( ! defined( 'DOING_CRON' ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Using native WordPress constant.
define( 'DOING_CRON', true );
}

if ( $event->schedule != false ) {
if ( false !== $event->schedule ) {
$new_args = array( $event->time, $event->schedule, $event->hook, $event->args );
call_user_func_array( 'wp_reschedule_event', $new_args );
}

wp_unschedule_event( $event->time, $event->hook, $event->args );

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Can't prefix dynamic hooks here, calling registered hooks.
do_action_ref_array( $event->hook, $event->args );

return true;
Expand Down Expand Up @@ -316,7 +321,7 @@ public function delete( $args, $assoc_args ) {

$deleted = 0;
foreach ( $events as $event ) {
if ( $event->hook == $hook ) {
if ( $event->hook === $hook ) {
$result = self::delete_event( $event );
if ( $result ) {
$deleted++;
Expand All @@ -327,7 +332,7 @@ public function delete( $args, $assoc_args ) {
}

if ( $deleted ) {
$message = ( 1 == $deleted ) ? "Deleted the cron event '%2\$s'." : "Deleted %1\$d instances of the cron event '%2\$s'.";
$message = ( 1 === $deleted ) ? "Deleted the cron event '%2\$s'." : "Deleted %1\$d instances of the cron event '%2\$s'.";
WP_CLI::success( sprintf( $message, $deleted, $hook ) );
} else {
WP_CLI::error( sprintf( "Invalid cron event '%s'.", $hook ) );
Expand All @@ -344,7 +349,7 @@ public function delete( $args, $assoc_args ) {
protected static function delete_event( stdClass $event ) {
$crons = _get_cron_array();

if ( ! isset( $crons[$event->time][$event->hook][$event->sig] ) ) {
if ( ! isset( $crons[ $event->time ][ $event->hook ][ $event->sig ] ) ) {
return false;
}

Expand Down Expand Up @@ -395,7 +400,7 @@ protected static function get_cron_events() {
'sig' => $sig,
'args' => $data['args'],
'schedule' => $data['schedule'],
'interval' => \WP_CLI\Utils\get_flag_value( $data, 'interval' ),
'interval' => Utils\get_flag_value( $data, 'interval' ),
);

}
Expand Down Expand Up @@ -425,13 +430,13 @@ private static function interval( $since ) {

// array of time period chunks
$chunks = array(
array( 60 * 60 * 24 * 365 , \_n_noop( '%s year', '%s years' ) ),
array( 60 * 60 * 24 * 30 , \_n_noop( '%s month', '%s months' ) ),
array( 60 * 60 * 24 * 7, \_n_noop( '%s week', '%s weeks' ) ),
array( 60 * 60 * 24 , \_n_noop( '%s day', '%s days' ) ),
array( 60 * 60 , \_n_noop( '%s hour', '%s hours' ) ),
array( 60 , \_n_noop( '%s minute', '%s minutes' ) ),
array( 1 , \_n_noop( '%s second', '%s seconds' ) ),
array( 60 * 60 * 24 * 365, 'year' ),
array( 60 * 60 * 24 * 30, 'month' ),
array( 60 * 60 * 24 * 7, 'week' ),
array( 60 * 60 * 24, 'day' ),
array( 60 * 60, 'hour' ),
array( 60, 'minute' ),
array( 1, 'second' ),
);

// we only want to output two chunks of time here, eg:
Expand All @@ -441,26 +446,28 @@ private static function interval( $since ) {

// step one: the first chunk
for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
$seconds = $chunks[ $i ][0];
$name = $chunks[ $i ][1];

// finding the biggest chunk (if the chunk fits, break)
if ( ( $count = floor( $since / $seconds ) ) != 0 ){
$count = floor( $since / $seconds );
if ( floatval( 0 ) !== $count ) {
break;
}
}

// set output var
$output = sprintf( \_n( $name[0], $name[1], $count ), $count );
$output = sprintf( '%d %s', $count, Utils\pluralize( $name, absint( $count ) ) );

// step two: the second chunk
if ( $i + 1 < $j ) {
$seconds2 = $chunks[$i + 1][0];
$name2 = $chunks[$i + 1][1];
$seconds2 = $chunks[ $i + 1 ][0];
$name2 = $chunks[ $i + 1 ][1];

if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {
$count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
if ( floatval( 0 ) !== $count2 ) {
// add to output var
$output .= ' ' . sprintf( \_n( $name2[0], $name2[1], $count2 ), $count2 );
$output .= ' ' . sprintf( '%d %s', $count2, Utils\pluralize( $name2, absint( $count2 ) ) );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cron_Schedule_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function list_( $args, $assoc_args ) {

$schedules = self::get_schedules();

if ( 'ids' == $formatter->format ) {
if ( 'ids' === $formatter->format ) {
echo implode( ' ', wp_list_pluck( $schedules, 'name' ) );
} else {
$formatter->display_items( $schedules );
Expand All @@ -106,7 +106,7 @@ protected static function format_schedule( array $schedule, $name ) {
*/
protected static function get_schedules() {
$schedules = wp_get_schedules();
if ( !empty( $schedules ) ) {
if ( ! empty( $schedules ) ) {
uasort( $schedules, 'Cron_Schedule_Command::sort' );
$schedules = array_map( 'Cron_Schedule_Command::format_schedule', $schedules, array_keys( $schedules ) );
}
Expand Down

0 comments on commit dfff746

Please sign in to comment.