Skip to content

Commit 35688c5

Browse files
authored
Merge pull request #33 from wp-cli/feature/use-phpcs
Implement CS checking based on the `WP_CLI_CS` ruleset
2 parents a5de1ea + aca6758 commit 35688c5

File tree

7 files changed

+186
-80
lines changed

7 files changed

+186
-80
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
@@ -15,7 +15,7 @@
1515
"wp-cli/wp-cli": "^2"
1616
},
1717
"require-dev": {
18-
"wp-cli/wp-cli-tests": "^2.0.7"
18+
"wp-cli/wp-cli-tests": "^2.1"
1919
},
2020
"config": {
2121
"process-timeout": 7200,

phpcs.xml.dist

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-role">
3+
<description>Custom ruleset for WP-CLI role-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\Role"/><!-- Namespaces. -->
49+
<element value="wpcli_role"/><!-- 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/(Role|Capabilities)_Command\.php$</exclude-pattern>
57+
</rule>
58+
</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: 52 additions & 33 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,12 @@
2123
*/
2224
class Capabilities_Command extends WP_CLI_Command {
2325

24-
private $fields = array(
25-
'name'
26-
);
26+
/**
27+
* List of available fields.
28+
*
29+
* @var array
30+
*/
31+
private $fields = [ 'name' ];
2732

2833
/**
2934
* Lists capabilities for a given role.
@@ -45,7 +50,7 @@ class Capabilities_Command extends WP_CLI_Command {
4550
* - count
4651
* - yaml
4752
* ---
48-
*
53+
*
4954
* [--show-grant]
5055
* : Display all capabilities defined for a role including grant.
5156
* ---
@@ -66,39 +71,36 @@ class Capabilities_Command extends WP_CLI_Command {
6671
*/
6772
public function list_( $args, $assoc_args ) {
6873
$role_obj = self::get_role( $args[0] );
69-
74+
7075
$show_grant = ! empty( $assoc_args['show-grant'] );
7176

7277
if ( $show_grant ) {
7378
array_push( $this->fields, 'grant' );
7479
$capabilities = $role_obj->capabilities;
75-
}
76-
else {
80+
} else {
7781
$capabilities = array_filter( $role_obj->capabilities );
7882
}
79-
83+
8084
$output_caps = array();
8185
foreach ( $capabilities as $cap => $grant ) {
82-
$output_cap = new StdClass;
86+
$output_cap = new stdClass();
87+
88+
$output_cap->name = $cap;
89+
$output_cap->grant = $grant ? 'true' : 'false';
8390

84-
$output_cap->name = $cap;
85-
$output_cap->grant = ( $grant ) ? 'true' : 'false';
86-
8791
$output_caps[] = $output_cap;
8892
}
8993

9094
if ( 'list' === $assoc_args['format'] ) {
9195
foreach ( $output_caps as $cap ) {
9296
if ( $show_grant ) {
9397
WP_CLI::line( implode( ',', array( $cap->name, $cap->grant ) ) );
94-
}
95-
else {
98+
} else {
9699
WP_CLI::line( $cap->name );
97100
}
98101
}
99-
}
100-
else {
101-
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields );
102+
} else {
103+
$formatter = new Formatter( $assoc_args, $this->fields );
102104
$formatter->display_items( $output_caps );
103105
}
104106
}
@@ -113,7 +115,7 @@ public function list_( $args, $assoc_args ) {
113115
*
114116
* <cap>...
115117
* : One or more capabilities to add.
116-
*
118+
*
117119
* [--grant]
118120
* : Adds the capability as an explicit boolean value, instead of implicitly defaulting to `true`.
119121
* ---
@@ -141,23 +143,23 @@ public function add( $args, $assoc_args ) {
141143
$count = 0;
142144

143145
foreach ( $args as $cap ) {
144-
if ( true === $grant && $role_obj->has_cap( $cap ) )
146+
if ( true === $grant && $role_obj->has_cap( $cap ) ) {
145147
continue;
148+
}
146149

147-
if ( false === $grant && isset( $role_obj->capabilities[ $cap ] ) && false === $role_obj->capabilities[ $cap ] )
150+
if ( false === $grant && isset( $role_obj->capabilities[ $cap ] ) && false === $role_obj->capabilities[ $cap ] ) {
148151
continue;
152+
}
149153

150154
$role_obj->add_cap( $cap, $grant );
151155

152156
$count++;
153157
}
154158

155-
if ( $grant ) {
156-
$message = ( 1 === $count ) ? "Added %d capability to '%s' role." : "Added %d capabilities to '%s' role.";
157-
} else {
158-
$message = ( 1 === $count ) ? "Added %d capability to '%s' role as false." : "Added %d capabilities to '%s' role as false.";
159-
}
160-
WP_CLI::success( sprintf( $message, $count, $role ) );
159+
$capability = WP_CLI\Utils\pluralize( 'capability', $count );
160+
$grant_qualification = $grant ? '' : ' as false';
161+
162+
WP_CLI::success( "Added {$count} {$capability} to '{$role}' role{$grant_qualification}." );
161163
}
162164

163165
/**
@@ -187,33 +189,50 @@ public function remove( $args ) {
187189
$count = 0;
188190

189191
foreach ( $args as $cap ) {
190-
if ( ! isset( $role_obj->capabilities[ $cap ] ) )
192+
if ( ! isset( $role_obj->capabilities[ $cap ] ) ) {
191193
continue;
194+
}
192195

193196
$role_obj->remove_cap( $cap );
194197

195198
$count++;
196199
}
197200

198-
$message = ( 1 === $count ) ? "Removed %d capability from '%s' role." : "Removed %d capabilities from '%s' role.";
199-
WP_CLI::success( sprintf( $message, $count, $role ) );
201+
$capability = WP_CLI\Utils\pluralize( 'capability', $count );
202+
203+
WP_CLI::success( "Removed {$count} {$capability} from '{$role}' role." );
200204
}
201205

206+
/**
207+
* Retrieve a specific role from the system.
208+
*
209+
* @param string $role Role to retrieve.
210+
* @return WP_Role Requested role.
211+
* @throws \WP_CLI\ExitException If the role could not be found.
212+
*/
202213
private static function get_role( $role ) {
203214
global $wp_roles;
204215

205216
$role_obj = $wp_roles->get_role( $role );
206217

207-
if ( !$role_obj )
208-
WP_CLI::error( "'$role' role not found." );
218+
if ( ! $role_obj ) {
219+
WP_CLI::error( "'{$role}' role not found." );
220+
}
209221

210222
return $role_obj;
211223
}
212224

225+
/**
226+
* Assert that the roles are persisted to the database.
227+
*
228+
* @throws \WP_CLI\ExitException If the roles are not persisted to the
229+
* database.
230+
*/
213231
private static function persistence_check() {
214232
global $wp_roles;
215233

216-
if ( !$wp_roles->use_db )
217-
WP_CLI::error( "Role definitions are not persistent." );
234+
if ( ! $wp_roles->use_db ) {
235+
WP_CLI::error( 'Role definitions are not persistent.' );
236+
}
218237
}
219238
}

0 commit comments

Comments
 (0)