EnumInstantiation
error native on by default PHP ≥ 8.1Group: Probable bugs
Use an enum case instead of instantiation.
Example
Finding:
php
<?php
enum Phase { case Fresh; } new Phase();Reported:
- line 2: Use an enum case instead of instantiation.
Detection
- Report new applied to a resolved backed or unbacked enum declaration.
- Report
new self()andnew static()written lexically inside an enum body (a method or constant expression of the enum itself): both name the enclosing enum. The keywords are matched case-insensitively. - Emit one finding per violating operation. Flow facts require a reachable path with no intervening mutation, escaped alias or unknown call involving the tracked value. When the proof is inconclusive, nothing is reported.
Exceptions (no report)
- Static case access and from/tryFrom calls are excluded.
self/staticinstantiation is not reported inside a closure or arrow function (it can be rebound to another class scope), inside an anonymous class or a trait (the keyword names that scope, or the class using the trait), or outside any class.- Exclude shadowed builtin symbols, unresolved required types, incomplete syntax and unsupported PHP versions.
Fix
No automatic fix. Choosing a repair requires runtime information, error-handling policy or a semantic decision.
PHP versions
Minimum PHP 8.1.
Configure
In custos.json:
json
{
"rules": {
"EnumInstantiation": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore EnumInstantiation