Php_codesniffer: [Feature request] Allow selective ignoring with `phpcs:ignoreFile`

Created on 17 Feb 2018  路  5Comments  路  Source: squizlabs/PHP_CodeSniffer

Just ran into the fact that selective ignoring appears not to be supported for the phpcs:ignoreFile directive.
Feels counter-intuitive that that is the only annotation which does not support setting which sniffs/errorcodes should be ignored.

<?php
// phpcs:ignoreFile Standard.Category.Sniff -- this is not a core file
Documentation

Most helpful comment

I've added 2 notes to cover both points made here. The annotations could use their own dedicated wiki page with more documentation, but that's not something I have the time for at the moment.

All 5 comments

The phpcs:ignoreFile comment is only there to completely ignore a file. It's like writing phpcs:disable except that PHPCS uses that specific syntax to stop processing as soon as the comment is found, knowing that PHPCS cannot be enabled again later on with a phpcs:enable comment. If you put it at the top of the file, it shouldn't even waste time tokenizing it.

If this comment syntax allowed selective disabling, it would remove its usefulness as a performance shortcut to skip the file completely.

If you want to disable a specific sniff for the file, use the phpcs:disable syntax instead. That should do exactly what you need while continuing to process the file for other issues.

Fair enough.

It might be an idea to make this explicit in the documentation in the wiki. Something like "_Unlike the other phpcs: annotations, the phpcs:ignoreFile annotation does not allow for selective ignoring of sniffs by providing a sniffcode. It is intended as a performance shortcut._"

This has tripped up a few MediaWiki developers including myself (that ignoreFile disables everything). The downside to using phpcs:disable is that you also need a phpcs:enable comment at the bottom of the file - or is it safe to ignore re-enabling the sniff at the end of the file?

or is it safe to ignore re-enabling the sniff at the end of the file?

The phpcs:disable and phpcs:enable annotations only apply to the file they are in. So if you disable a sniff at the top of the file, it is still enabled for all other files. There is no need to re-enable it at the end.

I seem to have missed the last comment on this issue, so I haven't updated the docs yet. But I'll also put that note about them being localised to the file in there as well.

I've added 2 notes to cover both points made here. The annotations could use their own dedicated wiki page with more documentation, but that's not something I have the time for at the moment.

Was this page helpful?
0 / 5 - 0 ratings