use_class_prefix
Flags classes that derive from a type you configure but don’t carry the name prefix you require. The mirror image of use_class_suffix.
This rule reports nothing until you configure it. It enforces your naming convention, not a built-in one.
Why use this rule
Section titled “Why use this rule”Some conventions read better as a prefix than a suffix: DbUserRepository and MockPaymentGateway put the distinguishing quality first, so implementations of the same interface sort together alphabetically and the variant is visible before the noun.
A type matches whether it is reached by extends, implements, with, or an indirect ancestor. The configured base type is never reported against itself.
abstract class Repository {}
// With an entry requiring the 'Db' prefix for implementations of Repository:class UserRepository implements Repository {} // LINTabstract class Repository {}
class DbUserRepository implements Repository {}Quick fix
Section titled “Quick fix”The fix prepends the required prefix, and repairs a near-miss rather than stacking onto it. A same-named unnamed constructor is renamed along with the class, so the result still compiles.
Turning this rule off
Section titled “Turning this rule off”This rule is in no preset, so it is off unless you enable it by name:
rules: use_class_prefix: trueTo turn it off again:
rules: use_class_prefix: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: use_class_prefix: ignore_private: true # rule-wide default, optional entries: - type: Repository package: my_data # optional; omit to match any package prefix: Dbrules: use_class_prefix: ignore_private: true # rule-wide default, optional entries: - type: Repository package: my_data # optional; omit to match any package prefix: Db| Option | Type | Default | Description |
|---|---|---|---|
entries |
list of maps | [] |
The base types to track. With none, the rule reports nothing |
ignore_private |
bool | false |
Rule-wide default for skipping classes whose name starts with _ |
Each entry accepts:
| Key | Type | Required | Description |
|---|---|---|---|
type |
string | yes | Name of the base type, e.g. Repository |
prefix |
string | yes | The prefix subtypes must start with |
package |
string | no | Package declaring type. Omit to match a type of that name from any library, including your own package |
ignore_private |
bool | no | Overrides the rule-wide ignore_private for this entry |
The two rules read different per-entry keys, so an entry carrying only suffix: gives this rule nothing to enforce, and vice versa. Configure each rule with its own entries.
When a class matches several entries, the first one wins and it is reported once. An entry missing type or prefix is skipped; because a plugin cannot report problems against a YAML file, a malformed entry degrades quietly rather than failing analysis.
Related rules
Section titled “Related rules”match_class_name_pattern— Match class names against a regular expression.use_class_suffix— Require a name suffix for classes deriving from a configured type.prefer_correct_identifier_length— Keep identifier length within bounds.prefer_correct_type_name— Keep type names within a sensible length and correctly capitalised.