ikappas / vscode-phpcs

PHP CodeSniffer for Visual Studio Code
MIT License
128 stars 56 forks source link

How to globally ignore a rule? #160

Open cagross opened 4 years ago

cagross commented 4 years ago

Currently, what is the proper way to ignore a phpcs rule in VSCode, without adding comments on a file-by-file basis? Is the current method still to create my own custom ruleset, and omit any rules I don't want to be checked, as in this Stack Overflow question? I've seen this previous issue, but it was closed when an alternate solution became available for the original poster.

The motivation for my post has to deal with comments. I frequently comment out code during testing, and when I do so, phpcs displays both a warning and an error:

Error:  Inline comments must end in full-stops, exclamation marks, or question marks
Warning:  This comment is 55% valid code; is this commented out code?

(screenshot).

To me, neither of these are very helpful, and more importantly, they just serve to obfuscate other, more important linting errors. The warning especially doesn't really add any utility, at least in my usage. Perhaps there is a separate way to manage these specific errors/warnings about comments that I'm not aware of?

Thanks.

TrekkerUK commented 4 years ago

Hi, I'd say the correct way is the Stack Overflow one - create your own coding standard ruleset and configuring it exactly how you need. What's in the file will depend on what you're already got set up - I presume you've got a standard set in the settings.json file?

You'll need to created a phpcs.xml file in the root of the workspace, containing something like below. Note that this file will overwrite any other settings you have, so the warnings/errors will be different unless it's changed you what you use now.

[Edited to attachment because the formatting was screwed]

phpcs.txt

cagross commented 4 years ago

Hey very sorry for the delay. The holidays came up, as well as some unrelated PHPCS issues, which prevented me from returning to this. I'm back.

I'd say the correct way is the Stack Overflow one - create your own coding standard ruleset and configuring it exactly how you need.

OK got it.

What's in the file will depend on what you're already got set up - I presume you've got a standard set in the settings.json file?

Actually, this project is configured so PHPCS uses a specific ruleset, one which enforces WordPress coding standards (this ruleset). I'd like to continue using that custom ruleset, but simply ignore the error and the warning I indicated in my original post. In light of that, what's the proper course of action? In my project's directory, this ruleset is located in the directory wp-coding-standards\wpcs. And indeed, a .XML file is located at wp-coding-standards\wpcs\WordPress\ruleset.xml. Would I make a copy of this file, assign it my own file name, add to it the three <rule> tags that you indicated in your .XML file (i.e. those with severity of 0), and save the file in the root of my project?

Separately, how do you handle this issue with PHPCS and comments? i.e. during development, sometimes I temporarily comment out some of my code. But when I do so, PHPCS flags each of those lines with both a warning and an error. It gets a bit confusing when I see so many errors/warnings for lines I know are benign. Maybe PHPCS isn't intended to be used during development, i.e. I should run it only when my code is ready to go to production?