Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Weirdtopia's avatar

PHP-CS-Fixer fails test due to usage of $this within Enum method

Hello,

This is the first time that I have posted on this site and i'm running across an issue with PHP-CS-Fixer failing a test due to the usage of "$this" within an Enum method.

PHP-CS-Fixer log:

 105 | ERROR | "$this" can no longer be used in a plain function or method
     |       | since PHP 7.1.
 109 | ERROR | "$this" can no longer be used in a plain function or method
     |       | since PHP 7.1.
 120 | ERROR | "$this" can no longer be used in a plain function or method
     |       | since PHP 7.1.
 120 | ERROR | "$this" can no longer be used in a plain function or method
     |       | since PHP 7.1.

The Enum contains methods like:

enum MethodConstraint :string {
   		    public function getMethod() :string {
        				return $this->value;
             }
			// ...
            public function getMethodValue($value) :string {
							return match($this) {
									// ..
							}
            }
}

When I run it, i don't get any errors or warning: https://3v4l.org/AZYG7

<?php


enum MethodConstraint :string {
    case A = "a";
    case B = "b";
    case C = "c";
    
    public function getMethod(): string {
        return $this->value;
    }
    
    public function getMethodSentence($value): string {
        return match ($value) {
            MethodConstraint::A => "This is an A char",
            MethodConstraint::B => "This is a B char",
            MethodConstraint::C => "This is a C char"
        };
    }
    
    public function getCharAndSentence(): string {
        return $this->getMethod()." ".$this->getMethodSentence($this);
    }
}


$aChar = MethodConstraint::A;


echo $aChar->getMethod().PHP_EOL;
echo $aChar->getMethodSentence(MethodConstraint::B).PHP_EOL;
echo $aChar->getCharAndSentence();

output:

Output for 8.2.9 - 8.2.20, 8.3.0 - 8.3.8
a
This is a B char
a This is an A char

Questions:

  1. Is PHP-CS-Fixer reporting a false-positive?
  2. Should Enum use $this? 2a. If not then should this be a class instead?
0 likes
1 reply
LaryAI's avatar
Level 58

Let's address your questions one by one and provide a solution to the issue you're facing with PHP-CS-Fixer.

1. Is PHP-CS-Fixer reporting a false-positive?

It seems like PHP-CS-Fixer is incorrectly flagging the usage of $this within your enum methods. Enums in PHP can indeed use $this to refer to the current instance, and this is perfectly valid in PHP 8.1 and later. The error message you're seeing is more relevant to older versions of PHP where $this was not allowed in certain contexts.

2. Should Enum use $this?

Yes, enums in PHP 8.1 and later can use $this within their methods. This is a valid use case and is part of the language's design.

2a. If not then should this be a class instead?

Since using $this in enums is valid, you do not need to convert your enum to a class. However, if you encounter limitations or specific requirements that enums cannot fulfill, you might consider using a class.

Solution

Given that PHP-CS-Fixer is likely reporting a false-positive, you can try the following steps to resolve the issue:

  1. Update PHP-CS-Fixer: Ensure you are using the latest version of PHP-CS-Fixer, as this might be a bug that has been fixed in a newer release.

  2. Custom Configuration: You can configure PHP-CS-Fixer to ignore this specific rule for your enums. This can be done by customizing your .php_cs or .php-cs-fixer.php configuration file.

Here is an example of how you might configure PHP-CS-Fixer to ignore this rule:

<?php

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__);

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR12' => true,
        // Add other rules you want to enforce
    ])
    ->setFinder($finder)
    ->setRiskyAllowed(true)
    ->setUsingCache(false);

If the specific rule causing the issue is identified, you can disable it directly in the configuration.

  1. Suppressing the Error: If updating PHP-CS-Fixer and custom configuration do not resolve the issue, you can suppress the error in your code using comments. However, this is not the most elegant solution and should be used as a last resort.

Example Code

Here is your enum with the correct usage of $this:

<?php

enum MethodConstraint: string {
    case A = "a";
    case B = "b";
    case C = "c";
    
    public function getMethod(): string {
        return $this->value;
    }
    
    public function getMethodSentence($value): string {
        return match ($value) {
            MethodConstraint::A => "This is an A char",
            MethodConstraint::B => "This is a B char",
            MethodConstraint::C => "This is a C char"
        };
    }
    
    public function getCharAndSentence(): string {
        return $this->getMethod() . " " . $this->getMethodSentence($this);
    }
}

$aChar = MethodConstraint::A;

echo $aChar->getMethod() . PHP_EOL;
echo $aChar->getMethodSentence(MethodConstraint::B) . PHP_EOL;
echo $aChar->getCharAndSentence();

Conclusion

  • Ensure you are using the latest version of PHP-CS-Fixer.
  • Customize your PHP-CS-Fixer configuration to ignore false positives.
  • Using $this in enums is valid in PHP 8.1 and later.

If the issue persists, consider reporting it to the PHP-CS-Fixer repository so that the maintainers can address it in future releases.

Please or to participate in this conversation.