Skip to content

Commit dfff746

Browse files
authored
Merge pull request #46 from thrijith/feature/use-phpcs
Implement CS checking based on the `WP_CLI_CS` ruleset
2 parents 1d00df4 + 9f7e116 commit dfff746

File tree

8 files changed

+120
-45
lines changed

8 files changed

+120
-45
lines changed

.distignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
.travis.yml
77
behat.yml
88
circle.yml
9+
phpcs.xml.dist
10+
phpunit.xml.dist
911
bin/
1012
features/
1113
utils/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ vendor/
66
*.tar.gz
77
composer.lock
88
*.log
9+
phpunit.xml
10+
phpcs.xml
11+
.phpcs.xml

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"require-dev": {
1818
"wp-cli/entity-command": "^1.3 || ^2",
19-
"wp-cli/wp-cli-tests": "^2.0.11"
19+
"wp-cli/wp-cli-tests": "^2.1"
2020
},
2121
"config": {
2222
"process-timeout": 7200,

cron-command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
return;
55
}
66

7-
$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
8-
if ( file_exists( $autoload ) ) {
9-
require_once $autoload;
7+
$wpcli_cron_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
8+
if ( file_exists( $wpcli_cron_autoloader ) ) {
9+
require_once $wpcli_cron_autoloader;
1010
}
1111

1212
WP_CLI::add_command( 'cron', 'Cron_Command' );

phpcs.xml.dist

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-cron">
3+
<description>Custom ruleset for WP-CLI cron-command</description>
4+
5+
<!--
6+
#############################################################################
7+
COMMAND LINE ARGUMENTS
8+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
9+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
10+
#############################################################################
11+
-->
12+
13+
<!-- What to scan. -->
14+
<file>.</file>
15+
16+
<!-- Show progress. -->
17+
<arg value="p"/>
18+
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
21+
22+
<!-- Check up to 8 files simultaneously. -->
23+
<arg name="parallel" value="8"/>
24+
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
30+
31+
<rule ref="WP_CLI_CS"/>
32+
33+
<!--
34+
#############################################################################
35+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
36+
#############################################################################
37+
-->
38+
39+
<!-- For help understanding the `testVersion` configuration setting:
40+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41+
<config name="testVersion" value="5.4-"/>
42+
43+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
44+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
45+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
46+
<properties>
47+
<property name="prefixes" type="array">
48+
<element value="WP_CLI\Cron"/><!-- Namespaces. -->
49+
<element value="wpcli_cron"/><!-- Global variables and such. -->
50+
</property>
51+
</properties>
52+
</rule>
53+
54+
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
55+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
56+
<exclude-pattern>*/src/Cron_(Event_|Schedule_)?Command\.php$</exclude-pattern>
57+
</rule>
58+
59+
</ruleset>

src/Cron_Command.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@ protected static function get_cron_spawn() {
6969
$sslverify = \WP_CLI\Utils\wp_version_compare( 4.0, '<' );
7070
$doing_wp_cron = sprintf( '%.22F', microtime( true ) );
7171

72-
$cron_request = apply_filters( 'cron_request', array(
72+
$cron_request_array = array(
7373
'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
7474
'key' => $doing_wp_cron,
7575
'args' => array(
7676
'timeout' => 3,
7777
'blocking' => true,
78-
'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify )
79-
)
80-
) );
78+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
79+
'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ),
80+
),
81+
);
82+
83+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
84+
$cron_request = apply_filters( 'cron_request', $cron_request_array );
8185

8286
# Enforce a blocking request in case something that's hooked onto the 'cron_request' filter sets it to false
8387
$cron_request['args']['blocking'] = true;

src/Cron_Event_Command.php

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
35
/**
46
* Schedules, runs, and deletes WP-Cron events.
57
*
@@ -31,6 +33,7 @@ class Cron_Event_Command extends WP_CLI_Command {
3133
'next_run_relative',
3234
'recurrence',
3335
);
36+
3437
private static $time_format = 'Y-m-d H:i:s';
3538

3639
/**
@@ -112,7 +115,7 @@ public function list_( $args, $assoc_args ) {
112115
}
113116
}
114117

115-
if ( 'ids' == $formatter->format ) {
118+
if ( 'ids' === $formatter->format ) {
116119
echo implode( ' ', wp_list_pluck( $events, 'hook' ) );
117120
} else {
118121
$formatter->display_items( $events );
@@ -153,13 +156,13 @@ public function list_( $args, $assoc_args ) {
153156
*/
154157
public function schedule( $args, $assoc_args ) {
155158

156-
$hook = $args[0];
157-
$next_run = \WP_CLI\Utils\get_flag_value( $args, 1, 'now' );
158-
$recurrence = \WP_CLI\Utils\get_flag_value( $args, 2, false );
159+
$hook = $args[0];
160+
$next_run = Utils\get_flag_value( $args, 1, 'now' );
161+
$recurrence = Utils\get_flag_value( $args, 2, false );
159162

160163
if ( empty( $next_run ) ) {
161164
$timestamp = time();
162-
} else if ( is_numeric( $next_run ) ) {
165+
} elseif ( is_numeric( $next_run ) ) {
163166
$timestamp = absint( $next_run );
164167
} else {
165168
$timestamp = strtotime( $next_run );
@@ -173,7 +176,7 @@ public function schedule( $args, $assoc_args ) {
173176

174177
$schedules = wp_get_schedules();
175178

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

@@ -215,7 +218,7 @@ public function schedule( $args, $assoc_args ) {
215218
*/
216219
public function run( $args, $assoc_args ) {
217220

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

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

228231
$hooks = wp_list_pluck( $events, 'hook' );
229-
foreach( $args as $hook ) {
232+
foreach ( $args as $hook ) {
230233
if ( ! in_array( $hook, $hooks, true ) ) {
231234
WP_CLI::error( sprintf( "Invalid cron event '%s'", $hook ) );
232235
}
233236
}
234237

235-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'due-now' ) ) {
238+
if ( Utils\get_flag_value( $assoc_args, 'due-now' ) ) {
236239
$due_events = array();
237-
foreach( $events as $event ) {
240+
foreach ( $events as $event ) {
238241
if ( ! empty( $args ) && ! in_array( $event->hook, $args, true ) ) {
239242
continue;
240243
}
@@ -243,10 +246,10 @@ public function run( $args, $assoc_args ) {
243246
}
244247
}
245248
$events = $due_events;
246-
} else if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
249+
} elseif ( ! Utils\get_flag_value( $assoc_args, 'all' ) ) {
247250
$due_events = array();
248-
foreach( $events as $event ) {
249-
if ( in_array( $event->hook, $args ) ) {
251+
foreach ( $events as $event ) {
252+
if ( in_array( $event->hook, $args, true ) ) {
250253
$due_events[] = $event;
251254
}
252255
}
@@ -255,9 +258,9 @@ public function run( $args, $assoc_args ) {
255258

256259
$executed = 0;
257260
foreach ( $events as $event ) {
258-
$start = microtime( true );
261+
$start = microtime( true );
259262
$result = self::run_event( $event );
260-
$total = round( microtime( true ) - $start, 3 );
263+
$total = round( microtime( true ) - $start, 3 );
261264
$executed++;
262265
WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) );
263266
}
@@ -275,16 +278,18 @@ public function run( $args, $assoc_args ) {
275278
protected static function run_event( stdClass $event ) {
276279

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

281-
if ( $event->schedule != false ) {
285+
if ( false !== $event->schedule ) {
282286
$new_args = array( $event->time, $event->schedule, $event->hook, $event->args );
283287
call_user_func_array( 'wp_reschedule_event', $new_args );
284288
}
285289

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

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

290295
return true;
@@ -316,7 +321,7 @@ public function delete( $args, $assoc_args ) {
316321

317322
$deleted = 0;
318323
foreach ( $events as $event ) {
319-
if ( $event->hook == $hook ) {
324+
if ( $event->hook === $hook ) {
320325
$result = self::delete_event( $event );
321326
if ( $result ) {
322327
$deleted++;
@@ -327,7 +332,7 @@ public function delete( $args, $assoc_args ) {
327332
}
328333

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

347-
if ( ! isset( $crons[$event->time][$event->hook][$event->sig] ) ) {
352+
if ( ! isset( $crons[ $event->time ][ $event->hook ][ $event->sig ] ) ) {
348353
return false;
349354
}
350355

@@ -395,7 +400,7 @@ protected static function get_cron_events() {
395400
'sig' => $sig,
396401
'args' => $data['args'],
397402
'schedule' => $data['schedule'],
398-
'interval' => \WP_CLI\Utils\get_flag_value( $data, 'interval' ),
403+
'interval' => Utils\get_flag_value( $data, 'interval' ),
399404
);
400405

401406
}
@@ -425,13 +430,13 @@ private static function interval( $since ) {
425430

426431
// array of time period chunks
427432
$chunks = array(
428-
array( 60 * 60 * 24 * 365 , \_n_noop( '%s year', '%s years' ) ),
429-
array( 60 * 60 * 24 * 30 , \_n_noop( '%s month', '%s months' ) ),
430-
array( 60 * 60 * 24 * 7, \_n_noop( '%s week', '%s weeks' ) ),
431-
array( 60 * 60 * 24 , \_n_noop( '%s day', '%s days' ) ),
432-
array( 60 * 60 , \_n_noop( '%s hour', '%s hours' ) ),
433-
array( 60 , \_n_noop( '%s minute', '%s minutes' ) ),
434-
array( 1 , \_n_noop( '%s second', '%s seconds' ) ),
433+
array( 60 * 60 * 24 * 365, 'year' ),
434+
array( 60 * 60 * 24 * 30, 'month' ),
435+
array( 60 * 60 * 24 * 7, 'week' ),
436+
array( 60 * 60 * 24, 'day' ),
437+
array( 60 * 60, 'hour' ),
438+
array( 60, 'minute' ),
439+
array( 1, 'second' ),
435440
);
436441

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

442447
// step one: the first chunk
443448
for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) {
444-
$seconds = $chunks[$i][0];
445-
$name = $chunks[$i][1];
449+
$seconds = $chunks[ $i ][0];
450+
$name = $chunks[ $i ][1];
446451

447452
// finding the biggest chunk (if the chunk fits, break)
448-
if ( ( $count = floor( $since / $seconds ) ) != 0 ){
453+
$count = floor( $since / $seconds );
454+
if ( floatval( 0 ) !== $count ) {
449455
break;
450456
}
451457
}
452458

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

456462
// step two: the second chunk
457463
if ( $i + 1 < $j ) {
458-
$seconds2 = $chunks[$i + 1][0];
459-
$name2 = $chunks[$i + 1][1];
464+
$seconds2 = $chunks[ $i + 1 ][0];
465+
$name2 = $chunks[ $i + 1 ][1];
460466

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

src/Cron_Schedule_Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function list_( $args, $assoc_args ) {
7979

8080
$schedules = self::get_schedules();
8181

82-
if ( 'ids' == $formatter->format ) {
82+
if ( 'ids' === $formatter->format ) {
8383
echo implode( ' ', wp_list_pluck( $schedules, 'name' ) );
8484
} else {
8585
$formatter->display_items( $schedules );
@@ -106,7 +106,7 @@ protected static function format_schedule( array $schedule, $name ) {
106106
*/
107107
protected static function get_schedules() {
108108
$schedules = wp_get_schedules();
109-
if ( !empty( $schedules ) ) {
109+
if ( ! empty( $schedules ) ) {
110110
uasort( $schedules, 'Cron_Schedule_Command::sort' );
111111
$schedules = array_map( 'Cron_Schedule_Command::format_schedule', $schedules, array_keys( $schedules ) );
112112
}

0 commit comments

Comments
 (0)