Open jrfnl opened 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:
function &foo()
- would not be examined as the sniff thinks the reference is for the first parameter being passed by reference. (false negatives)function foo( array &$param )
-, would be examined, even though it doesn't need to be. (false positives)add_filter( 'filter', 'prefix' . $type . 'function_name' )
would lead the sniff to search for a function called function_name
.
If that function exists, this could lead to false positives, but independently of that, the sniff should bow out from searching any further when a compound name is encountered as it will not be able to reliably match it. (efficiency)$this
, self/static::class
, get_class()
and some more variants should be done before walking the whole class.MyNamespace\add_filter()
being seen as the global add_filter()
function, but also for the callbacks.$myObject->add_filter()
will also trigger the sniff. while again, this is not the global WP function.end()
and reset()
on large nested arrays.AbstractFunctionParametersSniff
which is to be added to PHPCSUtils.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.
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.
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.
// This will automatically removed falsey values from an array.
add_filter( 'filter_an_array', 'array_filter' );
add_filter( 'filter_boolean', '__return_false' );
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 areif
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 areturn
statement ... .
Review the
WordPressVIPMinimum.Hooks.AlwaysReturnInFilter
sniff for the following in as far as relevant to that sniff:list
statements::class
use function/const
Other:
Sniff basics, but changes need to be lined up for next major release:
public
properties (#234)Once PHPCS/PHPCSUtils supports this:
match
expressions