Skip to content

Commit

Permalink
Add Categories and Checks CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Dec 14, 2023
1 parent 0101c91 commit 7fbb19b
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use WordPress\Plugin_Check\Checker\CLI_Runner;
use WordPress\Plugin_Check\Plugin_Context;
use WordPress\Plugin_Check\CLI\Plugin_Check_Command;
use WordPress\Plugin_Check\CLI\Plugin_Check_Categories_Command;
use WordPress\Plugin_Check\CLI\Plugin_Check_Checks_Command;

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
Expand All @@ -35,7 +37,10 @@
// Create the CLI command instance and add to WP CLI.
$plugin_command = new Plugin_Check_Command( $context );
WP_CLI::add_command( 'plugin', $plugin_command );

$plugin_categories_command = new Plugin_Check_Categories_Command( $context );
WP_CLI::add_command( 'plugin', $plugin_categories_command );
$plugin_checks_command = new Plugin_Check_Checks_Command( $context );
WP_CLI::add_command( 'plugin', $plugin_checks_command );

/**
* Adds hook to set up the object-cache.php drop-in file.
Expand Down
191 changes: 191 additions & 0 deletions includes/CLI/Plugin_Check_Categories_Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
/**
* Class WordPress\Plugin_Check\CLI\Plugin_Check_Categories_Command
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\CLI;

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Plugin_Context;
use WP_CLI;

/**
* Plugin check categories command.
*/
final class Plugin_Check_Categories_Command {

/**
* Plugin context.
*
* @since n.e.x.t
* @var Plugin_Context
*/
protected $plugin_context;

/**
* Output format type.
*
* @since n.e.x.t
* @var string[]
*/
protected $output_formats = array(
'table',
'csv',
'json',
);

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Plugin_Context $plugin_context Plugin context.
*/
public function __construct( Plugin_Context $plugin_context ) {
$this->plugin_context = $plugin_context;

Check warning on line 47 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L46-L47

Added lines #L46 - L47 were not covered by tests
}

/**
* Runs plugin 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 check-categories
* wp plugin check-categories --format=json
*
* @subcommand 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 check_categories( $args, $assoc_args ) {

Check warning on line 83 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L83

Added line #L83 was not covered by tests
/*
* Bail early if the Plugin Checker is not activated.
*
* If the Plugin Checker is not activated, it will throw an error and
* CLI commands won't be usable.
*/
if ( is_plugin_inactive( $this->plugin_context->basename() ) ) {
WP_CLI::error(
__( 'Plugin Checker is not active.', 'plugin-check' )
);

Check warning on line 93 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L90-L93

Added lines #L90 - L93 were not covered by tests
}

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

Check warning on line 97 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L97

Added line #L97 was not covered by tests

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

Check warning on line 100 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L100

Added line #L100 was not covered by tests

// Get formatter.
$formatter = $this->get_formatter( $options );

Check warning on line 103 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L103

Added line #L103 was not covered by tests

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

Check warning on line 106 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L106

Added line #L106 was not covered by tests
}

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

Check warning on line 117 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L116-L117

Added lines #L116 - L117 were not covered by tests

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

Check warning on line 120 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L119-L120

Added lines #L119 - L120 were not covered by tests

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

Check warning on line 131 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L122-L131

Added lines #L122 - L131 were not covered by tests

}

return $categories;

Check warning on line 135 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L135

Added line #L135 was not covered by tests
}

/**
* Validates the associative arguments.
*
* @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 plugin not found.
*/
private function get_options( $assoc_args ) {
$defaults = array(
'format' => 'table',
);

Check warning on line 151 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L148-L151

Added lines #L148 - L151 were not covered by tests

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

Check warning on line 153 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L153

Added line #L153 was not covered by tests

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

Check warning on line 157 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L155-L157

Added lines #L155 - L157 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 162 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L159-L162

Added lines #L159 - L162 were not covered by tests
}

return $options;

Check warning on line 165 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L165

Added line #L165 was not covered by tests
}

/**
* Gets the formatter instance to format check results.
*
* @since n.e.x.t
*
* @param array $assoc_args Associative arguments.
* @return WP_CLI\Formatter The formatter instance.
*/
private function get_formatter( $assoc_args ) {
$default_fields = array(
'name',
'slug',
);

Check warning on line 180 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L176-L180

Added lines #L176 - L180 were not covered by tests

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

Check warning on line 183 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L182-L183

Added lines #L182 - L183 were not covered by tests
}

return new WP_CLI\Formatter(
$assoc_args,
$default_fields
);

Check warning on line 189 in includes/CLI/Plugin_Check_Categories_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Categories_Command.php#L186-L189

Added lines #L186 - L189 were not covered by tests
}
}
177 changes: 177 additions & 0 deletions includes/CLI/Plugin_Check_Checks_Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Class WordPress\Plugin_Check\CLI\Plugin_Check_Checks_Command
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\CLI;

use WordPress\Plugin_Check\Checker\CLI_Checks_Runner;
use WordPress\Plugin_Check\Plugin_Context;
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
use WP_CLI;

/**
* Plugin check checks command.
*/
final class Plugin_Check_Checks_Command {

/**
* Plugin context.
*
* @since n.e.x.t
* @var Plugin_Context
*/
protected $plugin_context;

/**
* Output format type.
*
* @since n.e.x.t
* @var string[]
*/
protected $output_formats = array(
'table',
'csv',
'json',
);

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Plugin_Context $plugin_context Plugin context.
*/
public function __construct( Plugin_Context $plugin_context ) {
$this->plugin_context = $plugin_context;

Check warning on line 48 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L47-L48

Added lines #L47 - L48 were not covered by tests
}

/**
* Runs plugin check-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 check-checks
*
* @since n.e.x.t
*
* @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)
*/
public function check_checks( $args, $assoc_args ) {

Check failure on line 81 in includes/CLI/Plugin_Check_Checks_Command.php

View workflow job for this annotation

GitHub Actions / PHP

PHPDoc tag @throws with type WordPress\Plugin_Check\CLI\Exception is not subtype of Throwable

Check warning on line 81 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L81

Added line #L81 was not covered by tests
// Get the Runner.
$runner = Plugin_Request_Utility::get_runner();

Check warning on line 83 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L83

Added line #L83 was not covered by tests

// Create the runner if not already initialized.
if ( is_null( $runner ) ) {
$runner = new CLI_Checks_Runner();

Check warning on line 87 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L86-L87

Added lines #L86 - L87 were not covered by tests
}

// Make sure we are using the correct runner instance.
if ( ! ( $runner instanceof CLI_Checks_Runner ) ) {
WP_CLI::error(
__( 'CLI Checks Runner was not initialized correctly.', 'plugin-check' )
);

Check warning on line 94 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L91-L94

Added lines #L91 - L94 were not covered by tests
}

$all_checks = array();

Check warning on line 97 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L97

Added line #L97 was not covered by tests

$checks_to_run = $runner->get_checks_to_run();

Check warning on line 99 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L99

Added line #L99 was not covered by tests

if ( is_array( $checks_to_run ) && count( $checks_to_run ) > 0 ) {
foreach ( $checks_to_run as $key => $check ) {
$item = array();

Check warning on line 103 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L101-L103

Added lines #L101 - L103 were not covered by tests

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

Check warning on line 107 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L105-L107

Added lines #L105 - L107 were not covered by tests

$all_checks[] = $item;

Check warning on line 109 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L109

Added line #L109 was not covered by tests
}
}

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

Check warning on line 114 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L114

Added line #L114 was not covered by tests

// Get formatter.
$formatter = $this->get_formatter( $options );

Check warning on line 117 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L117

Added line #L117 was not covered by tests

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

Check warning on line 120 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L120

Added line #L120 was not covered by tests
}

/**
* Validates the associative arguments.
*
* @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 plugin not found.
*/
private function get_options( $assoc_args ) {
$defaults = array(
'format' => 'table',
);

Check warning on line 136 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L133-L136

Added lines #L133 - L136 were not covered by tests

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

Check warning on line 138 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L138

Added line #L138 was not covered by tests

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

Check warning on line 142 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L140-L142

Added lines #L140 - L142 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 147 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L144-L147

Added lines #L144 - L147 were not covered by tests
}

return $options;

Check warning on line 150 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L150

Added line #L150 was not covered by tests
}

/**
* Gets the formatter instance to format results.
*
* @since n.e.x.t
*
* @param array $assoc_args Associative arguments.
* @return WP_CLI\Formatter The formatter instance.
*/
private function get_formatter( $assoc_args ) {
$default_fields = array(
'slug',
'category',
'stability',
);

Check warning on line 166 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L161-L166

Added lines #L161 - L166 were not covered by tests

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

Check warning on line 169 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L168-L169

Added lines #L168 - L169 were not covered by tests
}

return new WP_CLI\Formatter(
$assoc_args,
$default_fields
);

Check warning on line 175 in includes/CLI/Plugin_Check_Checks_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Checks_Command.php#L172-L175

Added lines #L172 - L175 were not covered by tests
}
}
Loading

0 comments on commit 7fbb19b

Please sign in to comment.