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:
-
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.
-
Custom Configuration: You can configure PHP-CS-Fixer to ignore this specific rule for your enums. This can be done by customizing your
.php_csor.php-cs-fixer.phpconfiguration 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.
- 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
$thisin 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.