-
Notifications
You must be signed in to change notification settings - Fork 83
Description
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_Commandcommand class should be created and exists atincludes/CLI- Constructor should receive
Plugin_Context $plugin_context - Command should be registered using
WP_CLI::add_command( 'plugin check', __CLASS_NAMESPACE__ . '\register_command' );in thePlugin_Mainfile. - Provided
--checksargument, command should process only these checks - Provided
--flagargument, command should process only flagged checks, default should bestable - Provided
--formatargument, command should display results in that format - Provided
--fieldsargument, command should display results with these fields only - Provided
--ignore-warningsargument, command should display results with errors only - Provided
--ignore-errorsargument, command should display results with warnings only - The
Plugin_Check_Commandhas a publiccheck( array $args, array $assoc_args )method.- The method will attempt to resolve the plugin name passed in the
$argsarray. If the plugin does not exist and aWP_CLI::error()is printed. - The
CLI_Runnerclassesrun()method is called. TheCLI_Runneris 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 aWP_CLI:error(). - The
run()method will return an instance of theCheck_Resultclass. - If the
--ignore-warningsoption is exists in the$assoc_args, the warnings are ignored in theCheck_Resultinstance. - If the
--ignore-errorsoption is exists in the$assoc_args, the errors are ignored in theCheck_Resultinstance. - The
Check_Resultare then printed by the command looping over each of the files, displaying errors first and warnings second.- A new
WP_CLI\Formatterclass is created based on the--formatoption passed in the$assoc_argsarray. It will also use the--fieldsoption to set what fields are displayed. - Each file printed will display both errors and warnings by combining the
$errorsand$warningstogether and sorting each result by the file line number. - Results are then printed using the
WP_CLI::line()andWP_CLI\Formatterclass created earlier. Results are displayed with the filename first and each subsequent line an individual error or warning.
- A new
- The method will attempt to resolve the plugin name passed in the
- 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 theWP_CLI\Formatterbased 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 theWP_CLI\Formattercreated earlier.
- A protected
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-errorsshould run expected checks and return expected results - Execute CLI command without required arguments should give error