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

Add Core and Extra sniffs #382

Merged
merged 2 commits into from
May 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions WordPress-Core/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,26 @@
<severity>0</severity>
</rule>

<!-- Added for lowercase "as" keyword check -->
<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration">
<properties>
<property name="requiredSpacesAfterOpen" value="1"/>
<property name="requiredSpacesBeforeClose" value="1"/>
</properties>
</rule>
<rule ref="Generic.PHP.LowerCaseKeyword"/>

<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n"/>
</properties>
</rule>


<rule ref="Generic.Files.EndFileNewline"/>

<!-- https://make.wordpress.org/core/handbook/coding-standards/php/#naming-conventions -->
<rule ref="Generic.Files.LowercasedFilename"/>

<!-- https://make.wordpress.org/core/handbook/coding-standards/php/#space-usage -->
<rule ref="Generic.Formatting.SpaceAfterCast"/>

<!-- https://make.wordpress.org/core/handbook/coding-standards/php/#brace-style -->
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>

<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1" />
Expand Down
16 changes: 16 additions & 0 deletions WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
<ruleset name="WordPress Extra">
<description>Best practices beyond core WordPress Coding Standards</description>

<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Generic.Functions.CallTimePassByReference"/>
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
<rule ref="Generic.Formatting.MultipleStatementAlignment"/>
Copy link
Member

Choose a reason for hiding this comment

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

I really don't think we should include MultipleStatementAlignment. It's really annoying, for instance:

class-https-resource-proxy_php_-wordpress-develop-___projects_vvv_www_wordpress-develop

Copy link
Member

Choose a reason for hiding this comment

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

I removed it previously: 9f6854d

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, MultipleStatementAlignment is a good practice within reason, but the sniff isn't refined enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

In this case, I'd optionally put $redirect_vars_pairs above line 129, and align the = out.

In other cases, just adding a clear line above 134 would do - that the separates the predefined data in $redirect_vars, and the variable that's only populated from the foreach.

It is part of the standard.

However, I'm open to removing it if you both still insist.

Copy link
Member

Choose a reason for hiding this comment

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

It is part of the standard.

Kinda. It says “spaces can be used mid-line for alignment”. It doesn't say that that alignment is required.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably have in the README a list of other sniffs that users may like to opt-in to, such as this, but which are perhaps even more opinionated than Extra already is.

<rule ref="Generic.CodeAnalysis.EmptyStatement" />
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

Because of the way WordPress's filters/actions API works, UnusedFunctionParameter will return a lot of false positives. I often need to hook into a filter and need the 1st and 5th parameters, for example, but you still have to specify the 2nd-4th parameters (or use func_get_arg(), which is slow).

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I'll remove it. Do we want a custom sniff that does a similar check, but doesn't report unused (e.g. 2nd-4th) variables if a later one (e.g. 5th) is used?

Also, this sniff only gives warnings, not errors, so it may be permissible to keep this sniff in.

Copy link
Member

Choose a reason for hiding this comment

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

My standard practice has been to just unset( $unused_var ), and this makes PhpStorm happy, and I assume it would make this sniff happy as well. Having a sniff that skips warning if the last argument is used but the intermediate ones are not used, is a great idea. This seems like it should be a core property on Generic.CodeAnalysis.UnusedFunctionParameter.

Copy link
Member

Choose a reason for hiding this comment

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

I've opened an upstream PR which addresses this: squizlabs/PHP_CodeSniffer#1318

<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
<rule ref="Generic.Classes.DuplicateClassName"/>
<rule ref="Generic.Strings.UnnecessaryStringConcat"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

I will probably disable UnnecessaryStringConcat in my custom ruleset if it gets added, because sometimes I use concatenation to wrap a long string (which that sniff will report as an error).


<rule ref="WordPress-Core"/>

<rule ref="WordPress.XSS.EscapeOutput"/>
Expand Down