Skip to main content

add-static-field

effort: 4m
configurable
pro+

Warns when a class that mixes in, extends or implements a configured other class is missing a required static field.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set entries (default is empty) to configure the list of entries for usages to ban.

Each entry is an object with 4 fields (type, name, fields and severity):

  • Set type to configure the relationship type (can be either with, implements or extends).
  • Set name to configure the class name.
  • Set fields to configure the list of static fields.
  • Set severity (optional) to override default severity for the given entry.
analysis_options.yaml
dcm:
rules:
- add-static-field:
entries:
- type: with
name: MyMixin
fields: ['id', 'value']
- type: implements
name: MyInterface
fields: ['anotherField']
- type: extends
name: MySuperclass
fields: ['someField']

Example

❌ Bad:

// LINT: This class mixes in MyMixin and is expected to have static fields: id, value.
// Try adding these fields.
class MixinTarget with MyMixin {}

// LINT: This class implements MyInterface and is expected to have static fields: anotherField.
// Try adding these fields.
class InterfaceTarget implements MyInterface {}

// LINT: This class extends MySuperclass and is expected to have static fields: someField.
// Try adding these fields.
class ParentTarget extends MySuperclass {}

✅ Good:

class MixinTarget with MyMixin {
static final id = 0;
static final value = 1;
}

class InterfaceTarget implements MyInterface {
static final anotherField = 2;
}

class ParentTarget extends MySuperclass {
static final someField = 3;
}