Procedural files¶
Rules for the procedural files of a module. They take the module name from the file name, the part
before the first dot. All of them report only in .module and .install files, except
drupal/global-variable, which checks every file.
drupal/const-prefix¶
- Level: warning
- Fix: none
- Standard: Constants
- Ports:
Drupal.Semantics.ConstantName.ConstConstantStart
Module-defined constant names should also be prefixed by an uppercase spelling of the module that defines them.
Reports:
- a
constconstant at the top level of the file, or afternamespace Foo;, whose name doesn't start with the upper-case module name and an underscore. When one statement declares several constants, the rule checks the first.
Not reported:
- a
constinside a bracednamespace Foo { }. That skip doesn't apply todefine().
It checks the prefix the same way as drupal/constant-prefix.
Example
// Reported.
const LIMIT = 10;
// Fine.
const EXAMPLE_LIMIT = 10;
Compared with Coder
- The rule needs the underscore after the module name. Coder 9 only tests that the name starts
with the upper-case module name, so for a module named
mymodit acceptsMYMODULE_X.
drupal/constant-prefix¶
- Level: warning
- Fix: none
- Standard: Constants
- Ports:
Drupal.Semantics.ConstantName.ConstantStart
Module-defined constant names should also be prefixed by an uppercase spelling of the module that defines them.
Reports:
- a
define()constant whose name doesn't start with the upper-case module name and an underscore. For a name built with., the rule checks the first string literal.
Not reported:
- a name that starts with anything other than a string literal, such as a variable.
The constants of every module share one global namespace, and the prefix keeps them apart.
Example
// Reported.
define('LIMIT', 10);
// Fine.
define('EXAMPLE_LIMIT', 10);
Compared with Coder
- The rule needs the underscore after the module name, as
drupal/const-prefixdoes. - The rule checks a
define()call at any depth: in a function, a method, a closure, anifor a bracednamespace Foo { }. Coder checks only the calls at the top level of the file. A constant fromdefine()is global wherever the call is. - The rule also reports
\define(), and adefine()with a comment before its first argument. Coder skips both. - In a name built with
., a first literal that is the start of the prefix, as in'MY' . 'MODULE_X'inmymodule.module, isn't reported, because the rest of the name can complete the prefix. Coder reports it.
drupal/empty-install-hook¶
- Level: error
- Fix: none
- Source: Coder's
Drupalstandard - Ports:
Drupal.Semantics.EmptyInstall.EmptyInstall
Coder asks not to keep an empty hook_install() or hook_uninstall().
Reports:
- an empty
hook_install()orhook_uninstall()body in an.installfile.
drupal/function-prefix¶
- Level: error
- Fix: none
- Standard: Functions and variables
- Ports:
Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
Functions should in addition have the grouping/module name as a prefix, to avoid name collisions between modules.
Reports:
- a function in a
.modulefile whose name doesn't start with the module name and an underscore, with an optional leading underscore.
Not reported:
- a name that starts with
template_preprocessortheme; - a function in an
.installfile; - a function declared inside a class, trait, enum or anonymous class, such as in a method body.
Example
// Reported.
function build_list(): array {
return [];
}
// Fine.
function example_build_list(): array {
return [];
}
drupal/global-variable¶
- Level: error
- Fix: none
- Standard: Global variables
- Ports:
Drupal.NamingConventions.ValidGlobal.GlobalUnderScore
If you need to define global variables, their name should start with a single underscore followed by the module/theme name and another underscore.
Reports:
- a variable in a
globalstatement that doesn't start with an underscore. Only the first underscore is checked, not the module name after it.
Not reported:
- the globals owned by Drupal core, such as
$base_urland$user.
Unlike the other rules on this page, the rule checks every file read by Mago, such as .inc files
and class methods in .php files.
drupal/install-hook-location¶
- Level: error
- Fix: none
- Source: Coder's
Drupalstandard - Ports:
Drupal.Semantics.InstallHooks.InstallHook
Coder asks for the install hooks to be in the module's .install file.
Reports:
- an implementation of
hook_install(),hook_uninstall(),hook_requirements(),hook_schema(),hook_enable()orhook_disable()in a.modulefile.
drupal/t-in-hook-schema¶
- Level: error
- Fix: none
- Source: Coder's
Drupalstandard - Ports:
Drupal.Semantics.TInHookSchema.TFound
Coder asks for no t() in hook_schema(). A schema description is developer documentation, so a
translation only adds work for translators.
Reports:
- a
t()call insidehook_schema()in an.installfile.
Example
// Reported.
function example_schema() {
$schema['example'] = [
'description' => t('Stores the examples.'),
'fields' => [],
];
return $schema;
}
// Fine.
function example_schema() {
$schema['example'] = [
'description' => 'Stores the examples.',
'fields' => [],
];
return $schema;
}