squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

Ignoring "deprecated" errors in class file #827

Closed mvar closed 8 years ago

mvar commented 8 years ago

Code:

<?php

namespace Foo;

// @codingStandardsIgnoreStart
@trigger_error(
    'The XXX class is deprecated and will be removed in 2.0. Use YYY instead.',
    E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd

use Bar\YYY;

/**
 * @deprecated Will be removed in 2.0. Use the YYY instead.
 */
class XXX extends YYY
{
}

Code Sniffer generates warnings for trigger_error () even when it is wrapped in @codingStandardsIgnore. Is there a way to silent these warnings?

gsherwood commented 8 years ago

I'm assuming you are talking about the side effects warning from PSR1/2. If that is the case, the error is reported on line 1 as it applies to the entire file and not a specific line. The only way to mute that error is to ignore the entire file or exclude it using the --ignore command line argument.

The reason for this is that wrapping code with the ignore start/end comments just mutes errors reported on those lines. It doesn't make those parts of the file invisible to checking.

To ignore the entire file, put the comment // @codingStandardsIgnoreFile somewhere near the top of your file (anywhere is actually file, but performance is better the higher you place it in the file).

Does that help?

mvar commented 8 years ago

Thanks for reply. I got it! I don't want to ignore whole file (because I have a lot of deprecated classes). I think it's better to skip Code Sniffer warnings (-n).

Lets talk a little bit more about deprecated classes. An idea to trigger error I took from Symfony source code. I think that's really useful for end user. Sadly code like this does not comply PSR2. Mmm.. Maybe I need to address my issue to php-fig?

gsherwood commented 8 years ago

I think it's better to skip Code Sniffer warnings (-n).

Yes, also a good option.

Sadly code like this does not comply PSR2. Mmm.. Maybe I need to address my issue to php-fig?

That might be a good idea. I'm not aware of any existing PHP-FIG discussions around deprecation.

I'll close this issue now that you know what is going on. Thanks for getting back to me.

mvar commented 8 years ago

Thanks for answers.

If you are interested in this topic you can follow issue on php-fig/fig-standards#714