Skip to content

Commit

Permalink
add table format, simplify code and right exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Sep 13, 2024
1 parent 8d61a59 commit 92a3b76
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 70 deletions.
48 changes: 48 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
116 changes: 46 additions & 70 deletions wp-cli/class-theme-check-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Theme_Check_Command extends WP_CLI_Command {
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: cli
* default: table
* options:
* - cli
* - table
* - json
* ---
*
Expand All @@ -42,7 +42,7 @@ class Theme_Check_Command extends WP_CLI_Command {
* @return void
*/
public function run( $args, $assoc_args ) {
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' );
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' );

// Get the current theme
$current_theme = wp_get_theme();
Expand All @@ -55,38 +55,35 @@ public function run( $args, $assoc_args ) {
$theme = wp_get_theme( $check_theme_slug );

if ( ! $theme->exists() ) {
if ( $format === 'json' ) {
$json_output = array(
'completed' => false,
'result' => "Error: Theme '{$check_theme_slug}' not found.",
'messages' => array(),
);
WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) );
return;
}
WP_CLI::error( "Theme '{$check_theme_slug}' not found." );
}

// Run the checks.
// Run the checks
$success = run_themechecks_against_theme( $theme, $check_theme_slug );

Check warning on line 62 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space
$processed_messages = $this->process_themecheck_messages();

if ( $format === 'json' ) {
if ( ! $success ) {
$json_output = array(
'completed' => false,
'result' => "Error: Theme check failed for {$check_theme_slug}.",
'messages' => array(),
);
WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) );
return;
}
$this->display_themechecks_as_json( $check_theme_slug );
} else {
if ( ! $success ) {
WP_CLI::error( "Theme check failed for {$check_theme_slug}." );
}
$this->display_themechecks_in_cli( $check_theme_slug );
// The validation value is a boolean, but if the format is not JSON, we want to return a string.
$validation_value = $format === 'json'
? false
: "There are no required changes in the theme {$check_theme_slug}.";

if ( ! $success ) {
$validation_value =

Check failure on line 71 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Whitespace found at end of line
// If the format is JSON, return false, otherwise return a message
$format === 'json'
? true
: "There are required changes in the theme {$check_theme_slug}.";
}

$processed_messages[] = array(
'type' => 'VALIDATION',

Check warning on line 79 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Array double arrow not aligned correctly; expected 2 space(s) between &quot;'type'&quot; and double arrow, but found 1.
'value' => $validation_value,
);

WP_CLI\Utils\format_items( $format, $processed_messages, array( 'type', 'value' ) );

Check failure on line 84 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Whitespace found at end of line
// Set the exit code based on $success
WP_CLI::halt( $success ? 0 : 1 );
}

/**
Expand All @@ -110,51 +107,30 @@ private function process_themecheck_messages() {

$processed_messages = array_map(
function( $message ) {
return html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' );
},
$messages
);

return $processed_messages;
}

/**
* Display the theme checks in the CLI.
*
* @param string $slug The slug of the theme to display the checks for.
* @return void
*/
private function display_themechecks_in_cli( $slug ) {
$processed_messages = $this->process_themecheck_messages();

WP_CLI::success( "Theme check completed for {$slug}." );

if ( empty( $processed_messages ) ) {
WP_CLI::line( 'No issues found.' );
return;
}
if ( preg_match( '/<span[^>]*>(.*?)<\/span>(.*)/', $message, $matches ) ) {
$key = $matches[1];

Check warning on line 111 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$value = $matches[2];
} else {
$key = '';

Check warning on line 114 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$value = $message;
}

foreach ( $processed_messages as $message ) {
WP_CLI::line( '' );
WP_CLI::line( $message );
}
}
$key = wp_strip_all_tags( $key );

Check warning on line 118 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Equals sign not aligned with surrounding assignments; expected 1 space but found 3 spaces
$key = html_entity_decode( $key, ENT_QUOTES, 'UTF-8' );

Check warning on line 119 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Equals sign not aligned with surrounding assignments; expected 1 space but found 3 spaces
$key = rtrim( $key, ':' );

Check warning on line 120 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Equals sign not aligned with surrounding assignments; expected 1 space but found 3 spaces

/**
* Display the theme checks in JSON format.
*
* @param string $slug The slug of the theme to display the checks for.
* @return void
*/
private function display_themechecks_as_json( $slug ) {
$processed_messages = $this->process_themecheck_messages();
$value = wp_strip_all_tags( $value );
$value = html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
$value = ltrim( $value, ': ' );

$json_output = array(
'completed' => true,
'result' => "Theme check completed for {$slug}.",
'messages' => $processed_messages,
return array(
'type' => trim( $key ),

Check warning on line 127 in wp-cli/class-theme-check-cli.php

View workflow job for this annotation

GitHub Actions / PHP Code Standards

Array double arrow not aligned correctly; expected 2 space(s) between &quot;'type'&quot; and double arrow, but found 1.
'value' => trim( $value ),
);
},
$messages
);

WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) );
return $processed_messages;
}
}

0 comments on commit 92a3b76

Please sign in to comment.