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

SideEffectsSniff with php 8.1 enum #3615

Closed ArthurKacprzak closed 2 years ago

ArthurKacprzak commented 2 years ago

Describe the bug Functions in a php Enum trigger SideEffectsSniff error

Code sample

enum tmp: int
{
    case one = 1;
    case two = 2;

    public function label(): string
    {
        return match ($this) {
            tmp::one => 'one',
            tmp::two => 'two',
        };
    }
}

Expected behavior An explanation of why I can't create a function in a php enum.

jrfnl commented 2 years ago

@ArthurKacprzak I cannot reproduce this with PHPCS 3.7.0, which is the first PHPCS version with syntax support for PHP 8.1.

Please upgrade to PHPCS 3.7.0

ArthurKacprzak commented 2 years ago

I use phpinsights with squizlabs/php_codesniffer 3.7.0.

Side effects: (PHP_CodeSniffer\Standards\PSR1\Sniffs\Files\SideEffectsSniff)
  /var/www/tmp.php:1: A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 23 and the first side effect is on line 8.
ArthurKacprzak commented 2 years ago

so the problem is probably insight, thank you.

gsherwood commented 2 years ago

It's likely that whatever tool you are using is not actually using version 3.7.0 of PHP_CodeSniffer. The error will appear on all PHP_CodeSniffer versions < 3.7.0 but I also can't replicate on 3.7.0.