Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CS checking based on the WP_CLI_CS ruleset #46

Merged
merged 13 commits into from
Apr 19, 2019
Merged
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
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_autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
thrijith marked this conversation as resolved.
Show resolved Hide resolved
if ( file_exists( $wpcli_cron_autoload ) ) {
require_once $wpcli_cron_autoload;
}

WP_CLI::add_command( 'cron', 'Cron_Command' );
Expand Down
72 changes: 72 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 -->
thrijith marked this conversation as resolved.
Show resolved Hide resolved
<file>.</file>

<!-- Ignoring select files/folders
thrijith marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- 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 WPCliCS RULESET
#############################################################################
-->

<rule ref="WPCliCS"/>

<!--
#############################################################################
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-"/>

<!-- Make this sniff slightly less finicky. The WP Core default is 40. -->
<rule ref="Generic.Formatting.MultipleStatementAlignment">
thrijith marked this conversation as resolved.
Show resolved Hide resolved
<properties>
<property name="maxPadding" value="20"/>
</properties>
</rule>

<!-- 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_Command\.php$</exclude-pattern>
thrijith marked this conversation as resolved.
Show resolved Hide resolved
<exclude-pattern>*/src/Cron_(Event|Schedule)_Command\.php$</exclude-pattern>
</rule>

</ruleset>
19 changes: 11 additions & 8 deletions src/Cron_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,18 @@ 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(
'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 )
$cron_request = apply_filters(
'cron_request', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
thrijith marked this conversation as resolved.
Show resolved Hide resolved
array(
thrijith marked this conversation as resolved.
Show resolved Hide resolved
'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
),
)
) );
);

# 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
71 changes: 38 additions & 33 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' );
$hook = $args[0];
$next_run = \WP_CLI\Utils\get_flag_value( $args, 1, 'now' );
thrijith marked this conversation as resolved.
Show resolved Hide resolved
$recurrence = \WP_CLI\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 @@ -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' ) ) {
$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 ( ! \WP_CLI\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 ) ) {
schlessera marked this conversation as resolved.
Show resolved Hide resolved
$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,17 +278,17 @@ public function run( $args, $assoc_args ) {
protected static function run_event( stdClass $event ) {

if ( ! defined( 'DOING_CRON' ) ) {
define( 'DOING_CRON', true );
define( 'DOING_CRON', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
thrijith marked this conversation as resolved.
Show resolved Hide resolved
}

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 );

do_action_ref_array( $event->hook, $event->args );
do_action_ref_array( $event->hook, $event->args ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
thrijith marked this conversation as resolved.
Show resolved Hide resolved

return true;

Expand Down Expand Up @@ -316,7 +319,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 +330,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 +347,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 @@ -425,13 +428,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 +444,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 ) ) );
thrijith marked this conversation as resolved.
Show resolved Hide resolved

// 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 ) ) );
thrijith marked this conversation as resolved.
Show resolved Hide resolved
}
}

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