Skip to content

Commit

Permalink
Simplify methods in check CLI class
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jan 4, 2024
1 parent 6e79e77 commit e965985
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 152 deletions.
199 changes: 64 additions & 135 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

/**
* Plugin check command.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
final class Plugin_Check_Command {

Expand All @@ -44,30 +42,6 @@ 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 @@ -142,20 +116,18 @@ public function __construct( Plugin_Context $plugin_context ) {
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function check( $args, $assoc_args ) {
/*
* 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' )
);
}

// Get options based on the CLI arguments.
$options = $this->get_check_options( $assoc_args );
$options = $this->get_options(
$assoc_args,
array(
'checks' => '',
'format' => 'table',
'ignore-warnings' => false,
'ignore-errors' => false,
'include-experimental' => false,

)
);

// Create the plugin and checks array from CLI arguments.
$plugin = isset( $args[0] ) ? $args[0] : '';
Expand Down Expand Up @@ -238,8 +210,11 @@ static function ( $dirs ) use ( $excluded_directories ) {
$warnings = $result->get_warnings();
}

// Default fields.
$default_fields = $this->get_check_default_fields( $assoc_args );

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

// Print the formatted results.
// Go over all files with errors first and print them, combined with any warnings in the same file.
Expand Down Expand Up @@ -286,9 +261,6 @@ static function ( $dirs ) use ( $excluded_directories ) {
* @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();
Expand All @@ -308,10 +280,17 @@ public function list_checks( $args, $assoc_args ) {
}

// Get options based on the CLI arguments.
$options = $this->get_list_checks_options( $assoc_args );
$options = $this->get_options( $assoc_args, array( 'format' => 'table' ) );

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

// Display results.
$formatter->display_items( $all_checks );
Expand Down Expand Up @@ -346,19 +325,22 @@ public function list_checks( $args, $assoc_args ) {
*
* @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 );
$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, 'list-check-categories' );
$formatter = $this->get_formatter(
$options,
array(
'name',
'slug',
)
);

// Display results.
$formatter->display_items( $categories );
Expand Down Expand Up @@ -391,24 +373,17 @@ function ( $slug ) {
}

/**
* Validates the associative arguments for 'check' command.
* Validates the associative arguments.
*
* @since n.e.x.t
*
* @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 plugin not found.
*/
private function get_check_options( $assoc_args ) {
$defaults = array(
'checks' => '',
'format' => 'table',
'ignore-warnings' => false,
'ignore-errors' => false,
'include-experimental' => false,
);

private function get_options( $assoc_args, $defaults ) {
$options = wp_parse_args( $assoc_args, $defaults );

if ( ! in_array( $options['format'], $this->output_formats, true ) ) {
Expand All @@ -425,99 +400,53 @@ private function get_check_options( $assoc_args ) {
}

/**
* Validates the associative arguments 'list-check-categories' command.
* Gets the formatter instance to format check results.
*
* @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.
* @param array $assoc_args Associative arguments.
* @param array $default_fields Default fields.
* @return WP_CLI\Formatter The formatter instance.
*/
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(
// translators: 1. Output formats.
__( 'Invalid format argument, valid value will be one of [%1$s]', 'plugin-check' ),
implode( ', ', $this->output_formats )
)
);
private function get_formatter( $assoc_args, $default_fields ) {
if ( isset( $assoc_args['fields'] ) ) {
$default_fields = wp_parse_args( $assoc_args['fields'], $default_fields );
}

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

/**
* Validates the associative arguments 'list-checks' command.
* Returns check default fields.
*
* @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.
* @param array $assoc_args Associative arguments.
* @return array Default fields.
*/
private function get_list_checks_options( $assoc_args ) {
$defaults = array(
'format' => 'table',
private function get_check_default_fields( $assoc_args ) {
$default_fields = array(
'line',
'column',
'code',
'message',
);

$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 )
)
// 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 $options;
}

/**
* Gets the formatter instance to format check results.
*
* @since n.e.x.t
*
* @param array $assoc_args Associative arguments.
* @param string $type Command type.
* @return WP_CLI\Formatter The formatter instance.
*/
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 );
}

// 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(
$assoc_args,
$default_fields
);
return $default_fields;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/Checker/Default_Check_Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @since n.e.x.t
*/
class Default_Check_Repository extends Empty_Check_Repository implements Check_Repository {
class Default_Check_Repository extends Empty_Check_Repository {

/**
* Initializes checks.
Expand Down
8 changes: 0 additions & 8 deletions includes/Checker/Empty_Check_Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ class Empty_Check_Repository implements Check_Repository {
*/
protected $static_checks = array();

/**
* Constructor.
*
* @since n.e.x.t
*/
public function __construct() {
}

/**
* Registers a check to the repository.
*
Expand Down
10 changes: 8 additions & 2 deletions tests/behat/features/plugin-list-check-categories.feature
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
Feature: Test that the WP-CLI plugin list check categories command works.

Scenario: List check categories in JSON format
Scenario: List check categories
Given a WP install with the Plugin Check plugin

When I try the WP-CLI command `plugin list-check-categories --format=json`
When I run the WP-CLI command `plugin list-check-categories --format=json`
Then STDOUT should be JSON containing:
"""
[{"name":"General","slug":"general"}]
"""

When I run the WP-CLI command `plugin list-check-categories --format=csv --fields=slug,name`
Then STDOUT should contain:
"""
plugin_repo,"Plugin repo"
"""
10 changes: 8 additions & 2 deletions tests/behat/features/plugin-list-checks.feature
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
Feature: Test that the WP-CLI plugin list checks command works.

Scenario: List checks in JSON format
Scenario: List checks
Given a WP install with the Plugin Check plugin

When I try the WP-CLI command `plugin list-checks --format=json`
When I run the WP-CLI command `plugin list-checks --format=json`
Then STDOUT should be JSON containing:
"""
[{"slug":"i18n_usage","category":"general","stability":"stable"}]
"""

When I run the WP-CLI command `plugin list-checks --format=csv --fields=slug,category`
Then STDOUT should contain:
"""
plugin_header_text_domain,plugin_repo
"""
8 changes: 4 additions & 4 deletions tests/phpunit/tests/Checker/Check_Categories_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Default_Check_Repository;
use WordPress\Plugin_Check\Checker\Empty_Check_Repository;
use WordPress\Plugin_Check\Test_Data\Category_Check_Five;
use WordPress\Plugin_Check\Test_Data\Category_Check_Four;
use WordPress\Plugin_Check\Test_Data\Category_Check_One;
Expand All @@ -17,13 +17,13 @@

class Check_Categories_Tests extends WP_UnitTestCase {
/**
* @var Default_Check_Repository
* @var Empty_Check_Repository
*/
protected $repository;

public function set_up() {
parent::set_up();
$this->repository = new Default_Check_Repository();
$this->repository = new Empty_Check_Repository();
}

public function test_get_categories() {
Expand Down Expand Up @@ -53,7 +53,7 @@ public function test_filter_checks_by_categories( array $categories, array $all_
$check_categories = new Check_Categories();
$filtered_checks = $check_categories->filter_checks_by_categories( $checks, $categories );

$this->assertEquals( array_keys( $expected_filtered_checks ), array_values( array_intersect( array_keys( $filtered_checks->to_map() ), array_keys( $expected_filtered_checks ) ) ) );
$this->assertEquals( $expected_filtered_checks, $filtered_checks->to_map() );
}

public function data_checks_by_categories() {
Expand Down

0 comments on commit e965985

Please sign in to comment.