Skip to content

Create the WP-CLI subcommand wp plugin check #30

@mehulkaklotar

Description

@mehulkaklotar

Description

The plugin should extend the current wp plugin command, which will do single or multiple checks. The CLI command class file should be placed in the CLI directory under the includes directory. The class name should be WordPress\Plugin_Check\CLI\Plugin_Check_Command.

This issue is dependent on:

Sub-Command

check

Command arguments

<plugin>
The plugin to check. Plugin name.

--checks=<checks>
Only runs checks provided as an argument in comma-separated values, e.g. enqueued-scripts, escaping. Otherwise runs all checks.

--flag=<flags>
Limit the checks being executed according to their flags, e.g. stable, beta or experimental. Default is stable.

--format=<format>
Format to display the results. Options are table, csv, and json. The default will be a table.

--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.

Examples

wp plugin check akismet
wp plugin check akismet --check=escaping
wp plugin check akismet --format=json

Acceptance Criteria

  • Plugin_Check_Command command class should be created and exists at includes/CLI
  • Constructor should receive Plugin_Context $plugin_context
  • Command should be registered using WP_CLI::add_command( 'plugin check', __CLASS_NAMESPACE__ . '\register_command' ); in the Plugin_Main file.
  • Provided --checks argument, command should process only these checks
  • Provided --flag argument, command should process only flagged checks, default should be stable
  • Provided --format argument, command should display results in that format
  • Provided --fields argument, command should display results with these fields only
  • Provided --ignore-warnings argument, command should display results with errors only
  • Provided --ignore-errors argument, command should display results with warnings only
  • The Plugin_Check_Command has a public check( array $args, array $assoc_args ) method.
    • The method will attempt to resolve the plugin name passed in the $args array. If the plugin does not exist and a WP_CLI::error() is printed.
    • The CLI_Runner classes run() method is called. The CLI_Runner is instantiated and prepared earlier in the process so no parameters are needed to be passed.
    • The run() method is called in a try/catch block. If an exception is thrown by the method it is caught and printed as a WP_CLI:error().
    • The run() method will return an instance of the Check_Result class.
    • If the --ignore-warnings option is exists in the $assoc_args, the warnings are ignored in the Check_Result instance.
    • If the --ignore-errors option is exists in the $assoc_args, the errors are ignored in the Check_Result instance.
    • The Check_Result are then printed by the command looping over each of the files, displaying errors first and warnings second.
      • A new WP_CLI\Formatter class is created based on the --format option passed in the $assoc_args array. It will also use the --fields option to set what fields are displayed.
      • Each file printed will display both errors and warnings by combining the $errors and $warnings together and sorting each result by the file line number.
      • Results are then printed using the WP_CLI::line() and WP_CLI\Formatter class created earlier. Results are displayed with the filename first and each subsequent line an individual error or warning.
  • Additional protected methods can be created to encapsulate logic within the run method.
    • A protected get_plugin_from_args( array $args ) method can be used to find the plugin requested from the CLI arguments as well as check the plugin exist.
    • A protected get_formatter( array $assoc_args ) method can be used to create the WP_CLI\Formatter based on the CLI arguments passed.
    • A protected flatten_file_results( array $file_errors, array $file_warnings ) method can be created to handle combining a files errors and warning into a single array and order them by file number.
    • A protected display_results( WP_CLI\Formatter $formatter, string $file_name, array $file_results ) method can be used to display the results using the file name, flattened results array and the WP_CLI\Formatter created earlier.

Tests Coverage

  • Executing CLI command with default arguments should run expected checks
  • Execute CLI command with different arguments set for --checks, --flag, --format, --fields, --ignore-warnings, --ignore-errors should run expected checks and return expected results
  • Execute CLI command without required arguments should give error

Metadata

Metadata

Labels

WP-CLIIssues related to WP-CLI[Type] EnhancementA suggestion for improvement of an existing feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions