Skip to content

Commit c326dba

Browse files
authored
Merge pull request #34 from pattonwebz/feature/apply-phpcs-wpclics-rules
Apply PHPCS `WP_CLI_CS` rules
2 parents 7fbe45a + 0575301 commit c326dba

File tree

6 files changed

+132
-50
lines changed

6 files changed

+132
-50
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.7"
19+
"wp-cli/wp-cli-tests": "^2.1"
2020
},
2121
"config": {
2222
"process-timeout": 7200,

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-rewrite">
3+
<description>Custom ruleset for WP-CLI rewrite-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\Rewrite"/><!-- Namespaces. -->
49+
<element value="wpcli_rewrite"/><!-- 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/Rewrite_Command\.php$</exclude-pattern>
57+
</rule>
58+
59+
</ruleset>

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

1212
WP_CLI::add_command( 'rewrite', 'Rewrite_Command' );

src/Rewrite_Command.php

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

3+
use WP_CLI\Utils;
4+
35
/**
46
* Lists or flushes the site's rewrite rules, updates the permalink structure.
57
*
@@ -27,6 +29,7 @@
2729
*
2830
* @package wp-cli
2931
*/
32+
3033
class Rewrite_Command extends WP_CLI_Command {
3134

3235
/**
@@ -56,17 +59,17 @@ public function flush( $args, $assoc_args ) {
5659
// make sure we detect mod_rewrite if configured in apache_modules in config
5760
self::apache_modules();
5861

59-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'hard' ) && ! in_array( 'mod_rewrite', (array) WP_CLI::get_config( 'apache_modules' ) ) ) {
60-
WP_CLI::warning( "Regenerating a .htaccess file requires special configuration. See usage docs." );
62+
if ( Utils\get_flag_value( $assoc_args, 'hard' ) && ! in_array( 'mod_rewrite', (array) WP_CLI::get_config( 'apache_modules' ), true ) ) {
63+
WP_CLI::warning( 'Regenerating a .htaccess file requires special configuration. See usage docs.' );
6164
}
6265

63-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'hard' ) && is_multisite() ) {
66+
if ( Utils\get_flag_value( $assoc_args, 'hard' ) && is_multisite() ) {
6467
WP_CLI::warning( "WordPress can't generate .htaccess file for a multisite install." );
6568
}
6669

6770
self::check_skip_plugins_themes();
6871

69-
flush_rewrite_rules( \WP_CLI\Utils\get_flag_value( $assoc_args, 'hard' ) );
72+
flush_rewrite_rules( Utils\get_flag_value( $assoc_args, 'hard' ) );
7073

7174
if ( ! get_option( 'rewrite_rules' ) ) {
7275
WP_CLI::warning( "Rewrite rules are empty, possibly because of a missing permalink_structure option. Use 'wp rewrite list' to verify, or 'wp rewrite structure' to update permalink_structure." );
@@ -113,52 +116,57 @@ public function structure( $args, $assoc_args ) {
113116

114117
// copypasta from /wp-admin/options-permalink.php
115118

116-
$prefix = $blog_prefix = '';
117-
if ( is_multisite() && !is_subdomain_install() && is_main_site() )
119+
$blog_prefix = '';
120+
$prefix = $blog_prefix;
121+
if ( is_multisite() && ! is_subdomain_install() && is_main_site() ) {
118122
$blog_prefix = '/blog';
123+
}
119124

120-
$permalink_structure = ( $args[0] == 'default' ) ? '' : $args[0];
125+
$permalink_structure = ( 'default' === $args[0] ) ? '' : $args[0];
121126

122127
if ( ! empty( $permalink_structure ) ) {
123128
$permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
124-
if ( $prefix && $blog_prefix )
129+
if ( $prefix && $blog_prefix ) {
125130
$permalink_structure = $prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
126-
else
131+
} else {
127132
$permalink_structure = $blog_prefix . $permalink_structure;
133+
}
128134
}
129135
$wp_rewrite->set_permalink_structure( $permalink_structure );
130136

131137
// Update category or tag bases
132138
if ( isset( $assoc_args['category-base'] ) ) {
133139

134140
$category_base = $assoc_args['category-base'];
135-
if ( ! empty( $category_base ) )
136-
$category_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
141+
if ( ! empty( $category_base ) ) {
142+
$category_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
143+
}
137144
$wp_rewrite->set_category_base( $category_base );
138145
}
139146

140147
if ( isset( $assoc_args['tag-base'] ) ) {
141148

142149
$tag_base = $assoc_args['tag-base'];
143-
if ( ! empty( $tag_base ) )
144-
$tag_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
150+
if ( ! empty( $tag_base ) ) {
151+
$tag_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
152+
}
145153
$wp_rewrite->set_tag_base( $tag_base );
146154
}
147155

148156
// make sure we detect mod_rewrite if configured in apache_modules in config
149157
self::apache_modules();
150158

151-
WP_CLI::success( "Rewrite structure set." );
159+
WP_CLI::success( 'Rewrite structure set.' );
152160

153161
// Launch a new process to flush rewrites because core expects flush
154162
// to happen after rewrites are set
155-
$new_assoc_args = array();
156-
$cmd = 'rewrite flush';
157-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'hard' ) ) {
158-
$cmd .= ' --hard';
163+
$new_assoc_args = [];
164+
$cmd = 'rewrite flush';
165+
if ( Utils\get_flag_value( $assoc_args, 'hard' ) ) {
166+
$cmd .= ' --hard';
159167
$new_assoc_args['hard'] = true;
160-
if ( ! in_array( 'mod_rewrite', (array) WP_CLI::get_config( 'apache_modules' ) ) ) {
161-
WP_CLI::warning( "Regenerating a .htaccess file requires special configuration. See usage docs." );
168+
if ( ! in_array( 'mod_rewrite', (array) WP_CLI::get_config( 'apache_modules' ), true ) ) {
169+
WP_CLI::warning( 'Regenerating a .htaccess file requires special configuration. See usage docs.' );
162170
}
163171
}
164172

@@ -212,66 +220,75 @@ public function list_( $args, $assoc_args ) {
212220

213221
$rules = get_option( 'rewrite_rules' );
214222
if ( ! $rules ) {
215-
$rules = array();
223+
$rules = [];
216224
WP_CLI::warning( 'No rewrite rules.' );
217225
}
218226

219227
self::check_skip_plugins_themes();
220228

221-
$defaults = array(
229+
$defaults = [
222230
'source' => '',
223231
'match' => '',
224232
'format' => 'table',
225233
'fields' => 'match,query,source',
226-
);
234+
];
227235
$assoc_args = array_merge( $defaults, $assoc_args );
228236

229-
$rewrite_rules_by_source = array();
230-
$rewrite_rules_by_source['post'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->permalink_structure, EP_PERMALINK );
231-
$rewrite_rules_by_source['date'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_date_permastruct(), EP_DATE );
232-
$rewrite_rules_by_source['root'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->root . '/', EP_ROOT );
237+
$rewrite_rules_by_source = [];
238+
$rewrite_rules_by_source['post'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->permalink_structure, EP_PERMALINK );
239+
$rewrite_rules_by_source['date'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_date_permastruct(), EP_DATE );
240+
$rewrite_rules_by_source['root'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->root . '/', EP_ROOT );
233241
$rewrite_rules_by_source['comments'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->root . $wp_rewrite->comments_base, EP_COMMENTS, true, true, true, false );
234-
$rewrite_rules_by_source['search'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_search_permastruct(), EP_SEARCH );
235-
$rewrite_rules_by_source['author'] = $wp_rewrite->generate_rewrite_rules($wp_rewrite->get_author_permastruct(), EP_AUTHORS );
236-
$rewrite_rules_by_source['page'] = $wp_rewrite->page_rewrite_rules();
242+
$rewrite_rules_by_source['search'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_search_permastruct(), EP_SEARCH );
243+
$rewrite_rules_by_source['author'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_author_permastruct(), EP_AUTHORS );
244+
$rewrite_rules_by_source['page'] = $wp_rewrite->page_rewrite_rules();
237245

238246
// Extra permastructs including tags, categories, etc.
239247
foreach ( $wp_rewrite->extra_permastructs as $permastructname => $permastruct ) {
240248
if ( is_array( $permastruct ) ) {
241-
$rewrite_rules_by_source[$permastructname] = $wp_rewrite->generate_rewrite_rules( $permastruct['struct'], $permastruct['ep_mask'], $permastruct['paged'], $permastruct['feed'], $permastruct['forcomments'], $permastruct['walk_dirs'], $permastruct['endpoints'] );
249+
$rewrite_rules_by_source[ $permastructname ] = $wp_rewrite->generate_rewrite_rules( $permastruct['struct'], $permastruct['ep_mask'], $permastruct['paged'], $permastruct['feed'], $permastruct['forcomments'], $permastruct['walk_dirs'], $permastruct['endpoints'] );
242250
} else {
243-
$rewrite_rules_by_source[$permastructname] = $wp_rewrite->generate_rewrite_rules( $permastruct, EP_NONE );
251+
$rewrite_rules_by_source[ $permastructname ] = $wp_rewrite->generate_rewrite_rules( $permastruct, EP_NONE );
244252
}
245253
}
246254

247255
// Apply the filters used in core just in case
248-
foreach( $rewrite_rules_by_source as $source => $source_rules ) {
249-
$rewrite_rules_by_source[$source] = apply_filters( $source . '_rewrite_rules', $source_rules );
250-
if ( 'post_tag' == $source )
251-
$rewrite_rules_by_source[$source] = apply_filters( 'tag_rewrite_rules', $source_rules );
256+
foreach ( $rewrite_rules_by_source as $source => $source_rules ) {
257+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Can't prefix dynamic hooks here, calling hooks for custom permastructs.
258+
$rewrite_rules_by_source[ $source ] = apply_filters( $source . '_rewrite_rules', $source_rules );
259+
if ( 'post_tag' === $source ) {
260+
if ( Utils\wp_version_compare( '3.1.0', '>=' ) ) {
261+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
262+
$rewrite_rules_by_source[ $source ] = apply_filters( 'post_tag_rewrite_rules', $source_rules );
263+
} else {
264+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
265+
$rewrite_rules_by_source[ $source ] = apply_filters( 'tag_rewrite_rules', $source_rules );
266+
}
267+
}
252268
}
253269

254-
$rule_list = array();
270+
$rule_list = [];
255271
foreach ( $rules as $match => $query ) {
256272

257-
if ( ! empty( $assoc_args['match'] )
258-
&& ! preg_match( "!^$match!", trim( $assoc_args['match'], '/' ) ) )
273+
if ( ! empty( $assoc_args['match'] ) && ! preg_match( "!^$match!", trim( $assoc_args['match'], '/' ) ) ) {
259274
continue;
275+
}
260276

261277
$source = 'other';
262-
foreach( $rewrite_rules_by_source as $rules_source => $source_rules ) {
278+
foreach ( $rewrite_rules_by_source as $rules_source => $source_rules ) {
263279
if ( array_key_exists( $match, $source_rules ) ) {
264280
$source = $rules_source;
265281
}
266282
}
267283

268-
if ( ! empty( $assoc_args['source'] ) && $source != $assoc_args['source'] )
284+
if ( ! empty( $assoc_args['source'] ) && $source !== $assoc_args['source'] ) {
269285
continue;
286+
}
270287

271288
$rule_list[] = compact( 'match', 'query', 'source' );
272289
}
273290

274-
WP_CLI\Utils\format_items( $assoc_args['format'], $rule_list, explode( ',', $assoc_args['fields'] ) );
291+
Utils\format_items( $assoc_args['format'], $rule_list, explode( ',', $assoc_args['fields'] ) );
275292
}
276293

277294
/**
@@ -281,7 +298,7 @@ public function list_( $args, $assoc_args ) {
281298
* apache_get_modules and also sets the $is_apache global variable.
282299
*
283300
* This is so that flush_rewrite_rules will actually write out the
284-
* .htaccess file for apache wordpress installations. There is a check
301+
* .htaccess file for apache WordPress installations. There is a check
285302
* to see:
286303
*
287304
* 1. if the $is_apache variable is set.
@@ -300,9 +317,10 @@ public function list_( $args, $assoc_args ) {
300317
* to disk.
301318
*/
302319
private static function apache_modules() {
303-
$mods = WP_CLI::get_config('apache_modules');
304-
if ( !empty( $mods ) && !function_exists( 'apache_get_modules' ) ) {
320+
$mods = WP_CLI::get_config( 'apache_modules' );
321+
if ( ! empty( $mods ) && ! function_exists( 'apache_get_modules' ) ) {
305322
global $is_apache;
323+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
306324
$is_apache = true;
307325

308326
// needed for get_home_path() and .htaccess location
@@ -321,7 +339,7 @@ function apache_get_modules() {
321339
* are unregistered, which may cause erroneous behavior.
322340
*/
323341
private static function check_skip_plugins_themes() {
324-
$skipped = array();
342+
$skipped = [];
325343
if ( WP_CLI::get_config( 'skip-plugins' ) ) {
326344
$skipped[] = 'plugins';
327345
}

0 commit comments

Comments
 (0)