use_class_prefix
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.
many_lints: rules: use_class_prefix: entries: - type: Repository prefix: Dbrules: use_class_prefix: entries: - type: Repository prefix: Dbpackage: 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 {} // LINTabstract class Repository {}
class DbUserRepository implements Repository {}Examples
Section titled “Examples”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: Dbabstract class Repository {}
// Don't — the variant is buried at the end, or missingclass UserRepository implements Repository {} // LINTclass RepositoryInMemory implements Repository {} // LINT
// Doclass DbUserRepository implements Repository {}class DbCachedUserRepository implements Repository {}Mark test doubles
Section titled “Mark test doubles”A prefix on a fake makes it obvious in a test file which collaborator is real:
- type: PaymentGateway prefix: Mockabstract class PaymentGateway {}
// Don'tclass FakePaymentGateway implements PaymentGateway {} // LINT
// Doclass MockPaymentGateway implements PaymentGateway {}Leaving private classes alone
Section titled “Leaving private classes alone”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 tooabstract class Repository {}abstract class PaymentGateway {}
// Not reported — private, and this entry ignores private classesclass _CachedUsers implements Repository {}
// Reported — this entry overrides the rule-wide defaultclass _StripeGateway implements PaymentGateway {} // LINTAn indirect ancestor still matches
Section titled “An indirect ancestor still matches”- type: Repository prefix: Dbabstract class Repository {}abstract class CachingRepository implements Repository {}
// Don't — reaches Repository through CachingRepositoryclass UserStore extends CachingRepository {} // LINT
// Doclass DbUserStore extends CachingRepository {}When a class matches several entries, the first one wins and it is reported once.
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.
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 |
Known limitations
Section titled “Known limitations”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.
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. To turn it off again:
rules: use_class_prefix: falseTo keep the rule on but skip certain paths, use per-rule exclude.
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.