Skip to content

Commit 9cb4c1e

Browse files
committed
PHPCS: Update ruleset to exclude calsses from NamingConventions sniff
Fix CS in role-command.php, src/Capabilities_Command.php, src/Role_Command.php
1 parent 5ffe52c commit 9cb4c1e

File tree

4 files changed

+81
-66
lines changed

4 files changed

+81
-66
lines changed

phpcs.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@
5151
</properties>
5252
</rule>
5353

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/(Role|Capabilities)_Command\.php$</exclude-pattern>
57+
</rule>
5458
</ruleset>

role-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_role_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
8+
if ( file_exists( $wpcli_role_autoloader ) ) {
9+
require_once $wpcli_role_autoloader;
1010
}
1111

1212
WP_CLI::add_command( 'cap', 'Capabilities_Command' );

src/Capabilities_Command.php

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

3+
use WP_CLI\Formatter;
4+
35
/**
46
* Adds, removes, and lists capabilities of a user role.
57
*
@@ -21,9 +23,7 @@
2123
*/
2224
class Capabilities_Command extends WP_CLI_Command {
2325

24-
private $fields = array(
25-
'name'
26-
);
26+
private $fields = [ 'name' ];
2727

2828
/**
2929
* Lists capabilities for a given role.
@@ -45,7 +45,7 @@ class Capabilities_Command extends WP_CLI_Command {
4545
* - count
4646
* - yaml
4747
* ---
48-
*
48+
*
4949
* [--show-grant]
5050
* : Display all capabilities defined for a role including grant.
5151
* ---
@@ -66,39 +66,36 @@ class Capabilities_Command extends WP_CLI_Command {
6666
*/
6767
public function list_( $args, $assoc_args ) {
6868
$role_obj = self::get_role( $args[0] );
69-
69+
7070
$show_grant = ! empty( $assoc_args['show-grant'] );
7171

7272
if ( $show_grant ) {
7373
array_push( $this->fields, 'grant' );
7474
$capabilities = $role_obj->capabilities;
75-
}
76-
else {
75+
} else {
7776
$capabilities = array_filter( $role_obj->capabilities );
7877
}
79-
78+
8079
$output_caps = array();
8180
foreach ( $capabilities as $cap => $grant ) {
82-
$output_cap = new StdClass;
81+
$output_cap = new stdClass();
8382

84-
$output_cap->name = $cap;
83+
$output_cap->name = $cap;
8584
$output_cap->grant = ( $grant ) ? 'true' : 'false';
86-
85+
8786
$output_caps[] = $output_cap;
8887
}
8988

9089
if ( 'list' === $assoc_args['format'] ) {
9190
foreach ( $output_caps as $cap ) {
9291
if ( $show_grant ) {
9392
WP_CLI::line( implode( ',', array( $cap->name, $cap->grant ) ) );
94-
}
95-
else {
93+
} else {
9694
WP_CLI::line( $cap->name );
9795
}
9896
}
99-
}
100-
else {
101-
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields );
97+
} else {
98+
$formatter = new Formatter( $assoc_args, $this->fields );
10299
$formatter->display_items( $output_caps );
103100
}
104101
}
@@ -113,7 +110,7 @@ public function list_( $args, $assoc_args ) {
113110
*
114111
* <cap>...
115112
* : One or more capabilities to add.
116-
*
113+
*
117114
* [--grant]
118115
* : Adds the capability as an explicit boolean value, instead of implicitly defaulting to `true`.
119116
* ---
@@ -141,11 +138,13 @@ public function add( $args, $assoc_args ) {
141138
$count = 0;
142139

143140
foreach ( $args as $cap ) {
144-
if ( true === $grant && $role_obj->has_cap( $cap ) )
141+
if ( true === $grant && $role_obj->has_cap( $cap ) ) {
145142
continue;
143+
}
146144

147-
if ( false === $grant && isset( $role_obj->capabilities[ $cap ] ) && false === $role_obj->capabilities[ $cap ] )
145+
if ( false === $grant && isset( $role_obj->capabilities[ $cap ] ) && false === $role_obj->capabilities[ $cap ] ) {
148146
continue;
147+
}
149148

150149
$role_obj->add_cap( $cap, $grant );
151150

@@ -187,8 +186,9 @@ public function remove( $args ) {
187186
$count = 0;
188187

189188
foreach ( $args as $cap ) {
190-
if ( ! isset( $role_obj->capabilities[ $cap ] ) )
189+
if ( ! isset( $role_obj->capabilities[ $cap ] ) ) {
191190
continue;
191+
}
192192

193193
$role_obj->remove_cap( $cap );
194194

@@ -204,16 +204,18 @@ private static function get_role( $role ) {
204204

205205
$role_obj = $wp_roles->get_role( $role );
206206

207-
if ( !$role_obj )
208-
WP_CLI::error( "'$role' role not found." );
207+
if ( ! $role_obj ) {
208+
WP_CLI::error( "'{$role}' role not found." );
209+
}
209210

210211
return $role_obj;
211212
}
212213

213214
private static function persistence_check() {
214215
global $wp_roles;
215216

216-
if ( !$wp_roles->use_db )
217-
WP_CLI::error( "Role definitions are not persistent." );
217+
if ( ! $wp_roles->use_db ) {
218+
WP_CLI::error( 'Role definitions are not persistent.' );
219+
}
218220
}
219221
}

src/Role_Command.php

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
use WP_CLI\Formatter;
5+
36
/**
47
* Manages user roles, including creating new roles and resetting to defaults.
58
*
@@ -36,10 +39,10 @@
3639
*/
3740
class Role_Command extends WP_CLI_Command {
3841

39-
private $fields = array(
40-
'name',
41-
'role'
42-
);
42+
private $fields = [ 'name', 'role' ];
43+
44+
// Array of default roles.
45+
private $roles = [ 'administrator', 'editor', 'author', 'contributor', 'subscriber' ];
4346

4447
/**
4548
* Lists all roles.
@@ -91,15 +94,15 @@ public function list_( $args, $assoc_args ) {
9194

9295
$output_roles = array();
9396
foreach ( $wp_roles->roles as $key => $role ) {
94-
$output_role = new stdClass;
97+
$output_role = new stdClass();
9598

9699
$output_role->name = $role['name'];
97100
$output_role->role = $key;
98101

99102
$output_roles[] = $output_role;
100103
}
101104

102-
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields );
105+
$formatter = new Formatter( $assoc_args, $this->fields );
103106
$formatter->display_items( $output_roles );
104107
}
105108

@@ -122,11 +125,11 @@ public function list_( $args, $assoc_args ) {
122125
public function exists( $args ) {
123126
global $wp_roles;
124127

125-
if ( ! in_array($args[0], array_keys( $wp_roles->roles ) ) ) {
126-
WP_CLI::error( "Role with ID '$args[0]' does not exist." );
128+
if ( ! in_array( $args[0], array_keys( $wp_roles->roles ), true ) ) {
129+
WP_CLI::error( "Role with ID '{$args[0]}' does not exist." );
127130
}
128131

129-
WP_CLI::success( "Role with ID '$args[0]' exists." );
132+
WP_CLI::success( "Role with ID '{$args[0]}' exists." );
130133
}
131134

132135
/**
@@ -158,11 +161,11 @@ public function create( $args, $assoc_args ) {
158161

159162
self::persistence_check();
160163

161-
$role_key = array_shift( $args );
164+
$role_key = array_shift( $args );
162165
$role_name = array_shift( $args );
163166

164167
if ( empty( $role_key ) || empty( $role_name ) ) {
165-
WP_CLI::error( "Can't create role, insufficient information provided.");
168+
WP_CLI::error( "Can't create role, insufficient information provided." );
166169
}
167170

168171
$capabilities = false;
@@ -177,7 +180,7 @@ public function create( $args, $assoc_args ) {
177180
if ( add_role( $role_key, $role_name ) ) {
178181
if ( ! empty( $capabilities ) ) {
179182
$role_obj = $wp_roles->get_role( $role_key );
180-
foreach( $capabilities as $cap ) {
183+
foreach ( $capabilities as $cap ) {
181184
$role_obj->add_cap( $cap );
182185
}
183186
WP_CLI::success( sprintf( "Role with key '%s' created. Cloned capabilities from '%s'.", $role_key, $assoc_args['clone'] ) );
@@ -214,17 +217,19 @@ public function delete( $args ) {
214217

215218
$role_key = array_shift( $args );
216219

217-
if ( empty( $role_key ) || ! isset( $wp_roles->roles[$role_key] ) )
218-
WP_CLI::error( "Role key not provided, or is invalid." );
220+
if ( empty( $role_key ) || ! isset( $wp_roles->roles[ $role_key ] ) ) {
221+
WP_CLI::error( 'Role key not provided, or is invalid.' );
222+
}
219223

220224
remove_role( $role_key );
221225

222226
// Note: remove_role() doesn't indicate success or otherwise, so we have to
223227
// check ourselves
224-
if ( ! isset( $wp_roles->roles[$role_key] ) )
228+
if ( ! isset( $wp_roles->roles[ $role_key ] ) ) {
225229
WP_CLI::success( sprintf( "Role with key '%s' deleted.", $role_key ) );
226-
else
230+
} else {
227231
WP_CLI::error( sprintf( "Role with key '%s' could not be deleted.", $role_key ) );
232+
}
228233

229234
}
230235

@@ -253,26 +258,28 @@ public function reset( $args, $assoc_args ) {
253258

254259
self::persistence_check();
255260

256-
if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) )
257-
WP_CLI::error( "Role key not provided, or is invalid." );
261+
if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
262+
WP_CLI::error( 'Role key not provided, or is invalid.' );
263+
}
258264

259265
if ( ! function_exists( 'populate_roles' ) ) {
260-
require_once( ABSPATH.'wp-admin/includes/schema.php' );
266+
require_once ABSPATH . 'wp-admin/includes/schema.php';
261267
}
262268

263269
global $wp_roles;
264-
$all_roles = array_keys( $wp_roles->roles );
270+
$all_roles = array_keys( $wp_roles->roles );
265271
$preserve_args = $args;
266272

267273
// Get our default roles.
268-
$default_roles = $preserve = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
269-
$before = array();
274+
$default_roles = $this->roles;
275+
$preserve = $this->roles;
276+
$before = [];
270277

271-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
272-
foreach( $default_roles as $role ) {
278+
if ( Utils\get_flag_value( $assoc_args, 'all' ) ) {
279+
foreach ( $default_roles as $role ) {
273280
$before[ $role ] = get_role( $role );
274281
remove_role( $role );
275-
$args[]= $role;
282+
$args[] = $role;
276283
}
277284
populate_roles();
278285
$not_affected_roles = array_diff( $all_roles, $default_roles );
@@ -284,7 +291,7 @@ public function reset( $args, $assoc_args ) {
284291
} else {
285292

286293
foreach ( $args as $k => $role_key ) {
287-
$key = array_search( $role_key, $default_roles );
294+
$key = array_search( $role_key, $default_roles, true );
288295
if ( false !== $key ) {
289296
unset( $preserve[ $key ] );
290297
$before[ $role_key ] = get_role( $role_key );
@@ -302,7 +309,7 @@ public function reset( $args, $assoc_args ) {
302309
}
303310

304311
// No roles were unset, bail.
305-
if ( count( $default_roles ) == count( $preserve ) ) {
312+
if ( count( $default_roles ) === count( $preserve ) ) {
306313
WP_CLI::error( 'Must specify a default role to reset.' );
307314
}
308315

@@ -312,7 +319,7 @@ public function reset( $args, $assoc_args ) {
312319
* if get_role is null
313320
* save role name for re-removal
314321
*/
315-
$roleobj = get_role( $role );
322+
$roleobj = get_role( $role );
316323
$preserve[ $k ] = is_null( $roleobj ) ? $role : $roleobj;
317324

318325
remove_role( $role );
@@ -334,21 +341,22 @@ public function reset( $args, $assoc_args ) {
334341
}
335342
}
336343

337-
$num_reset = 0;
338-
$args = array_unique( $args );
344+
$num_reset = 0;
345+
$args = array_unique( $args );
339346
$num_to_reset = count( $args );
340-
foreach( $args as $role_key ) {
347+
foreach ( $args as $role_key ) {
341348
$after[ $role_key ] = get_role( $role_key );
342349

350+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Object instances won't be same, strict check will fail here.
343351
if ( $after[ $role_key ] != $before[ $role_key ] ) {
344352
++$num_reset;
345-
$restored_cap = array_diff_key( $after[ $role_key ]->capabilities, $before[ $role_key ]->capabilities );
346-
$removed_cap = array_diff_key( $before[ $role_key ]->capabilities, $after[ $role_key ]->capabilities );
353+
$restored_cap = array_diff_key( $after[ $role_key ]->capabilities, $before[ $role_key ]->capabilities );
354+
$removed_cap = array_diff_key( $before[ $role_key ]->capabilities, $after[ $role_key ]->capabilities );
347355
$restored_cap_count = count( $restored_cap );
348-
$removed_cap_count = count( $removed_cap );
349-
$restored_text = ( 1 === $restored_cap_count ) ? '%d capability' : '%d capabilities';
350-
$removed_text = ( 1 === $removed_cap_count ) ? '%d capability' : '%d capabilities';
351-
$message = "Restored ". $restored_text . " to and removed " . $removed_text . " from '%s' role.";
356+
$removed_cap_count = count( $removed_cap );
357+
$restored_text = ( 1 === $restored_cap_count ) ? '%d capability' : '%d capabilities';
358+
$removed_text = ( 1 === $removed_cap_count ) ? '%d capability' : '%d capabilities';
359+
$message = "Restored {$restored_text} to and removed {$removed_text} from '%s' role.";
352360
WP_CLI::log( sprintf( $message, $restored_cap_count, $removed_cap_count, $role_key ) );
353361
} else {
354362
WP_CLI::log( "No changes necessary for '{$role_key}' role." );
@@ -372,7 +380,8 @@ public function reset( $args, $assoc_args ) {
372380
private static function persistence_check() {
373381
global $wp_roles;
374382

375-
if ( !$wp_roles->use_db )
376-
WP_CLI::error( "Role definitions are not persistent." );
383+
if ( ! $wp_roles->use_db ) {
384+
WP_CLI::error( 'Role definitions are not persistent.' );
385+
}
377386
}
378387
}

0 commit comments

Comments
 (0)