Skip to content

use_class_prefix

v1.0.0WarningFixConfigurableClass Naming

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.

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 {} // LINT
abstract class Repository {}
class DbUserRepository implements Repository {}

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.

This rule is in no preset, so it is off unless you enable it by name:

many_lints.yaml
rules:
use_class_prefix: true

To turn it off again:

many_lints.yaml
rules:
use_class_prefix: false

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
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: 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.