Skip to content

Commit dec4ccc

Browse files
authored
Merge pull request #104 from wp-cli/add-wp-cli-cs-checking
Implement CS checking based on the `WP_CLI_CS` ruleset
2 parents dc69582 + bf69d6d commit dec4ccc

13 files changed

+311
-259
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ PHAR_BUILD_VERSION
66
/packages
77
/vendor
88
/*.phar
9-
/phpunit.xml.dist
10-
/codesniffer
11-
/PHP_Codesniffer-VariableAnalysis
129
.*.swp
1310
*.log
11+
phpunit.xml
12+
phpcs.xml
13+
.phpcs.xml

phpcs.xml.dist

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
<?xml version="1.0"?>
2-
<ruleset name="WP-CLI">
3-
<description>WordPress Coding Standards for WP-CLI</description>
4-
5-
<!-- Show sniff codes in all reports, and progress while running -->
6-
<arg value="sp"/>
7-
<!-- Check all PHP files in directory tree by default. -->
8-
<arg name="extensions" value="php"/>
9-
<!-- Run different reports -->
10-
<arg name="report" value="full"/>
11-
<arg name="report" value="summary"/>
12-
<arg name="report" value="source"/>
2+
<ruleset name="WP-CLI-bundle">
3+
<description>Custom ruleset for WP-CLI-bundle</description>
134

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. -->
1414
<file>.</file>
15-
<exclude-pattern>*/ci/*</exclude-pattern>
16-
<exclude-pattern>*/features/*</exclude-pattern>
17-
<exclude-pattern>*/packages/*</exclude-pattern>
18-
<exclude-pattern>*/tests/*</exclude-pattern>
19-
<exclude-pattern>*/utils/*</exclude-pattern>
20-
<exclude-pattern>*/vendor/*</exclude-pattern>
21-
22-
<rule ref="PHPCompatibility">
23-
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
24-
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
25-
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
26-
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
27-
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
28-
</rule>
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 simultanously. -->
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 -->
2941
<config name="testVersion" value="5.4-"/>
3042

31-
<rule ref="WordPress-Core">
32-
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
33-
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
34-
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
35-
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
43+
<!--
44+
#############################################################################
45+
SELECTIVE EXCLUSIONS
46+
#############################################################################
47+
-->
48+
49+
<!-- These are two procedural stand-alone file that is never loaded in a
50+
WordPress context, so these files do not have to comply with WP naming
51+
conventions. -->
52+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
53+
<exclude-pattern>*/utils/get-package-require-from-composer\.php$</exclude-pattern>
54+
<exclude-pattern>*/utils/make-phar\.php$</exclude-pattern>
3655
</rule>
37-
<rule ref="WordPress.Files.FileName">
38-
<properties>
39-
<property name="strict_class_file_names" value="false"/>
40-
</properties>
41-
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
56+
<rule ref="WordPress.WP.GlobalVariablesOverride">
57+
<exclude-pattern>*/utils/get-package-require-from-composer\.php$</exclude-pattern>
58+
<exclude-pattern>*/utils/make-phar\.php$</exclude-pattern>
4259
</rule>
60+
4361
</ruleset>

utils/get-package-require-from-composer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
$file = $argv[1];
44
if ( ! file_exists( $file ) ) {
55
echo 'File does not exist.';
6-
exit(1);
6+
exit( 1 );
77
}
88

99
$contents = file_get_contents( $file );
1010
$composer = json_decode( $contents );
1111

1212
if ( empty( $composer ) || ! is_object( $composer ) ) {
1313
echo 'Invalid composer.json for package.';
14-
exit(1);
14+
exit( 1 );
1515
}
1616

1717
if ( empty( $composer->autoload->files ) ) {
1818
echo 'composer.json must specify valid "autoload" => "files"';
19-
exit(1);
19+
exit( 1 );
2020
}
2121

2222
echo implode( PHP_EOL, $composer->autoload->files );
23-
exit(0);
23+
exit( 0 );

utils/maintenance-bootstrap.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3-
$autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
4-
require_once $autoloader;
3+
$wpcli_maintenance_autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
4+
if ( is_readable( $wpcli_maintenance_autoloader ) ) {
5+
require_once $wpcli_maintenance_autoloader;
6+
}
57

68
WP_CLI::add_command( 'contrib-list', 'WP_CLI\Maintenance\Contrib_list_Command' );
79
WP_CLI::add_command( 'milestones-after', 'WP_CLI\Maintenance\Milestones_After_Command' );

utils/maintenance/Contrib_List_Command.php

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55

66
final class Contrib_List_Command {
77

8+
/**
9+
* Packages excluded from contributor list generation.
10+
*
11+
* @var array
12+
*/
13+
private $excluded_packages = [
14+
'wp-cli/wp-cli-tests',
15+
'wp-cli/regenerate-readme',
16+
'wp-cli/autoload-splitter',
17+
'wp-cli/wp-config-transformer',
18+
'wp-cli/php-cli-tools',
19+
'wp-cli/spyc',
20+
];
21+
822
/**
923
* Lists all contributors to this release.
1024
*
@@ -25,40 +39,33 @@ final class Contrib_List_Command {
2539
*/
2640
public function __invoke( $_, $assoc_args ) {
2741

28-
$contributors = array();
42+
$contributors = [];
2943
$pull_request_count = 0;
3044

3145
// Get the contributors to the current open large project milestones
32-
foreach( array( 'wp-cli/wp-cli-bundle', 'wp-cli/wp-cli', 'wp-cli/handbook', 'wp-cli/wp-cli.github.com' ) as $repo ) {
46+
foreach ( [ 'wp-cli/wp-cli-bundle', 'wp-cli/wp-cli', 'wp-cli/handbook', 'wp-cli/wp-cli.github.com' ] as $repo ) {
3347
$milestones = GitHub::get_project_milestones( $repo );
3448
// Cheap way to get the latest milestone
3549
$milestone = array_shift( $milestones );
3650
if ( ! $milestone ) {
3751
continue;
3852
}
3953
WP_CLI::log( 'Current open ' . $repo . ' milestone: ' . $milestone->title );
40-
$pull_requests = GitHub::get_project_milestone_pull_requests( $repo, $milestone->number );
54+
$pull_requests = GitHub::get_project_milestone_pull_requests( $repo, $milestone->number );
4155
$repo_contributors = GitHub::parse_contributors_from_pull_requests( $pull_requests );
4256
WP_CLI::log( ' - Contributors: ' . count( $repo_contributors ) );
4357
WP_CLI::log( ' - Pull requests: ' . count( $pull_requests ) );
4458
$pull_request_count += count( $pull_requests );
45-
$contributors = array_merge( $contributors, $repo_contributors );
59+
$contributors = array_merge( $contributors, $repo_contributors );
4660
}
4761

4862
// Identify all command dependencies and their contributors
63+
$bundle = 'wp-cli/wp-cli-bundle';
4964

50-
// TODO: Bundle repo needs to be switched to `wp-cli/wp-cli-bundle` for next release.
51-
$bundle = 'wp-cli/wp-cli';
52-
53-
$milestones = GitHub::get_project_milestones( 'wp-cli/wp-cli', array( 'state' => 'closed' ) );
65+
$milestones = GitHub::get_project_milestones( 'wp-cli/wp-cli', [ 'state' => 'closed' ] );
5466
// Cheap way to get the latest closed milestone
5567
$milestone = array_shift( $milestones );
56-
$tag = is_object( $milestone ) ? "v{$milestone->title}" : 'master';
57-
58-
// TODO: Only needed for switch from v1 to v2.
59-
if ( 'wp-cli/wp-cli' === $bundle ) {
60-
$tag = 'v1.5.1';
61-
}
68+
$tag = is_object( $milestone ) ? "v{$milestone->title}" : 'master';
6269

6370
$composer_lock_url = sprintf( 'https://raw.githubusercontent.com/%s/%s/composer.lock', $bundle, $tag );
6471
WP_CLI::log( 'Fetching ' . $composer_lock_url );
@@ -68,49 +75,43 @@ public function __invoke( $_, $assoc_args ) {
6875
}
6976
$composer_json = json_decode( $response->body, true );
7077

71-
// TODO: Only need for initial v2.
72-
$composer_json['packages'][] = array( 'name' => 'wp-cli/i18n-command', 'version' => 'v2' );
73-
usort( $composer_json['packages'], function ( $a, $b ) {
74-
return $a['name'] < $b['name'] ? -1 : 1;
75-
} );
78+
usort(
79+
$composer_json['packages'],
80+
function ( $a, $b ) {
81+
return $a['name'] < $b['name'] ? -1 : 1;
82+
}
83+
);
7684

77-
foreach( $composer_json['packages'] as $package ) {
78-
$package_name = $package['name'];
85+
foreach ( $composer_json['packages'] as $package ) {
86+
$package_name = $package['name'];
7987
$version_constraint = str_replace( 'v', '', $package['version'] );
8088
if ( ! preg_match( '#^wp-cli/.+-command$#', $package_name )
81-
&& ! in_array( $package_name, array(
82-
'wp-cli/wp-cli-tests',
83-
'wp-cli/regenerate-readme',
84-
'wp-cli/autoload-splitter',
85-
'wp-cli/wp-config-transformer',
86-
'wp-cli/php-cli-tools',
87-
'wp-cli/spyc',
88-
), true ) ) {
89+
&& ! in_array( $package_name, $this->excluded_packages, true ) ) {
8990
continue;
9091
}
9192
// Closed milestones denote a tagged release
92-
$milestones = GitHub::get_project_milestones( $package_name, array( 'state' => 'closed' ) );
93-
$milestone_ids = array();
94-
$milestone_titles = array();
95-
foreach( $milestones as $milestone ) {
93+
$milestones = GitHub::get_project_milestones( $package_name, [ 'state' => 'closed' ] );
94+
$milestone_ids = [];
95+
$milestone_titles = [];
96+
foreach ( $milestones as $milestone ) {
9697
if ( ! version_compare( $milestone->title, $version_constraint, '>' ) ) {
9798
continue;
9899
}
99-
$milestone_ids[] = $milestone->number;
100+
$milestone_ids[] = $milestone->number;
100101
$milestone_titles[] = $milestone->title;
101102
}
102103
// No shipped releases for this milestone.
103104
if ( empty( $milestone_ids ) ) {
104105
continue;
105106
}
106107
WP_CLI::log( 'Closed ' . $package_name . ' milestone(s): ' . implode( ', ', $milestone_titles ) );
107-
foreach( $milestone_ids as $milestone_id ) {
108-
$pull_requests = GitHub::get_project_milestone_pull_requests( $package_name, $milestone_id );
108+
foreach ( $milestone_ids as $milestone_id ) {
109+
$pull_requests = GitHub::get_project_milestone_pull_requests( $package_name, $milestone_id );
109110
$repo_contributors = GitHub::parse_contributors_from_pull_requests( $pull_requests );
110111
WP_CLI::log( ' - Contributors: ' . count( $repo_contributors ) );
111112
WP_CLI::log( ' - Pull requests: ' . count( $pull_requests ) );
112113
$pull_request_count += count( $pull_requests );
113-
$contributors = array_merge( $contributors, $repo_contributors );
114+
$contributors = array_merge( $contributors, $repo_contributors );
114115
}
115116
}
116117

@@ -119,9 +120,9 @@ public function __invoke( $_, $assoc_args ) {
119120

120121
// Sort and render the contributor list
121122
asort( $contributors, SORT_NATURAL | SORT_FLAG_CASE );
122-
if ( in_array( $assoc_args['format'], array( 'markdown', 'html' ) ) ) {
123+
if ( in_array( $assoc_args['format'], [ 'markdown', 'html' ], true ) ) {
123124
$contrib_list = '';
124-
foreach( $contributors as $url => $login ) {
125+
foreach ( $contributors as $url => $login ) {
125126
if ( 'markdown' === $assoc_args['format'] ) {
126127
$contrib_list .= '[@' . $login . '](' . $url . '), ';
127128
} elseif ( 'html' === $assoc_args['format'] ) {

0 commit comments

Comments
 (0)