Skip to content

Commit 941acde

Browse files
authored
Merge pull request #58 from wp-cli/feature/use-phpcs
Implement CS checking based on the `WP_CLI_CS` ruleset
2 parents e434a40 + 01bddd8 commit 941acde

19 files changed

Lines changed: 392 additions & 266 deletions

.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
@@ -20,7 +20,7 @@
2020
"wp-cli/entity-command": "^1.3 || ^2",
2121
"wp-cli/extension-command": "^1.2 || ^2",
2222
"wp-cli/import-command": "^1 || ^2",
23-
"wp-cli/wp-cli-tests": "^2.0.7"
23+
"wp-cli/wp-cli-tests": "^2.1"
2424
},
2525
"config": {
2626
"process-timeout": 7200,

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

1212
WP_CLI::add_command( 'export', 'Export_Command' );

features/export.feature

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,31 +454,32 @@ Feature: Export content.
454454

455455
Scenario: Export posts using --max_num_posts
456456
Given a WP install
457+
And I run `wp site empty --yes`
457458
And a count-instances.php file:
458459
"""
459460
<?php
460-
echo preg_match_all( '#<wp:post_type>' . $args[0] . '<\/wp:post_type>#', file_get_contents( 'php://stdin' ), $matches );
461+
echo 'count=' . preg_match_all( '#<wp:post_type>' . $args[0] . '<\/wp:post_type>#', file_get_contents( 'php://stdin' ), $matches );
461462
"""
462463

463464
When I run `wp post generate --post_type=post --count=10`
464465
And I run `wp export --post_type=post --max_num_posts=1 --stdout | wp --skip-wordpress eval-file count-instances.php post`
465-
Then STDOUT should be:
466+
Then STDOUT should contain:
466467
"""
467-
1
468+
count=1
468469
"""
469470

470471
When I run `wp post generate --post_type=post --count=10`
471472
And I run `wp post generate --post_type=attachment --count=10`
472473
And I run `wp export --max_num_posts=1 --stdout | wp --skip-wordpress eval-file count-instances.php "(post|attachment)"`
473-
Then STDOUT should be:
474+
Then STDOUT should contain:
474475
"""
475-
1
476+
count=1
476477
"""
477478

478479
When I run `wp export --max_num_posts=5 --stdout | wp --skip-wordpress eval-file count-instances.php "(post|attachment)"`
479-
Then STDOUT should be:
480+
Then STDOUT should contain:
480481
"""
481-
5
482+
count=5
482483
"""
483484

484485
Scenario: Export a site with a custom filename format

functions.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
11
<?php
22

3+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
34
function wp_export( $args = array() ) {
4-
$defaults = array(
5-
'filters' => array(),
6-
'format' => 'WP_Export_WXR_Formatter',
7-
'writer' => 'WP_Export_Returner',
5+
$defaults = array(
6+
'filters' => array(),
7+
'format' => 'WP_Export_WXR_Formatter',
8+
'writer' => 'WP_Export_Returner',
89
'writer_args' => null,
910
);
10-
$args = wp_parse_args( $args, $defaults );
11+
$args = wp_parse_args( $args, $defaults );
1112
$export_query = new WP_Export_Query( $args['filters'] );
12-
$formatter = new $args['format']( $export_query );
13-
$writer = new $args['writer']( $formatter, $args['writer_args'] );
13+
$formatter = new $args['format']( $export_query );
14+
$writer = new $args['writer']( $formatter, $args['writer_args'] );
1415
try {
1516
return $writer->export();
1617
} catch ( WP_Export_Exception $e ) {
1718
return new WP_Error( 'wp-export-error', $e->getMessage() );
1819
}
1920
}
2021

21-
function wp_export_new_style_args_from_old_style_args( $args ) {
22-
if ( isset( $args['content'] ) ) {
23-
if ( 'all' == $args['content'] ) {
24-
unset( $args['content'] );
25-
} else {
26-
$args['post_type'] = $args['content'];
27-
}
28-
}
29-
return $args;
30-
}
31-
32-
// TEMPORARY
22+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound,WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid -- Renaming breaks Phar compat.
3323
function _wp_export_build_IN_condition( $column_name, $values, $format = '%s' ) {
3424
global $wpdb;
3525

36-
if ( !is_array( $values ) || empty( $values ) ) {
26+
if ( ! is_array( $values ) || empty( $values ) ) {
3727
return '';
3828
}
3929
$formats = implode( ', ', array_fill( 0, count( $values ), $format ) );
40-
return $wpdb->prepare( "$column_name IN ($formats)", $values );
30+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $column_name_sql escaped as ident, $formats hardcoded value.
31+
return $wpdb->prepare( "{$column_name} IN ({$formats})", $values );
4132
}

phpcs.xml.dist

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-export">
3+
<description>Custom ruleset for WP-CLI export-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\Export"/><!-- Namespaces. -->
49+
<element value="wpcli_export"/><!-- 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/Export_Command\.php$</exclude-pattern>
57+
<exclude-pattern>*/src/WP_(Map|Post_IDs)_Iterator\.php$</exclude-pattern>
58+
<exclude-pattern>*/src/WP_(Export_Term|Iterator)_Exception\.php$</exclude-pattern>
59+
<exclude-pattern>*/src/WP_Export_WXR_Formatter\.php$</exclude-pattern>
60+
<exclude-pattern>*/src/WP_Export_XML_Over_HTTP\.php$</exclude-pattern>
61+
<exclude-pattern>*/src/WP_Export_(Base|File|Split_Files|Stdout)?_Writer\.php$</exclude-pattern>
62+
<exclude-pattern>*/src/WP_Export_(Exception|Oxymel|Query|Returner)\.php$</exclude-pattern>
63+
</rule>
64+
65+
66+
<!-- Exclude the word `wordpress` from being forced into "trademarked" capitalization in the file names. -->
67+
<rule ref="WordPress.WP.CapitalPDangit.Misspelled">
68+
<exclude-pattern>*/src/Export_Command\.php$</exclude-pattern>
69+
</rule>
70+
71+
</ruleset>

0 commit comments

Comments
 (0)