Refactor to remove repeated code.#157
Conversation
jjgrainger
left a comment
There was a problem hiding this comment.
Thanks @spacedmonkey looks good, left a couple of comments based on previous feedback in another PR
| 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(); |
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
I don't think those two things are the same. This does change any of the logic, just moves it around.
There was a problem hiding this comment.
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.
| Plugin_Request_Utility::initialize_runner(); | ||
| $runner = Plugin_Request_Utility::get_runner(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Then what is the point of Plugin_Request_Utility::get_runner(); then. Why not just create a AJAX_Runner here?
There was a problem hiding this comment.
+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.
| wp_send_json_error( | ||
| new WP_Error( 'invalid-runner', __( 'AJAX Runner was not initialized correctly.', 'plugin-check' ) ), | ||
| 500 | ||
| ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
This is just the same logic that was there before. Why change it?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
felixarntz
left a comment
There was a problem hiding this comment.
@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.
| 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(); |
There was a problem hiding this comment.
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.
| Plugin_Request_Utility::initialize_runner(); | ||
| $runner = Plugin_Request_Utility::get_runner(); |
There was a problem hiding this comment.
+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.
| wp_send_json_error( | ||
| new WP_Error( 'invalid-runner', __( 'AJAX Runner was not initialized correctly.', 'plugin-check' ) ), | ||
| 500 | ||
| ); |
There was a problem hiding this comment.
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.
| if ( is_wp_error( $valid_request ) ) { | ||
| wp_send_json_error( $valid_request, 403 ); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
| $runner = Plugin_Request_Utility::get_runner(); | ||
|
|
||
| if ( is_null( $runner ) ) { | ||
| $runner = new AJAX_Runner(); |
There was a problem hiding this comment.
@felixarntz @jjgrainger By doing this, should we not also be calling the prepare method on this runner?
felixarntz
left a comment
There was a problem hiding this comment.
This looks almost good to go, however one thing needs to be addressed.
|
|
||
| if ( ! ( $runner instanceof AJAX_Runner ) ) { | ||
| $runner = new AJAX_Runner(); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
mukeshpanchal27
left a comment
There was a problem hiding this comment.
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.
| ); | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Remove extra line.
| * @return array | ||
| */ | ||
| protected function get_checks() { | ||
| $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); |
There was a problem hiding this comment.
Similar function present in AJAX_Runner::get_check_slugs_param()
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
Credits
Props @username, @username2, ...
Checklist: