Skip to content

Commit

Permalink
Add List Checks and List Check Categories commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jan 3, 2024
1 parent 0162771 commit e6ea8b2
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 154 deletions.
257 changes: 236 additions & 21 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace WordPress\Plugin_Check\CLI;

use Exception;
use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\CLI_Runner;
use WordPress\Plugin_Check\Checker\Default_Check_Repository;
use WordPress\Plugin_Check\Checker\Runtime_Check;
use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup;
use WordPress\Plugin_Check\Plugin_Context;
Expand Down Expand Up @@ -40,6 +42,30 @@ final class Plugin_Check_Command {
'json',
);

/**
* Default fields.
*
* @since n.e.x.t
* @var array
*/
protected $default_fields = array(
'check' => array(
'line',
'column',
'code',
'message',
),
'list-checks' => array(
'slug',
'category',
'stability',
),
'list-check-categories' => array(
'name',
'slug',
),
);

/**
* Constructor.
*
Expand Down Expand Up @@ -127,7 +153,7 @@ public function check( $args, $assoc_args ) {
}

// Get options based on the CLI arguments.
$options = $this->get_options( $assoc_args );
$options = $this->get_check_options( $assoc_args );

// Create the plugin and checks array from CLI arguments.
$plugin = isset( $args[0] ) ? $args[0] : '';
Expand Down Expand Up @@ -211,7 +237,7 @@ static function ( $dirs ) use ( $excluded_directories ) {
}

// Get formatter.
$formatter = $this->get_formatter( $assoc_args );
$formatter = $this->get_formatter( $assoc_args, 'check' );

// Print the formatted results.
// Go over all files with errors first and print them, combined with any warnings in the same file.
Expand All @@ -233,7 +259,137 @@ static function ( $dirs ) use ( $excluded_directories ) {
}

/**
* Validates the associative arguments.
* Runs plugin list-checks.
*
* ## 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
* ---
*
* @subcommand list-checks
*
* @since n.e.x.t
*
* @param array $args List of the positional arguments.
* @param array $assoc_args List of the associative arguments.
*
* @throws WP_CLI\ExitException Show error if not valid runner.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function list_checks( $args, $assoc_args ) {
$check_repo = new Default_Check_Repository();

$all_checks = array();

$checks_items = $check_repo->get_checks()->to_map();

foreach ( $checks_items as $key => $check ) {
$item = array();

$item['slug'] = $key;
$item['stability'] = strtolower( $check->get_stability() );
$item['category'] = join( ', ', $check->get_categories() );

$all_checks[] = $item;
}

// Get options based on the CLI arguments.
$options = $this->get_list_checks_options( $assoc_args );

// Get formatter.
$formatter = $this->get_formatter( $options, 'list-checks' );

// Display results.
$formatter->display_items( $all_checks );
}

/**
* Runs plugin list-check-categories.
*
* ## 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 n.e.x.t
*
* @param array $args List of the positional arguments.
* @param array $assoc_args List of the associative arguments.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function list_check_categories( $args, $assoc_args ) {
// Get options based on the CLI arguments.
$options = $this->get_list_check_categories_options( $assoc_args );

// Get check categories details.
$categories = $this->get_check_categories();

// Get formatter.
$formatter = $this->get_formatter( $options, 'list-check-categories' );

// Display results.
$formatter->display_items( $categories );
}

/**
* Returns check categories details.
*
* @since n.e.x.t
*
* @return array List of the check categories.
*/
private function get_check_categories() {
$categories = array();

$check_categories = new Check_Categories();
$categories_slugs = $check_categories->get_categories();

$categories = array_map(
function ( $slug ) {
return array(
'slug' => $slug,
'name' => ucfirst( str_replace( '_', ' ', $slug ) ),
);
},
$categories_slugs
);

return $categories;
}

/**
* Validates the associative arguments for 'check' command.
*
* @since n.e.x.t
*
Expand All @@ -242,7 +398,7 @@ static function ( $dirs ) use ( $excluded_directories ) {
*
* @throws WP_CLI\ExitException Show error if plugin not found.
*/
private function get_options( $assoc_args ) {
private function get_check_options( $assoc_args ) {
$defaults = array(
'checks' => '',
'format' => 'table',
Expand All @@ -266,35 +422,94 @@ private function get_options( $assoc_args ) {
return $options;
}

/**
* Validates the associative arguments 'list-check-categories' command.
*
* @since n.e.x.t
*
* @param array $assoc_args List of the associative arguments.
* @return array List of the associative arguments.
*
* @throws WP_CLI\ExitException Show error if format is invalid.
*/
private function get_list_check_categories_options( $assoc_args ) {
$defaults = array(
'format' => 'table',
);

$options = wp_parse_args( $assoc_args, $defaults );

if ( ! in_array( $options['format'], $this->output_formats, true ) ) {
WP_CLI::error(
sprintf(

Check warning on line 444 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L443-L444

Added lines #L443 - L444 were not covered by tests
// translators: 1. Output formats.
__( 'Invalid format argument, valid value will be one of [%1$s]', 'plugin-check' ),
implode( ', ', $this->output_formats )
)
);

Check warning on line 449 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L446-L449

Added lines #L446 - L449 were not covered by tests
}

return $options;
}

/**
* Validates the associative arguments 'list-checks' command.
*
* @since n.e.x.t
*
* @param array $assoc_args List of the associative arguments.
* @return array List of the associative arguments.
*
* @throws WP_CLI\ExitException Show error if format is invalid.
*/
private function get_list_checks_options( $assoc_args ) {
$defaults = array(
'format' => 'table',
);

$options = wp_parse_args( $assoc_args, $defaults );

if ( ! in_array( $options['format'], $this->output_formats, true ) ) {
WP_CLI::error(
sprintf(

Check warning on line 474 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L473-L474

Added lines #L473 - L474 were not covered by tests
// translators: 1. Output formats.
__( 'Invalid format argument, valid value will be one of [%1$s]', 'plugin-check' ),
implode( ', ', $this->output_formats )
)
);

Check warning on line 479 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L476-L479

Added lines #L476 - L479 were not covered by tests
}

return $options;
}

/**
* Gets the formatter instance to format check results.
*
* @since n.e.x.t
*
* @param array $assoc_args Associative arguments.
* @param array $assoc_args Associative arguments.
* @param string $type Command type.
* @return WP_CLI\Formatter The formatter instance.
*/
private function get_formatter( $assoc_args ) {
$default_fields = array(
'line',
'column',
'code',
'message',
);
private function get_formatter( $assoc_args, $type ) {
$default_fields = $this->default_fields[ $type ];

if ( isset( $assoc_args['fields'] ) ) {
$default_fields = wp_parse_args( $assoc_args['fields'], $default_fields );
}

// 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',
);
// This applies only to 'wp plugin check' command.
if ( 'check' === $type ) {
// 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',
);
}
}

return new WP_CLI\Formatter(
Expand Down
46 changes: 2 additions & 44 deletions includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ abstract protected function get_categories_param();
* @since n.e.x.t
*/
final public function __construct() {
$this->initialized_early = ! did_action( 'muplugins_loaded' );
$this->register_checks();
$this->initialized_early = ! did_action( 'muplugins_loaded' );
$this->check_repository = new Default_Check_Repository();
$this->runtime_environment = new Runtime_Environment_Setup();
}

Expand Down Expand Up @@ -529,48 +529,6 @@ private function get_check_context() {
return new Check_Context( WP_PLUGIN_DIR . '/' . $this->get_plugin_basename() );
}

/**
* Registers Checks to the Check_Repository.
*
* @since n.e.x.t
*/
private function register_checks() {
$this->check_repository = new Default_Check_Repository();

/**
* Filters the available plugin check classes.
*
* @since n.e.x.t
*
* @param array $checks An array map of check slugs to Check instances.
*/
$checks = apply_filters(
'wp_plugin_check_checks',
array(
'i18n_usage' => new Checks\I18n_Usage_Check(),
'enqueued_scripts_size' => new Checks\Enqueued_Scripts_Size_Check(),
'code_obfuscation' => new Checks\Code_Obfuscation_Check(),
'file_type' => new Checks\File_Type_Check(),
'plugin_header_text_domain' => new Checks\Plugin_Header_Text_Domain_Check(),
'late_escaping' => new Checks\Late_Escaping_Check(),
'plugin_updater' => new Checks\Plugin_Updater_Check(),
'plugin_review_phpcs' => new Checks\Plugin_Review_PHPCS_Check(),
'direct_db_queries' => new Checks\Direct_DB_Queries_Check(),
'performant_wp_query_params' => new Checks\Performant_WP_Query_Params_Check(),
'enqueued_scripts_in_footer' => new Checks\Enqueued_Scripts_In_Footer_Check(),
'plugin_readme' => new Checks\Plugin_Readme_Check(),
'enqueued_styles_scope' => new Checks\Enqueued_Styles_Scope_Check(),
'localhost' => new Checks\Localhost_Check(),
'no_unfiltered_uploads' => new Checks\No_Unfiltered_Uploads_Check(),
'trademarks' => new Checks\Trademarks_Check(),
)
);

foreach ( $checks as $slug => $check ) {
$this->check_repository->register_check( $slug, $check );
}
}

/**
* Sets the runtime environment setup.
*
Expand Down
Loading

0 comments on commit e6ea8b2

Please sign in to comment.