-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwp-cli-command.php
153 lines (142 loc) · 5.47 KB
/
wp-cli-command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* PHPCS Diff WP CLI command
*
* @package wp-cli
* @subpackage commands/internals
*/
class PHPCS_Diff_CLI_Command extends WP_CLI_Command {
/**
* Run PHPCS against committed revisions
*
* ## OPTIONS
* --repo=<repo>
* : The VIP theme to run a scan on. This can also be a top level plugins directory.
*
* --start_revision=<end-revision>
* : The revision to start at.
*
* --end_revision=<start-revision>
* : The end revision to test.
*
* [--format=<format>]
* : Specify the output format. Allowed values are: table(default) and markdown
*
* [--standard=<standard>]
* : Specify the standard which should be used. Possible values: WordPress, WordPress-VIP (default), WordPressVIPminimum
*
* [--nocache]
* : If present the cached value won't be used and the cache won't be updated
*
* [--ignore-diff-too-big]
* : If present, the command will try to get results even for diffs whic hare too big for the Deploy Queue
*
* [--excluded-exts=<excluded-exts>]
* : Ignore specified extensions. Use comma for separating multiple extensions
*
* [--folder=<folder>]
* : Process files in specific folder
*
* ## EXAMPLES
* wp phpcs-diff --repo="hello-dolly" --start_revision=99998 --end_revision=100000
*
* @subcommand phpcs-diff
* @synopsis --repo=<repo> --start_revision=<start-revision> --end_revision=<end-revision> [--standard=<standard>] [--format=<format>] [--nocache] [--ignore-diff-too-big] [--excluded-exts=<excluded-exts>] [--folder=<folder>]
*/
public function __invoke( $args, $assoc_args ) {
require_once( PHPCS_DIFF_PLUGIN_DIR . 'inc/class-phpcs-diff.php' );
$repo = sanitize_title( $assoc_args['repo'] );
$start_revision = absint( $assoc_args['start_revision'] );
$end_revision = absint( $assoc_args['end_revision'] );
if ( true === array_key_exists( 'format', $assoc_args ) && true === in_array( $assoc_args['format'], array( 'table', 'markdown' ), true ) ) {
$format = $assoc_args['format'];
} else {
$format = 'table';
}
if ( true === array_key_exists( 'excluded-exts', $assoc_args ) && false === empty( $assoc_args['excluded-exts'] ) ) {
$excluded_exts = array_map( 'sanitize_text_field', explode( ',', $assoc_args['excluded-exts'] ) );
}
if ( true === array_key_exists( 'folder', $assoc_args ) ) {
$folder = sanitize_title( $assoc_args['folder'] );
} else {
$folder = '';
}
// @todo: replace SVN version control backend with any other parser you might want to use - eg.: git
require_once( PHPCS_DIFF_PLUGIN_DIR . 'backends/class-phpcs-diff-svn.php' );
$phpcs = new PHPCS_Diff( new PHPCS_Diff_SVN( $repo ) );
if ( true === array_key_exists( 'ignore-diff-too-big', $assoc_args ) ) {
$phpcs->set_no_diff_too_big( true );
}
if ( true === array_key_exists( 'nocache', $assoc_args ) ) {
$phpcs->set_nocache( true );
}
if ( true === array_key_exists( 'standard', $assoc_args )
&& true === in_array( sanitize_text_field( $assoc_args['standard'] ), array( 'WordPress', 'WordPress-VIP', 'WordPressVIPminimum' ), true ) )
{
$phpcs->set_phpcs_standard( sanitize_text_field( $assoc_args['standard'] ) );
}
if ( true === isset( $excluded_exts ) && false === empty( $excluded_exts ) && true === is_array( $excluded_exts ) ) {
$phpcs->set_excluded_extensions( $excluded_exts );
}
$found_issues = $phpcs->run( $start_revision, $end_revision, $folder );
if ( is_wp_error( $found_issues ) ) {
WP_CLI::error( $found_issues->get_error_message(), true );
} else if ( true === is_array( $found_issues ) && true === empty( $found_issues ) ) {
WP_CLI::line( 'There are no PHPCS issues in the diff. Yay!' );
return;
}
switch( $format ) {
case 'markdown':
$blockers = $warnings = $notes = array();
foreach ( $found_issues as $filename => $issues ) {
foreach ( $issues as $line => $line_issues ) {
foreach( $line_issues as $issue ) {
if ( 'ERROR' === $issue['level'] ) {
$blockers[] = '* ' . $filename . "#L" . $line . ' : ' . ltrim( $issue['message'], '[ x]' );
} else if ( 'WARNING' === $issue['level'] ) {
$warnings[] = '* ' . $filename . "#L" . $line . ' : ' . ltrim( $issue['message'], '[ x]' );
} else if ( 'NOTE' === $issue['level'] ) {
$notes[] = '* ' . $filename . "#L" . $line . ' : ' . ltrim( $issue['message'], '[ x]' );
}
}
}
}
if ( false === empty( $blockers ) ) {
WP_CLI::line( '### Blockers' );
foreach ( $blockers as $blocker ) {
WP_CLI::line( $blocker );
}
}
if ( false === empty( $warnings ) ) {
WP_CLI::line( '### Warnings' );
foreach ( $warnings as $warning ) {
WP_CLI::line( $warning );
}
}
if ( false === empty( $notes ) ) {
WP_CLI::line( '### Notes' );
foreach( $notes as $note ) {
WP_CLI::line( $note );
}
}
break;
case 'table':
default:
foreach( $found_issues as $filename => $issues ) {
$issue_display = array();
WP_CLI::line( 'File: ' . $filename . ' (' . count( $issues ) . ')' );
foreach( $issues as $line => $line_issues ) {
foreach( $line_issues as $issue ) {
$issue_display[] = array(
'line' => $line,
'level' => str_replace( array( 'ERROR', 'WARNING' ), array( 'Blocker', 'Warning' ), $issue['level'] ),
'message' => ltrim( $issue['message'], '[ x]' ),
);
}
}
WP_CLI\Utils\format_items( 'table', $issue_display, array( 'line', 'level', 'message' ) );
}
}
}
}
WP_CLI::add_command( 'phpcs-diff', 'PHPCS_Diff_CLI_Command' );