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

Unexpected behaviour when using phpcs:enable #2476

Closed davidkmenta closed 5 years ago

davidkmenta commented 5 years ago

I'm getting a weird error when I'm using partial ignoring.

Here is part of my code:

/**
 * @return iterable|BundleInterface[]
 */
public function registerBundles(): iterable
{
    // phpcs:disable Generic.PHP.ForbiddenFunctions
    $contents = require $this->getProjectDir() . '/config/bundles.php';
    // phpcs:enable

    foreach ($contents as $class => $envs) {
        if ($envs[$this->environment] ?? $envs['all'] ?? false) {
            yield new $class();
        }
    }
}

The error I'm getting:

29 | ERROR | [x] Expected 1 lines before "foreach", found 0.

If I use the // phpcs:ignore Generic.PHP.ForbiddenFunctions comment instead, everything is fine.

gsherwood commented 5 years ago

That doesn't look like an error message generated by one of the included sniffs. What coding standard are you using? Please also run PHPCS with the -s command line argument so it also indicates what sniff generated that error message.

davidkmenta commented 5 years ago

@gsherwood yop, you're right, my mistake. It's caused by the SlevomatCodingStandard.ControlStructures.ControlStructureSpacing.IncorrectLinesCountBeforeControlStructure sniff. But I can't say if it is an error in that sniff or it's a general problem in PHPCS...

gsherwood commented 5 years ago

But I can't say if it is an error in that sniff or it's a general problem in PHPCS...

It looks like a sniff problem. I've checked PHPCS and it is tokenising the content correctly (including assigning different tokens to the enable and disable lines) so the sniff itself is probably not taking those enable/disable comments into account.