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

"$this" is not allowed in enum methods, but it should be #3827

Closed mhgtlc closed 1 year ago

mhgtlc commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

Code sample

enum Status
{
    case DRAFT;
    case PUBLISHED;
    case ARCHIVED;

    public function color(): string
    {
        return match($this) 
        {
            Status::DRAFT => 'grey',   
            Status::PUBLISHED => 'green',   
            Status::ARCHIVED => 'red',   
        };
    }
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    ...
    <config name="testVersion" value="8.1-"/>
    <rule ref="PHPCompatibility"/>
    ...
</ruleset>

To reproduce Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
    ----------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------
    10 | ERROR | "$this" can no longer be used in a plain function or
     |       | method since PHP 7.1.
    ----------------------------------------------------------------------

Expected behavior $this should be allowed inside enum methods, please take a look at this example: https://stitcher.io/blog/php-enums

Versions (please complete the following information):

jrfnl commented 1 year ago

Closing as reported to the wrong repo.

Please report bugs for externally maintained sniffs to the appropriate external standard repository (not here).

P.S.: I happen to know this has already been fixed in the PHPCompatibility develop branch.

golliholzland commented 3 months ago

@jrfnl could you include the commit or version to use, in order to fix this

jrfnl commented 3 months ago

@golliholzland As stated before: this question is about a sniff from another - externally maintained - project. Check the repo of that project and don't ask here.