Skip to content

Commit a396128

Browse files
authored
Merge pull request #55 from wp-cli/feature/use-phpcs
2 parents 312a80e + 9cd5492 commit a396128

File tree

6 files changed

+71
-67
lines changed

6 files changed

+71
-67
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
@@ -17,7 +17,7 @@
1717
},
1818
"require-dev": {
1919
"behat/behat": "~2.5",
20-
"wp-cli/wp-cli-tests": "^2"
20+
"wp-cli/wp-cli-tests": "^2.1"
2121
},
2222
"config": {
2323
"platform": {

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

1212
WP_CLI::add_command( 'find', 'Find_Command' );

phpcs.xml.dist

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
11
<?xml version="1.0"?>
2-
<ruleset name="WP-CLI">
3-
<description>Custom ruleset for WP-CLI</description>
2+
<ruleset name="WP-CLI-find">
3+
<description>Custom ruleset for WP-CLI find-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>
415

5-
<!-- For help in understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
6-
<!-- For help in using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
16+
<!-- Show progress. -->
17+
<arg value="p"/>
718

8-
<!-- What to scan -->
9-
<file>.</file>
10-
<!-- Ignoring Files and Folders:
11-
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
12-
<exclude-pattern>*/.git/*</exclude-pattern>
13-
<exclude-pattern>*/ci/*</exclude-pattern>
14-
<exclude-pattern>*/features/*</exclude-pattern>
15-
<exclude-pattern>*/packages/*</exclude-pattern>
16-
<exclude-pattern>*/tests/*</exclude-pattern>
17-
<exclude-pattern>*/utils/*</exclude-pattern>
18-
<exclude-pattern>*/vendor/*</exclude-pattern>
19-
20-
<!-- How to scan -->
21-
<arg value="sp"/> <!-- Show sniff and progress -->
22-
<arg name="colors"/> <!-- Show results with colors -->
23-
<arg name="extensions" value="php"/> <!-- Limit to PHP files -->
24-
25-
<!-- Rules: Check PHP version compatibility - see
26-
https://github.com/PHPCompatibility/PHPCompatibilityWP -->
27-
<rule ref="PHPCompatibilityWP">
28-
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
29-
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
30-
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
31-
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
32-
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
33-
</rule>
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
3421

35-
<!-- For help in understanding this testVersion:
36-
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
37-
<config name="testVersion" value="5.4-"/>
22+
<!-- Check up to 8 files simultaneously. -->
23+
<arg name="parallel" value="8"/>
3824

39-
<!-- Ignore php_uname mode issue, we're conditionally providing a callback -->
40-
<rule ref="PHPCompatibility.PHP.NewFunctionParameters.php_uname_modeFound">
41-
<exclude-pattern>*/php/commands/src/CLI_Command.php</exclude-pattern>
42-
</rule>
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
4330

44-
<!-- Rules: WordPress Coding Standards - see
45-
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
46-
<rule ref="WordPress-Core">
47-
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
48-
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
49-
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
50-
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
51-
</rule>
31+
<rule ref="WP_CLI_CS"/>
5232

53-
<!-- For help in understanding these custom sniff properties:
54-
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
55-
<rule ref="WordPress.Files.FileName">
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">
5646
<properties>
57-
<property name="strict_class_file_names" value="false"/>
47+
<property name="prefixes" type="array">
48+
<element value="WP_CLI\Find"/><!-- Namespaces. -->
49+
<element value="wpcli_find"/><!-- Global variables and such. -->
50+
</property>
5851
</properties>
59-
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
6052
</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/Find_Command\.php$</exclude-pattern>
57+
</rule>
58+
6159
</ruleset>

src/Find_Command.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use WP_CLI\Utils;
4+
use WP_CLI\Formatter;
45

56
class Find_Command {
67

@@ -9,7 +10,7 @@ class Find_Command {
910
*
1011
* @var array
1112
*/
12-
private $ignored_paths = array(
13+
private $ignored_paths = [
1314
// System directories
1415
'/__MACOSX/',
1516
// Webserver directories
@@ -53,7 +54,7 @@ class Find_Command {
5354
// Already in a WordPress install
5455
'/wp-admin/',
5556
'/wp-content/',
56-
);
57+
];
5758

5859
/**
5960
* Beginning of the recursion path.
@@ -102,14 +103,14 @@ class Find_Command {
102103
*
103104
* @var array
104105
*/
105-
private $resolved_aliases = array();
106+
private $resolved_aliases = [];
106107

107108
/**
108109
* Found WordPress installations.
109110
*
110111
* @var array
111112
*/
112-
private $found_wp = array();
113+
private $found_wp = [];
113114

114115
/**
115116
* Find WordPress installations on the filesystem.
@@ -207,7 +208,7 @@ public function __invoke( $args, $assoc_args ) {
207208
$this->resolved_aliases[ rtrim( $target['path'], '/' ) ] = $alias;
208209
}
209210

210-
$fields = array( 'version_path', 'version', 'depth', 'alias' );
211+
$fields = [ 'version_path', 'version', 'depth', 'alias' ];
211212
if ( ! empty( $assoc_args['fields'] ) ) {
212213
$fields = explode( ',', $assoc_args['fields'] );
213214
}
@@ -216,7 +217,7 @@ public function __invoke( $args, $assoc_args ) {
216217
$this->log( "Searching for WordPress installations in '{$path}'" );
217218
$this->recurse_directory( $this->base_path );
218219
$this->log( "Finished search for WordPress installations in '{$path}'" );
219-
$formatter = new \WP_CLI\Formatter( $assoc_args, $fields );
220+
$formatter = new Formatter( $assoc_args, $fields );
220221
$formatter->display_items( $this->found_wp );
221222
}
222223

@@ -233,7 +234,7 @@ private function recurse_directory( $path ) {
233234
// Don't recurse directories that probably don't have a WordPress installation.
234235
if ( ! $this->skip_ignored_paths ) {
235236
// Assume base path doesn't need comparison
236-
$compared_path = preg_replace( '#^' . preg_quote( $this->base_path ) . '#', '', $path );
237+
$compared_path = preg_replace( '#^' . preg_quote( $this->base_path, '/' ) . '#', '', $path );
237238
// Ignore all hidden system directories
238239
$bits = explode( '/', trim( $compared_path, '/' ) );
239240
$current_dir = array_pop( $bits );
@@ -258,7 +259,7 @@ private function recurse_directory( $path ) {
258259
$alias = isset( $this->resolved_aliases[ $wp_path ] ) ? $this->resolved_aliases[ $wp_path ] : '';
259260
$wp_path = rtrim( $wp_path, '/' ) . '/';
260261

261-
$this->found_wp[ $version_path ] = array(
262+
$this->found_wp[ $version_path ] = [
262263
'version_path' => $version_path,
263264
'version' => self::get_wp_version( $version_path ),
264265
'wp_path' => rtrim( $wp_path, '/' ) . '/',
@@ -267,25 +268,25 @@ private function recurse_directory( $path ) {
267268
'db_host' => '',
268269
'db_name' => '',
269270
'db_user' => '',
270-
);
271+
];
271272

272273
$config_path = self::get_wp_config_path( $wp_path );
273274
if ( $config_path ) {
274275
try {
275276
$transformer = new WPConfigTransformer( $config_path );
276-
foreach ( array( 'db_host', 'db_name', 'db_user' ) as $constant ) {
277+
foreach ( [ 'db_host', 'db_name', 'db_user' ] as $constant ) {
277278
$value = $transformer->get_value( 'constant', strtoupper( $constant ) );
278279
// Clean up strings.
279280
$first = substr( $value, 0, 1 );
280281
$last = substr( $value, -1 );
281-
$both = array_unique( array( $first, $last ) );
282-
if ( in_array( $both, array( array( '"' ), array( "'" ) ), true ) ) {
282+
$both = array_unique( [ $first, $last ] );
283+
if ( in_array( $both, [ [ '"' ], [ "'" ] ], true ) ) {
283284
$value = substr( $value, 1, -1 );
284285
}
285286
$this->found_wp[ $version_path ][ $constant ] = $value;
286287
}
287288
} catch ( Exception $exception ) {
288-
$this->log( "Could not process the 'wp-config.php' transformation: " . $exception->getMessage() );
289+
$this->log( "Could not process the 'wp-config.php' transformation: {$exception->getMessage()}" );
289290
}
290291
}
291292
$this->log( "Found WordPress installation at '{$version_path}'" );

0 commit comments

Comments
 (0)