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

Sniff: move "missing unslashing" handling to callback function #2347

Merged
merged 1 commit into from
Aug 17, 2023

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Aug 17, 2023

As things were, the "missing unslashing" error was being thrown from the Sniff::is_sanitized() method, based on whether the $require_unslash parameter was set to true or false.

In my opinion, it should not be the responsibility of utility functions to throw errors/warning, but always of the originating sniff.

This commit replaces the boolean $require_unslash parameter for the Sniff::is_sanitized() method to a nullable callable $unslash_callback parameter.

Instead of letting the Sniff::is_sanitized() method throw the error/warning, the callback will now be called and as the originating sniff passes the callback, the originating sniff can now handle the throwing of the error/warning.

To remove any presumptions of the callback being passed, being in the same context as the utility method/originating sniff and having access to the $phpcsFile via a property, the callback will receive both the $phpcsFile as well as the $stackPtr as parameters.

The "missing unslash" check can still be skipped by not passing the parameter or by passing anything non-callable. This maintains the original behaviour for the method for the default value/false case.

The Sniff::add_unslash_error() method can with this change now be moved to the ValidatedSanitizedInput sniff and that sniff will now have full control over the error message and error code.

Note: the callback is called using call_user_func() instead of $callable to allow some tolerance for the partially supported callables, which were deprecated in PHP 8.2. While WPCS itself does not use these, the ValidatedSanitizedInputSniff is one of the exceptions which has not been made final, so an extending class could use one of the partially supported callables and the Sniff::is_sanitized() method should not be the reason that doesn't work.

As things were, the "missing unslashing" error was being thrown from the `Sniff::is_sanitized()` method, based on whether the `$require_unslash` parameter was set to `true` or `false`.

In my opinion, it should not be the responsibility of utility functions to throw errors/warning, but always of the originating sniff.

This commit replaces the boolean `$require_unslash` parameter for the `Sniff::is_sanitized()` method to a nullable callable `$unslash_callback` parameter.

Instead of letting the `Sniff::is_sanitized()` method throw the error/warning, the callback will now be called and as the originating sniff passes the callback, the originating sniff can now handle the throwing of the error/warning.

To remove any presumptions of the callback being passed, being in the same context as the utility method/originating sniff and having access to the `$phpcsFile` via a property, the callback will receive both the `$phpcsFile` as well as the `$stackPtr` as parameters.

The "missing unslash" check can still be skipped by not passing the parameter or by passing anything non-callable. This maintains the original behaviour for the method for the default value/`false` case.

The `Sniff::add_unslash_error()` method can with this change now be moved to the `ValidatedSanitizedInput` sniff and that sniff will now have full control over the error message and error code.

Note: the callback is called using `call_user_func()` instead of `$callable` to allow some tolerance for the _partially supported callables_, which were deprecated in PHP 8.2. While WPCS itself does not use these, the `ValidatedSanitizedInputSniff` is one of the exceptions which has not been made `final`, so an extending class _could_ use one of the _partially supported callables_ and the `Sniff::is_sanitized()` method should not be the reason that doesn't work.
@jrfnl
Copy link
Member Author

jrfnl commented Aug 17, 2023

Looks like we may need to tweak the codecov (patch) config a little more. As project is ➕ , I don't think it should be a blocker for now though.

Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

Lovely change ✅

@GaryJones GaryJones merged commit e31c0cb into develop Aug 17, 2023
38 of 39 checks passed
@GaryJones GaryJones deleted the feature/move-unslash-handling-to-sniff branch August 17, 2023 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants