Skip to content

Refactor to remove repeated code.#157

Closed
spacedmonkey wants to merge 7 commits into
trunkfrom
fix/repeated-code
Closed

Refactor to remove repeated code.#157
spacedmonkey wants to merge 7 commits into
trunkfrom
fix/repeated-code

Conversation

@spacedmonkey
Copy link
Copy Markdown
Member

Description of the Change

While reviewing code, I noticed a lot of repeated code in Admin_Ajax. Simple refactor to tidy up the code.

Closes #

How to test the Change

Changelog Entry

Added - New feature
Changed - Existing functionality
Deprecated - Soon-to-be removed feature
Removed - Feature
Fixed - Bug fix
Security - Vulnerability

Credits

Props @username, @username2, ...

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

Copy link
Copy Markdown
Contributor

@jjgrainger jjgrainger left a comment

Choose a reason for hiding this comment

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

Thanks @spacedmonkey looks good, left a couple of comments based on previous feedback in another PR

Comment thread includes/Admin/Admin_AJAX.php Outdated
public function set_up_environment() {
// Verify the nonce before continuing.
$valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$this->validate_request();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had originally approached this that way before but it was recommended to decouple this and handle the response outside of the verify/validate method. (see #97 (comment))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think those two things are the same. This does change any of the logic, just moves it around.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Moving the logic into reusable functions makes sense, but to @jjgrainger's point we shouldn't couple the new utility methods to send JSON response data. The response should be handled separately by the AJAX callbacks, the new utility functions should rather throw an Exception or return WP_Error that can then be returned as JSON.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment on lines +238 to +239
Plugin_Request_Utility::initialize_runner();
$runner = Plugin_Request_Utility::get_runner();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The initialize_runner method handles other logic alongside creating the correct runner (and could handle more in the future). As we know we're in the AJAX context I think we should simply instantiate an AJAX_Runner as before rather than use the Plugin_Request_Utility methods here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Then what is the point of Plugin_Request_Utility::get_runner(); then. Why not just create a AJAX_Runner here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1 to what @jjgrainger said, this is changing the logic from what it was before. We should create an AJAX_Runner here, not call Plugin_Request_Utility::initialize_runner() and Plugin_Request_Utility::get_runner(), as that also runs other logic.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment on lines +244 to +247
wp_send_json_error(
new WP_Error( 'invalid-runner', __( 'AJAX Runner was not initialized correctly.', 'plugin-check' ) ),
500
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similar to my other comment, we may want to return the WP_Error rather than send the JSON response here. (see #97 (comment))

Alternatively, we could throw an Exception instead and call get_runner in the try/catch block?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is just the same logic that was there before. Why change it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See https://github.com/10up/plugin-check/pull/157/files#r1180537801, the utility methods should have the responsibility of what their name is, not return a JSON response. For example this method should return the runner, or throw an exception if there is an error. The AJAX callbacks can then use try { ... } catch and return a JSON error response in case of an exception.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we do that, that could mean repeated the same code every time this method is called. I am trying avoid lots of boilerplate code here.

Copy link
Copy Markdown
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.

@spacedmonkey Moving this logic to reusable methods is a great idea, however I agree with @jjgrainger that we should decouple those methods from sending JSON response data. The methods should just return what they're named for, or throw an exception if something goes wrong.

The AJAX callbacks can then catch the exception and return it as a JSON error response.

Comment thread includes/Admin/Admin_AJAX.php Outdated
public function set_up_environment() {
// Verify the nonce before continuing.
$valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$this->validate_request();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Moving the logic into reusable functions makes sense, but to @jjgrainger's point we shouldn't couple the new utility methods to send JSON response data. The response should be handled separately by the AJAX callbacks, the new utility functions should rather throw an Exception or return WP_Error that can then be returned as JSON.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment on lines +238 to +239
Plugin_Request_Utility::initialize_runner();
$runner = Plugin_Request_Utility::get_runner();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1 to what @jjgrainger said, this is changing the logic from what it was before. We should create an AJAX_Runner here, not call Plugin_Request_Utility::initialize_runner() and Plugin_Request_Utility::get_runner(), as that also runs other logic.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment on lines +244 to +247
wp_send_json_error(
new WP_Error( 'invalid-runner', __( 'AJAX Runner was not initialized correctly.', 'plugin-check' ) ),
500
);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See https://github.com/10up/plugin-check/pull/157/files#r1180537801, the utility methods should have the responsibility of what their name is, not return a JSON response. For example this method should return the runner, or throw an exception if there is an error. The AJAX callbacks can then use try { ... } catch and return a JSON error response in case of an exception.

Comment thread includes/Admin/Admin_AJAX.php Outdated
if ( is_wp_error( $valid_request ) ) {
wp_send_json_error( $valid_request, 403 );
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See above. This method shouldn't be necessary at all. I would suggest we rather modify the verify_request() method to throw an exception instead of returning WP_Error. That way we can catch a potential exception with the same code that we would use also for the other methods, like get_runner() above.

Comment thread includes/Admin/Admin_AJAX.php Outdated
$runner = Plugin_Request_Utility::get_runner();

if ( is_null( $runner ) ) {
$runner = new AJAX_Runner();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@felixarntz @jjgrainger By doing this, should we not also be calling the prepare method on this runner?

Copy link
Copy Markdown
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 almost good to go, however one thing needs to be addressed.


if ( ! ( $runner instanceof AJAX_Runner ) ) {
$runner = new AJAX_Runner();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a functional change we shouldn't make: This method should only instantiate a new AJAX_Runner if Plugin_Request_Utility::get_runner() returns null. Otherwise, if the runner returned from that method itself is not an AJAX_Runner, we should throw an exception and then the underlying AJAX callback should terminate with a JSON error, similar to how that works today.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why would we want to do that? If it returns null, it is okay to create a new runner right? If get_runner returns a CLI_Runner, then error? Why error here? What is the benefit of erroring when we can just create an instance?

Copy link
Copy Markdown
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @spacedmonkey, Can we make util function for filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) also as it used four time in this file also I left some feedbacks.

);
}


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change

Remove extra line.

* @return array
*/
protected function get_checks() {
$checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar function present in AJAX_Runner::get_check_slugs_param()

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.

4 participants