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

How to remove errors for specific comment or regex type string in a comment #3921

Open
kapilpaul opened this issue Nov 10, 2023 · 5 comments

Comments

@kapilpaul
Copy link

I've encountered a situation where I need to selectively exclude reported errors triggered by PHP_CodeSniffer based on specific inline comments within the code, such as // NOSONAR. However, after exploring the current functionalities available in PHP_CodeSniffer, I couldn't find a direct method to achieve this dynamic error exclusion based on these comments.

Use Case:
In my codebase, certain lines or blocks of code are intentionally marked with // NOSONAR comments to indicate that they should be excluded from the linting process. I'm seeking a method to instruct PHP_CodeSniffer to respect these exclusion markers and not report errors on these marked lines.

Proposed Solution:
I'm looking for a way to dynamically exclude or suppress specific reported errors that occur after // NOSONAR comments. I'm unsure if such functionality exists within PHP_CodeSniffer, but if it does not, I would like to propose an enhancement to introduce this capability.

Potential Implementation:
If not currently available, a potential solution could involve enhancing PHP_CodeSniffer to recognize specific inline comments and prevent the reporting of associated errors based on a configurable set of rules. This might involve introducing a setting or flag within the ruleset configuration to define which errors should be ignored when occurring after // NOSONAR comments.

Expected Impact:
Implementing such a feature would be immensely beneficial, providing a more flexible and tailored approach to handling reported errors in line with contextual markers in the code. It would empower users to manage reported errors more precisely, improving code quality while allowing for intentional exclusions based on specific markers.

Note:
While I'm uncertain about the current existence of such functionality or workaround within PHP_CodeSniffer, having a direct in-tool solution for this scenario would greatly enhance its functionality and cater to a common need for codebase management based on exclusion markers.

@jrfnl
Copy link
Contributor

jrfnl commented Nov 10, 2023

@kapilpaul This is not a bug, but a question/feature request.

PHPCS has its own ignore annotations which you can use to selectively ignore certain errors or ignore all errors for specific parts of the codebase. Please read the associated documentation to familiarize yourself with these: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-parts-of-a-file

These inline annotation also allow for comments, so you could possibly use something along the lines of the below to combine the annotations

// phpcs:ignore Stnd.Category.SniffName.ErroCode -- NOSONAR

Does that solve your problem ?

@kapilpaul
Copy link
Author

Hi @jrfnl, thanks for your valuable feedback. Yes, you can say feature request now. I was not sure, if it's available or not.

actually, I did not want to do that ignoring parts of a file or the inline annotations. Because I do have the NOSONAR in 100 places in my codebase.
So I was looking for something that I could define easily in the ruleset or maybe with a custom sniff. But there was no option/method to remove errors.
It will be great if we have something in the future.

@jrfnl
Copy link
Contributor

jrfnl commented Nov 10, 2023

@kapilpaul To be honest, if it's up to me, that's not a feature which will be added. Too little gain, too much maintenance overhead for something which is basically already supported, just not in the way you want.

@kapilpaul
Copy link
Author

@jrfnl gotcha. Instead, can we have something where we can override/remove the error from the custom sniff? or may be a filter before processing the final result?

@jrfnl
Copy link
Contributor

jrfnl commented Nov 10, 2023

@kapilpaul I'm not sure what you mean ? I presume you are already aware of the things you can do with a custom ruleset ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants