Skip to content

Commit

Permalink
Validate fields names
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Dec 27, 2023
1 parent 72f3eb3 commit 572b042
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ final class Plugin_Check_Command {
'json',
);

/**
* Field names.
*
* @since n.e.x.t
* @var string[]
*/
protected $allowed_fields = array(
'line',
'column',
'type',
'code',
'message',
);

/**
* Constructor.
*
Expand Down Expand Up @@ -114,21 +128,12 @@ 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_options( $assoc_args );

// Validate field names.
$this->validate_fields( wp_parse_list( $options['fields'] ) );

// Create the plugin and checks array from CLI arguments.
$plugin = isset( $args[0] ) ? $args[0] : '';
$checks = wp_parse_list( $options['checks'] );
Expand Down Expand Up @@ -411,4 +416,21 @@ private function has_runtime_check( array $checks ) {

return false;
}

/**
* Validate field names.
*
* @since n.e.x.t
*
* @param array $fields An array of field names.
*/
private function validate_fields( array $fields ) {
$invalid_fields = array_diff( $fields, $this->allowed_fields );

if ( count( $invalid_fields ) > 0 ) {
// translators: %s: Field names.
$message = ( 1 === count( $invalid_fields ) ) ? __( 'Invalid field: %s', 'plugin-check' ) : __( 'Invalid fields: %s', 'plugin-check' );
WP_CLI::error( sprintf( $message, join( ', ', $invalid_fields ) ) );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L432-L433

Added lines #L432 - L433 were not covered by tests
}
}
}

0 comments on commit 572b042

Please sign in to comment.