Skip to content

use_class_prefix

v1.0.0WarningFixConfigurableClass Naming

Flags a class that derives from a type you name but does not carry the prefix you require. A quick fix prepends it. The mirror image of use_class_suffix.

Some conventions read better at the front: DbUserRepository and MockPaymentGateway put the distinguishing quality first, so every implementation of one interface sorts together and the variant is visible before the noun.

This rule is in no preset and reports nothing until you add entries:. Every example below shows the configuration that produces it.

analysis_options.yaml
many_lints:
rules:
use_class_prefix:
entries:
- type: Repository
prefix: Db

package: is optional. Omit it for a type you declare yourself — a local type has no package: URI to pin against. Add it when the name is ambiguous, so an unrelated Repository from another dependency does not match.

A type matches whether it is reached by extends, implements, with, or an indirect ancestor. The configured base type is never reported against itself.

With entries: [{type: Repository, prefix: Db}]:

abstract class Repository {}
class UserRepository implements Repository {} // LINT
abstract class Repository {}
class DbUserRepository implements Repository {}

Sort implementations of one interface together

Section titled “Sort implementations of one interface together”

The point of a prefix is that the backing store leads. Three implementations of Repository land next to each other in a file listing, and the call site says which one it got:

- type: Repository
prefix: Db
abstract class Repository {}
// Don't — the variant is buried at the end, or missing
class UserRepository implements Repository {} // LINT
class RepositoryInMemory implements Repository {} // LINT
// Do
class DbUserRepository implements Repository {}
class DbCachedUserRepository implements Repository {}

A prefix on a fake makes it obvious in a test file which collaborator is real:

- type: PaymentGateway
prefix: Mock
abstract class PaymentGateway {}
// Don't
class FakePaymentGateway implements PaymentGateway {} // LINT
// Do
class MockPaymentGateway implements PaymentGateway {}

ignore_private: true skips classes whose name starts with _. A private class cannot be referenced outside its library, so the naming convention buys less there.

rules:
use_class_prefix:
ignore_private: true # rule-wide default
entries:
- type: Repository
prefix: Db
- type: PaymentGateway
prefix: Mock
ignore_private: false # this one checks private classes too
abstract class Repository {}
abstract class PaymentGateway {}
// Not reported — private, and this entry ignores private classes
class _CachedUsers implements Repository {}
// Reported — this entry overrides the rule-wide default
class _StripeGateway implements PaymentGateway {} // LINT
- type: Repository
prefix: Db
abstract class Repository {}
abstract class CachingRepository implements Repository {}
// Don't — reaches Repository through CachingRepository
class UserStore extends CachingRepository {} // LINT
// Do
class DbUserStore extends CachingRepository {}

When a class matches several entries, the first one wins and it is reported once.

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.

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

Classes only. A mixin, enum or extension type is not checked, even when it implements a configured type. For those, reach for match_class_name_pattern.

This rule reads only prefix:. An entry carrying only suffix: gives it nothing to enforce — that key belongs to use_class_suffix. Configure each rule with its own entries.

A malformed entry is silent. An entry missing type or prefix is skipped, because a plugin cannot report problems against a YAML file. If an entry never fires, check both keys are present and non-empty.

This rule is in no preset, so it is off unless you enable it by name. 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.