-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Improve sniff handling of modern PHP code #764
Comments
Another typical one which is currently not or hardly taken into account: |
Let's try & keep track of the reviews done: Globally checked (I might well have missed something) that sniffs allow correctly for:
N.B.: I don't "own" this ticket. Everyone is warmly invited to contribute, both in ideas of what to check for as well as doing the reviews! |
Just a check: would this handle things like namespace Custom_Namespace;
class My_Class {
public function get_posts() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'perm' => 'readable',
);
$query = new WP_Query( $args ); // This would trigger error, since it's not called from global namespace like \WP_Query.
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
}
wp_reset_postdata();
} else {
// no posts found
}
}
} Basically any WP class called within a namespace should be called from a global namespace |
Or should have a That's not actually what this issue is about. The current issue is about making the existing sniffs more resilient when they encounter modern PHP code, including prevent false positives/negatives. I.e. say there would be a sniff which would detect the use of There is currently no sniff available in WPCS which checks that WP native classes when used in a namespaced context are prefixed with a In the mean time, there might be a sniff in the |
Yeah, this was basically what I was thinking about, but I didn't want to create an issue if there is no need for it 🙂 Thanks for clarifying it out 👍 |
All sniffs have been reviewed and fixed to support modern PHP in as far as my imagination reached. There is still a known issue with resolving namespaces/use statements. That is expected to be fixed via a future PHPCSUtils version and abstract sniffs provided by PHPCSUtils. |
Speaking of add_action(
'populate_options',
- static function () use( $permalink_structure ) {
+ static function () use ( $permalink_structure ) {
add_option( 'permalink_structure', $permalink_structure );
}
); |
@westonruter Interesting. In my original tests that resulted in a duplicate error being thrown - both from Looks like the duplication exists only when there is too much whitespace, not when there is not enough. This is related to WordPress-Coding-Standards/WordPress-Core/ruleset.xml Lines 221 to 224 in 15c3037
I suppose we can remove that exclusion in favour of sometimes receiving duplicate messages. |
Turns out the exclusion is a little too aggresive. The `Generic.WhiteSpace.LanguageConstructSpacing` _will_ throw an error when there is too much whitespace, but not when there is no whitespace and the next thing is an open parenthesis. By removing the exclusion, an error will now (correctly) be thrown when there is no whitespace between a closure `use` keyword and the open parenthesis. It also means that where there is _too much_ whitespace between the keyword and the open parenthesis, two errors will be thrown, one from the `Generic.WhiteSpace.LanguageConstructSpacing` sniff and one from the `Squiz.Functions.MultiLineFunctionDeclaration` sniff. Well, so be it. As both sniffs expect the same thing, this shouldn't lead to fixer conflicts anyhow. See #764 (comment)
@westonruter See #2415 |
While WP still has a minimum requirement of PHP 5.2 and a lot of related projects code accordingly, there are also numerous projects out there which have set their plugin/theme minimum requirement at PHP 5.3 or higher.
Most sniffs currently don't take things like namespaces into account so the below code would trigger an error (even though the
fwrite
function in this case is namespaced).While this may not be the best example (or good practice in the first place), I would like to suggest we review code to see if we can make it more forward compatible with modern PHP code.
The text was updated successfully, but these errors were encountered: