Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Categories and Checks CLI commands #339

Merged
merged 3 commits into from
Jan 5, 2024

Conversation

ernilambar
Copy link
Member

@ernilambar ernilambar commented Dec 14, 2023

Fixes #303

Command:
wp plugin list-check-categories

Output:
Screen Shot 2023-12-14 at 4 01 54 PM

Command:
wp plugin list-checks

Output:
Screen Shot 2023-12-14 at 4 01 39 PM

Comment on lines 85 to 95
// Create the runner if not already initialized.
if ( is_null( $runner ) ) {
$runner = new CLI_Checks_Runner();
}

// Make sure we are using the correct runner instance.
if ( ! ( $runner instanceof CLI_Checks_Runner ) ) {
WP_CLI::error(
__( 'CLI Checks Runner was not initialized correctly.', 'plugin-check' )
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I really don't like that this simple command requires a new *_Runner just because the check registration happens in Abstract_Check_Runner::register_checks()

Especially since the current CLI_Runner class is mostly a hack anyway

I think we need some refactoring to move the registration out of the runner class, maybe to a factory method on Default_Check_Repository or so.

Listing all registered checks should be as simple as getting a Default_Check_Repository and listing what's in there.

cc @felixarntz for thoughts

Comment on lines 84 to 94
/*
* 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' )
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code can be removed as it's dead code.

This class can only ever be loaded when the plugin is active, so it does not make sense to check whether the plugin is active.

/**
* Plugin check categories command.
*/
final class Plugin_Check_Categories_Command {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add some functional tests for this command in tests/behat/features/list-categories.feature?

/**
* Plugin check checks command.
*/
final class Plugin_Check_Checks_Command {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add some functional tests for this command in tests/behat/features/list-checks.feature?

cli.php Outdated Show resolved Hide resolved
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @ernilambar! Looks great so far. I've added a few points of feedback below. Let me know if you have any questions.

includes/CLI/Plugin_Check_Categories_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Checks_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Checks_Command.php Outdated Show resolved Hide resolved
@ernilambar ernilambar force-pushed the 303-cli-commands branch 3 times, most recently from 361e9f3 to 0074b28 Compare December 22, 2023 12:04
@ernilambar
Copy link
Member Author

Code is updated as per the comments. New commands will be:

  • wp plugin list-checks
  • wp plugin list-check-categories

Behat tests implementation for new commands is remaining.

@swissspidy Is it possible to run behat tests in local environment? Or is it implemented now in GitHub actions only?

@swissspidy
Copy link
Member

Use composer behat to run the Behat tests.

You may need to run composer prepare-behat-tests once first to create the test database

@ernilambar ernilambar force-pushed the 303-cli-commands branch 11 times, most recently from e6d16e9 to 29e177f Compare December 25, 2023 07:54
@ernilambar
Copy link
Member Author

PR ready for review.

  • CLI commands updated as per the comments.
  • Behat tests added
  • Updated PHP Unit tests to reflect new changes.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ernilambar The logic itself looks great now. I think there are a couple places where we can drastically simplify the code and avoid duplicate code, most importantly by using the existing Plugin_Check_Command class for the 2 new CLI commands.

cli.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Check_Categories_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Check_Categories_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Check_Categories_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Checks_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Checks_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Checks_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_List_Check_Categories_Command.php Outdated Show resolved Hide resolved
@ernilambar ernilambar force-pushed the 303-cli-commands branch 8 times, most recently from aa2f059 to e6ea8b2 Compare January 3, 2024 11:21
@ernilambar
Copy link
Member Author

PR has been update as per the review comments.

Note:

  • Since PHPMD was generating error, I have added ignore @SuppressWarnings(PHPMD.ExcessiveClassComplexity) in Plugin_Check_Command.
  • get_list_check_categories_options() and get_list_checks_options() methods are similar now. But there is some room for enhancements for those listing commands. When we implement those, methods would be different.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ernilambar I think this is almost good to go now. A few follow up feedback points.

includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
@ernilambar ernilambar force-pushed the 303-cli-commands branch 7 times, most recently from c07f66f to 772805c Compare January 4, 2024 06:23
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @ernilambar!

Just a few last nit-picks, but no real blockers. Thank you for all your work on this! 🙌

includes/CLI/Plugin_Check_Command.php Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
includes/CLI/Plugin_Check_Command.php Outdated Show resolved Hide resolved
@mukeshpanchal27
Copy link
Member

In the follow-up PR, it's essential to update the documentation to reflect the changes made by this pull request. Specifically, we removed the Abstract_Check_Runner::register_checks() method. cc. @felixarntz

The affected documentation files that need updating are:

@ernilambar
Copy link
Member Author

PR updated as per the comments. Fixed spacing issues and improved function docs.

@swissspidy swissspidy merged commit 0e2a2ed into WordPress:trunk Jan 5, 2024
25 checks passed
@ernilambar ernilambar deleted the 303-cli-commands branch January 5, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI: add command for listing all possible checks and categories
4 participants