Skip to content

New "Generic.PHP.DiscourageGoto" sniff #1664

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

Merged
merged 1 commit into from
Oct 26, 2017

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Sep 18, 2017

Usage of goto is often seen as a typical code smell.

Includes docs file and unit tests.

Closes #1662

Usage of goto is often seen as a typical code smell.

Includes docs file and unit tests.

Closes 1662
@michalbundyra
Copy link
Contributor

michalbundyra commented Sep 19, 2017

I like it and I was thinking about the same recently, but maybe we should go a bit more 'crazy' and create a bit more generic sniff - I mean ForbiddenTokens (as we have Generic.PHP.ForbiddenFunctions) and allow customize forbidden tokens.

I've just created PR #1665 to allow 'better' definition arrays in ruleset.
Then we can have the following configuration:

<property name="forbiddenTokens" type="array">
    <element key="T_GOTO" value="Using the 'goto' language construct is discouraged"/>
    <element key="T_GOTO_LABEL" value="Using the goto labels is discouraged"/>
    ...
</property>

So we can define tags and custom error messages.

@jrfnl @gsherwood What do you think?

@gmponos
Copy link
Contributor

gmponos commented Sep 19, 2017

If the current PR stays as is and you don't go in the road that @webimpress suggested I believe that you should add an option that you can configure if this will be an error or not. Same as ForbiddenFunction has.

@jrfnl
Copy link
Contributor Author

jrfnl commented Sep 19, 2017

If the current PR stays as is and you don't go in the road that @webimpress suggested I believe that you should add an option that you can configure if this will be an error or not. Same as ForbiddenFunction has.

@gmponos Actually, that option should be removed in the ForbiddenFunction sniff as this is already overrulable for every sniff from the ruleset without using custom properties:

<rule ref="Generic.PHP.DiscourageGoto">
	<type>error</type>
</rule>

The above is available in PHPCS 3.x. In PHPCS 2.x this had to be done per error code - see how I did this in the downstream PR to WPCS: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/1150/files#diff-9dbd3b63f5d2589b5eb1701e92893803R134

@jrfnl
Copy link
Contributor Author

jrfnl commented Sep 19, 2017

maybe we should go a bit more 'crazy' and create a bit more generic sniff

@webimpress I don't mind making the change, but would like @gsherwood's opinion about it first.

Personally, the only other token which I can think of which should probably be blanket forbidden for most projects is T_EVAL and there is already a dedicated sniff for that - Squiz.PHP.Eval.

Forbidding abstract classes or long arrays using a sniff like this seems to be overshooting. Those things can be auto-fixed and should have a dedicated sniff.

If a certain syntax is to be forbidden in a project because of PHP cross-version compatibility requirements, there is a better way and more comprehensive way to check for that available, i.e. the external PHPCompatibility standard: https://github.com/wimg/PHPCompatibility/

Could you give some examples of tokens you'd like to forbid using a more flexible sniff so I can understand the usecase better ?

@gsherwood gsherwood added this to the 3.2.0 milestone Sep 19, 2017
@michalbundyra
Copy link
Contributor

@jrfnl I do not have anything specific in mind at this time. I just thought that "generic" sniff could be really generic, and devs could set it up us they want. Then the same sniff could be used for goto and eval (btw I didn't know about the other sniff you mentioned 👍 ).
You have right, that some people can start using it incorrectly, instead of proper sniff which can fix the issue.
Let's see what @gsherwood thinks 😄

@gsherwood
Copy link
Member

My feeling is that a specific sniff for gotos is probably a good thing.

I've (fairly) recently written a custom coding standard to ban a lot of PHP syntax and I ended up writing custom sniffs for each one. The main reason is that I wanted to be sure they were working.

Writing a single sniff to ban any combination of tokens sounds easier, but you end up with it either getting more and more complex and people's requirements change, or it's just not very well tested. I tried this with the pattern matching sniff in PHPCS (AbstractPatternSniff) but it didn't allow for really good error messages, and didn't allow for any sort of auto-fixing, so I've moved away from it for the Squiz standard now.

A really really generic token-based sniff might also be okay, but I think this goto sniff should be left as is.

@gsherwood gsherwood merged commit ab83256 into squizlabs:master Oct 26, 2017
gsherwood added a commit that referenced this pull request Oct 26, 2017
@gsherwood
Copy link
Member

Thanks for this sniff.

@jrfnl jrfnl deleted the feature/new-discourage-goto-sniff branch October 27, 2017 00:18
@jrfnl
Copy link
Contributor Author

jrfnl commented Oct 27, 2017

You're welcome 😄

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.

4 participants