Automattic / VIP-Coding-Standards

PHP_CodeSniffer ruleset to enforce WordPress VIP coding standards.
https://wpvip.com/documentation/how-to-install-php-code-sniffer-for-wordpress-com-vip/
Other
236 stars 40 forks source link

Review the WordPressVIPMinimum.Hooks.AlwaysReturnInFilter sniff #520

Open jrfnl opened 4 years ago

jrfnl commented 4 years ago

Review the WordPressVIPMinimum.Hooks.AlwaysReturnInFilter sniff for the following in as far as relevant to that sniff:

Other:

Sniff basics, but changes need to be lined up for next major release:

Once PHPCS/PHPCSUtils supports this:

jrfnl commented 4 years ago

I've done an initial review of this sniff and the PreGetPosts sniff while fixing #358.

These sniffs can use quite some love. The age of the sniff is clearly showing through as code past the PHP 4 age is barely taken into account. Time to bring it into the modern age :smile:

Findings:

Recommendations

All in all, this sniff (and the PreGetPosts sniff) basically need a complete rewrite with modern code in mind. This is not an indictment of the principle of the sniff, it is just an old sniff which hasn't kept up with the changes in the PHP landscape around it.

Timeline

I'd recommend moving both this issue as well as the PreGetPosts issue out of the 2.2.0 milestone and scheduling the rewrite for a later release when the above mentioned abstract will be available.

Note: this abstract is currently not available in PHPCSUtils and still needs work before it can be pulled to PHPCSUtils.

Pertinent bugs can be patched up with quick fixes in the mean time if and when reported.

jrfnl commented 4 years ago

Been thinking about this sniff some more.

I think the wider WP community can benefit from this sniff, so once it is improved, this sniff would be a very strong candidate for moving to WPCS.

I also think that a generic "function call with callback parameter" abstract sniff in PHPCSUtils may not be a bad idea. This abstract could then be the base for this sniff.

Future scope for this sniff/the abstract (after the initial round of improvements):

jrfnl commented 1 year ago

From #783 - another thing which should be improved:

Looking at the sniff, I believe the intention was to only flag the "return outside condition missing" when not all control structure paths had a return statement, but this was never really properly checked as the only control structures taken into account are if control structures.

I believe it would be good to improve the sniff to handle more control structures (switch, while etc) and to not throw the "return outside condition missing" error if all possible paths have a return statement ... .