-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathPlugin_Check_Command.php
More file actions
698 lines (618 loc) · 18.9 KB
/
Plugin_Check_Command.php
File metadata and controls
698 lines (618 loc) · 18.9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<?php
/**
* Class WordPress\Plugin_Check\CLI\Plugin_Check_Command
*
* @package plugin-check
*/
namespace WordPress\Plugin_Check\CLI;
use Exception;
use WordPress\Plugin_Check\Checker\Check;
use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Repository;
use WordPress\Plugin_Check\Checker\CLI_Runner;
use WordPress\Plugin_Check\Checker\Default_Check_Repository;
use WordPress\Plugin_Check\Plugin_Context;
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
use WordPress\Plugin_Check\Utilities\Results_Exporter;
use WP_CLI;
/**
* Plugin check command.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
final class Plugin_Check_Command {
/**
* Plugin context.
*
* @since 1.0.0
* @var Plugin_Context
*/
protected $plugin_context;
/**
* Output format type.
*
* @since 1.0.0
* @var string[]
*/
protected $output_formats = array(
'table',
'csv',
'json',
'ctrf',
'strict-table',
'strict-csv',
'strict-json',
'strict-ctrf',
);
/**
* Constructor.
*
* @since 1.0.0
*
* @param Plugin_Context $plugin_context Plugin context.
*/
public function __construct( Plugin_Context $plugin_context ) {
$this->plugin_context = $plugin_context;
}
/**
* Runs plugin check.
*
* ## OPTIONS
*
* <plugin>
* : The plugin to check. Plugin name.
*
* [--checks=<checks>]
* : Only runs checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping. Otherwise runs all checks.
*
* [--exclude-checks=<checks>]
* : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping.
* Applies after evaluating `--checks`.
*
* [--ignore-codes=<codes>]
* : Ignore error codes provided as an argument in comma-separated values.
*
* [--format=<format>]
* : Format to display the results. Options are table, csv, json, ctrf, strict-table, strict-csv, strict-json, and strict-ctrf. The default will be a table.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - ctrf
* - strict-table
* - strict-csv
* - strict-json
* - strict-ctrf
* ---
*
* [--categories]
* : Limit displayed results to include only specific categories Checks.
*
* [--fields=<fields>]
* : Limit displayed results to a subset of fields provided.
*
* [--ignore-warnings]
* : Limit displayed results to exclude warnings.
*
* [--ignore-errors]
* : Limit displayed results to exclude errors.
*
* [--include-experimental]
* : Include experimental checks.
*
* [--exclude-directories=<directories>]
* : Additional directories to exclude from checks.
* By default, `.git`, `vendor`, `vendor_prefixed`, `vendor-prefixed` and `node_modules` directories are excluded.
*
* [--exclude-files=<files>]
* : Additional files to exclude from checks.
*
* [--severity=<severity>]
* : Severity level.
*
* [--error-severity=<error-severity>]
* : Error severity level.
*
* [--warning-severity=<warning-severity>]
* : Warning severity level.
*
* [--include-low-severity-errors]
* : Include errors with lower severity than the threshold as other type.
*
* [--include-low-severity-warnings]
* : Include warnings with lower severity than the threshold as other type.
*
* [--slug=<slug>]
* : Slug to override the default.
*
* [--mode=<mode>]
* : Mode to run the checks in. Options are 'new' (default) or 'update'.
* ---
* default: new
* options:
* - new
* - update
* ---
*
* ## EXAMPLES
*
* wp plugin check akismet
* wp plugin check akismet --checks=late_escaping
* wp plugin check akismet --format=json
* wp plugin check akismet --mode=update
*
* @subcommand check
*
* @since 1.0.0
*
* @param array $args List of the positional arguments.
* @param array $assoc_args List of the associative arguments.
*
* @throws Exception Throws exception.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function check( $args, $assoc_args ) {
// Get options based on the CLI arguments.
$options = $this->get_options(
$assoc_args,
array(
'checks' => '',
'format' => 'table',
'ignore-warnings' => false,
'ignore-errors' => false,
'include-experimental' => false,
'severity' => '',
'error-severity' => '',
'warning-severity' => '',
'include-low-severity-errors' => false,
'include-low-severity-warnings' => false,
'slug' => '',
'ignore-codes' => '',
'mode' => 'new',
)
);
// Create the plugin and checks array from CLI arguments.
$plugin = isset( $args[0] ) ? $args[0] : '';
$checks = wp_parse_list( $options['checks'] );
// Ignore codes.
$ignore_codes = isset( $options['ignore-codes'] ) ? wp_parse_list( $options['ignore-codes'] ) : array();
// Create the categories array from CLI arguments.
$categories = isset( $options['categories'] ) ? wp_parse_list( $options['categories'] ) : array();
$excluded_directories = isset( $options['exclude-directories'] ) ? wp_parse_list( $options['exclude-directories'] ) : array();
add_filter(
'wp_plugin_check_ignore_directories',
static function ( $dirs ) use ( $excluded_directories ) {
return array_unique( array_merge( $dirs, $excluded_directories ) );
}
);
$excluded_files = isset( $options['exclude-files'] ) ? wp_parse_list( $options['exclude-files'] ) : array();
add_filter(
'wp_plugin_check_ignore_files',
static function ( $dirs ) use ( $excluded_files ) {
return array_unique( array_merge( $dirs, $excluded_files ) );
}
);
// Get the CLI Runner.
$runner = Plugin_Request_Utility::get_runner();
// Create the runner if not already initialized.
if ( is_null( $runner ) ) {
$runner = new CLI_Runner();
}
// Make sure we are using the correct runner instance.
if ( ! ( $runner instanceof CLI_Runner ) ) {
WP_CLI::error(
__( 'CLI Runner was not initialized correctly.', 'plugin-check' )
);
}
// Ensure the correct slug.
if ( is_dir( $plugin ) && empty( $options['slug'] ) ) {
$options['slug'] = basename( $plugin );
} elseif ( filter_var( $plugin, FILTER_VALIDATE_URL ) && empty( $options['slug'] ) ) {
$options['slug'] = Plugin_Request_Utility::get_slug_from_url( $plugin );
}
try {
$runner->set_experimental_flag( $options['include-experimental'] );
$runner->set_check_slugs( $checks );
$runner->set_plugin( $plugin );
$runner->set_categories( $categories );
$runner->set_slug( $options['slug'] );
$runner->set_mode( $options['mode'] );
} catch ( Exception $error ) {
WP_CLI::error( $error->getMessage() );
}
$result = false;
// Run checks against the plugin.
try {
$result = $runner->run();
} catch ( Exception $error ) {
Plugin_Request_Utility::destroy_runner();
WP_CLI::error( $error->getMessage() );
}
Plugin_Request_Utility::destroy_runner();
// Get errors and warnings from the results.
$errors = array();
if ( $result && empty( $assoc_args['ignore-errors'] ) ) {
$errors = $result->get_errors();
}
$warnings = array();
if ( $result && empty( $assoc_args['ignore-warnings'] ) ) {
$warnings = $result->get_warnings();
}
if ( empty( $errors ) && empty( $warnings ) ) {
WP_CLI::success( __( 'Checks complete. No errors found.', 'plugin-check' ) );
return;
}
// Default fields.
$default_fields = $this->get_check_default_fields( $assoc_args );
// Get formatter.
$formatter = $this->get_formatter( $assoc_args, $default_fields );
// Severity.
$error_severity = ! empty( $options['error-severity'] ) ? $options['error-severity'] : $options['severity'];
$warning_severity = ! empty( $options['warning-severity'] ) ? $options['warning-severity'] : $options['severity'];
$include_low_severity_errors = ! empty( $options['include-low-severity-errors'] ) ? true : false;
$include_low_severity_warnings = ! empty( $options['include-low-severity-warnings'] ) ? true : false;
$all_results = array();
// Collect all errors.
foreach ( $errors as $file_name => $file_errors ) {
$file_warnings = array();
if ( isset( $warnings[ $file_name ] ) ) {
$file_warnings = $warnings[ $file_name ];
unset( $warnings[ $file_name ] );
}
$file_results = Results_Exporter::flatten_file_results( $file_errors, $file_warnings );
if ( ! empty( $ignore_codes ) ) {
$file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes );
}
if ( '' !== $error_severity || '' !== $warning_severity ) {
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ), $include_low_severity_errors, $include_low_severity_warnings );
}
foreach ( $file_results as $item ) {
$item['file'] = $file_name;
$all_results[] = $item;
}
}
// Collect remaining warnings.
foreach ( $warnings as $file_name => $file_warnings ) {
$file_results = Results_Exporter::flatten_file_results( array(), $file_warnings );
if ( ! empty( $ignore_codes ) ) {
$file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes );
}
if ( '' !== $error_severity || '' !== $warning_severity ) {
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ), $include_low_severity_errors, $include_low_severity_warnings );
}
foreach ( $file_results as $item ) {
$item['file'] = $file_name;
$all_results[] = $item;
}
}
// Handle CTRF formats.
if ( Results_Exporter::FORMAT_CTRF === $options['format'] || 'strict-' . Results_Exporter::FORMAT_CTRF === $options['format'] ) {
$ctrf_report = Results_Exporter::to_ctrf_json(
$all_results,
array(
'timestamp_iso' => gmdate( 'c' ),
)
);
WP_CLI::line( $ctrf_report );
return;
}
// Handle strict-* formats.
if ( str_starts_with( $options['format'], 'strict-' ) ) {
$base_format = substr( $options['format'], 7 );
$formatter_args = $assoc_args;
$formatter_args['format'] = $base_format;
$formatter = $this->get_formatter( $formatter_args, $default_fields );
$formatter->display_items( $all_results );
return;
}
// Group results by file.
$results_by_file = array();
foreach ( $all_results as $item ) {
$results_by_file[ $item['file'] ][] = $item;
}
foreach ( $results_by_file as $file_name => $file_results ) {
$this->display_results( $formatter, $file_name, $file_results );
}
}
/**
* Lists the available checks for plugins.
*
* ## OPTIONS
*
* [--fields=<fields>]
* : Limit displayed results to a subset of fields provided.
*
* [--format=<format>]
* : Format to display the results. Options are table, csv, and json. The default will be a table.
* ---
* default: table
* options:
* - table
* - csv
* - json
* ---
*
* [--categories]
* : Limit displayed results to include only specific categories.
*
* [--include-experimental]
* : Include experimental checks.
*
* ## EXAMPLES
*
* wp plugin list-checks
* wp plugin list-checks --format=json
*
* @subcommand list-checks
*
* @since 1.0.0
*
* @param array $args List of the positional arguments.
* @param array $assoc_args List of the associative arguments.
*
* @throws WP_CLI\ExitException Show error if invalid format argument.
*/
public function list_checks( $args, $assoc_args ) {
$check_repo = new Default_Check_Repository();
// Get options based on the CLI arguments.
$options = $this->get_options(
$assoc_args,
array(
'format' => 'table',
'categories' => '',
'include-experimental' => false,
)
);
$check_flags = Check_Repository::TYPE_ALL;
// Check whether to include experimental checks.
if ( $options['include-experimental'] ) {
$check_flags = $check_flags | Check_Repository::INCLUDE_EXPERIMENTAL;
}
$collection = $check_repo->get_checks( $check_flags );
// Filters the checks by specific categories.
if ( ! empty( $options['categories'] ) ) {
$categories = array_map( 'trim', explode( ',', $options['categories'] ) );
$collection = Check_Categories::filter_checks_by_categories( $collection, $categories );
}
$all_checks = array();
/**
* All checks to list.
*
* @var Check $check
*/
foreach ( $collection as $key => $check ) {
$item = array();
$item['slug'] = $key;
$item['stability'] = strtolower( $check->get_stability() );
$item['category'] = join( ', ', $check->get_categories() );
$item['description'] = $check->get_description();
$item['url'] = $check->get_documentation_url();
$all_checks[] = $item;
}
// Get formatter.
$formatter = $this->get_formatter(
$options,
array(
'slug',
'category',
'stability',
'description',
'url',
)
);
// Display results.
$formatter->display_items( $all_checks );
}
/**
* Lists the available check categories for plugins.
*
* ## OPTIONS
*
* [--fields=<fields>]
* : Limit displayed results to a subset of fields provided.
*
* [--format=<format>]
* : Format to display the results. Options are table, csv, and json. The default will be a table.
* ---
* default: table
* options:
* - table
* - csv
* - json
* ---
*
* ## EXAMPLES
*
* wp plugin list-check-categories
* wp plugin list-check-categories --format=json
*
* @subcommand list-check-categories
*
* @since 1.0.0
*
* @param array $args List of the positional arguments.
* @param array $assoc_args List of the associative arguments.
*
* @throws WP_CLI\ExitException Show error if invalid format argument.
*/
public function list_check_categories( $args, $assoc_args ) {
// Get options based on the CLI arguments.
$options = $this->get_options( $assoc_args, array( 'format' => 'table' ) );
// Get check categories details.
$categories = $this->get_check_categories();
// Get formatter.
$formatter = $this->get_formatter(
$options,
array(
'name',
'slug',
)
);
// Display results.
$formatter->display_items( $categories );
}
/**
* Returns check categories details.
*
* @since 1.0.0
*
* @return array List of the check categories.
*/
private function get_check_categories() {
$check_categories = new Check_Categories();
$all_categories = $check_categories->get_categories();
$categories = array();
foreach ( $all_categories as $slug => $label ) {
$categories[] = array(
'slug' => $slug,
'name' => $label,
);
}
return $categories;
}
/**
* Validates the associative arguments.
*
* @since 1.0.0
*
* @param array $assoc_args List of the associative arguments.
* @param array $defaults List of the default arguments.
* @return array List of the associative arguments.
*
* @throws WP_CLI\ExitException Show error if invalid format argument.
*/
private function get_options( $assoc_args, $defaults ) {
$options = wp_parse_args( $assoc_args, $defaults );
if ( ! in_array( $options['format'], $this->output_formats, true ) ) {
WP_CLI::error(
sprintf(
// translators: 1. Output formats.
__( 'Invalid format argument, valid value will be one of [%1$s]', 'plugin-check' ),
implode( ', ', $this->output_formats )
)
);
}
return $options;
}
/**
* Gets the formatter instance to format check results.
*
* @since 1.0.0
*
* @param array $assoc_args Associative arguments.
* @param array $default_fields Default fields.
* @return WP_CLI\Formatter The formatter instance.
*/
private function get_formatter( $assoc_args, $default_fields ) {
if ( isset( $assoc_args['fields'] ) ) {
$default_fields = wp_parse_args( $assoc_args['fields'], $default_fields );
}
return new WP_CLI\Formatter(
$assoc_args,
$default_fields
);
}
/**
* Returns check default fields.
*
* @since 1.0.0
*
* @param array $assoc_args Associative arguments.
* @return array Default fields.
*/
private function get_check_default_fields( $assoc_args ) {
$default_fields = array(
'line',
'column',
'code',
'message',
'docs',
);
// If both errors and warnings are included, display the type of each result too.
if ( empty( $assoc_args['ignore_errors'] ) && empty( $assoc_args['ignore_warnings'] ) ) {
$default_fields = array(
'line',
'column',
'type',
'code',
'message',
'docs',
);
}
return $default_fields;
}
/**
* Displays the results.
*
* @since 1.0.0
*
* @param WP_CLI\Formatter $formatter Formatter class.
* @param string $file_name File name.
* @param array $file_results Results.
*/
private function display_results( $formatter, $file_name, $file_results ) {
WP_CLI::line(
sprintf(
'FILE: %s',
$file_name
)
);
$formatter->display_items( $file_results );
WP_CLI::line();
WP_CLI::line();
}
/**
* Returns check results filtered by severity level.
*
* @since 1.1.0
*
* @param array $results Check results.
* @param int $error_severity Error severity level.
* @param int $warning_severity Warning severity level.
* @param bool $include_low_severity_errors Include less level of severity issues as warning.
* @param bool $include_low_severity_warnings Include less level of severity issues as warning.
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
* @return array Filtered results.
*/
private function get_filtered_results_by_severity( $results, $error_severity, $warning_severity, $include_low_severity_errors = false, $include_low_severity_warnings = false ) {
$errors = array();
$warnings = array();
foreach ( $results as $item ) {
if ( 'ERROR' === $item['type'] && $item['severity'] >= $error_severity ) {
$errors[] = $item;
} elseif ( $include_low_severity_errors && 'ERROR' === $item['type'] && $item['severity'] < $error_severity ) {
$item['type'] = 'ERROR_LOW_SEVERITY';
$errors[] = $item;
} elseif ( $include_low_severity_warnings && 'WARNING' === $item['type'] && $item['severity'] < $warning_severity ) {
$item['type'] = 'WARNING_LOW_SEVERITY';
$warnings[] = $item;
} elseif ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ) {
$warnings[] = $item;
}
}
return array_merge( $errors, $warnings );
}
/**
* Returns check results filtered by ignore codes.
*
* @since 1.4.0
*
* @param array $results Check results.
* @param array $ignore_codes Array of error codes to be ignored.
* @return array Filtered results.
*/
private function get_filtered_results_by_ignore_codes( $results, $ignore_codes ) {
return array_filter(
$results,
static function ( $result ) use ( $ignore_codes ) {
return ! in_array( $result['code'], $ignore_codes, true );
}
);
}
}