Questions tagged [coding-standards]
Use it for questions about Drupal coding standards and the tools to follow them.
31 questions
2
votes
0
answers
26
views
PHPStan 2/bleeding edge: PHPDoc tag @var with type UserBundle is not subtype of type Drupal\user\UserInterface
I am trying to get ready for PHPStan 2 by turning on the bleeding edge features in phpstan.info:
includes:
# https://phpstan.org/blog/what-is-bleeding-edge
- phar://phpstan.phar/conf/...
3
votes
1
answer
53
views
How to use Drupal rector to get started converting annotations to attributes?
Drupal Rector has a rule for converting annotations to attributes.
I wanted to run this rule on a local repo of the Key module to get started adding attributes.
So I installed Drupal Rector (composer ...
-3
votes
2
answers
73
views
Is my code valid and how does it relate to the coding standards?
How does the below piece of code relate to the Drupal coding standards? Notice that this function is not returning anything.
$a = foo();
function foo() {
// …
drupal_func();
}
Somebody claims ...
0
votes
1
answer
68
views
Dependecies prefix coding standard
Running phpcs --standard=DrupalPractice in several modules *.info.yml file I get a warning message:
All dependencies must be prefixed with the project name
I've seen some modules that use the ...
4
votes
2
answers
87
views
Does a third-party module need to implement hook_help()?
After having spent hours reading through (very old) coding standards and best practices threads on drupal.org, and even trying to revive the Contrib Development Best Practices group on groups.drupal....
-2
votes
1
answer
243
views
PSR12, Drupal or DrupalPractice?
I want to respect coding standard in my new Drupal projects (for Drupal 8 and Drupal 9).
Using phpcs and phpcbf, I am wondering if I should use PSR12, Drupal, or DrupalPractice coding standards.
6
votes
1
answer
440
views
Is it ok for a plugin to extend a class in some other module without declaring the other module as a dependency?
I have a module with a few helpful services and field formatters. I have one field formatter that applies only to entity_reference_revisions fields and, as a result, "uses" and "extends&...
4
votes
2
answers
3k
views
What's the coding standard for class constants?
For general constants, Coding standards / Naming Conventions says that constants should always be all-uppercase, with underscores to separate words and that module-defined constant names should also ...
-3
votes
1
answer
156
views
How to reference a class with a use statement? [closed]
Inside a function in a controller class, I have the following:
$form_state = new \Drupal\Core\Form\FormState();
It works, and produces a new instance of a FormState.
Codesniffer is not happy with ...
1
vote
1
answer
438
views
phpcs: How to ignore Drupal's 80 char line length limit
I want to use PHP CodeSniffer with the default Drupal and DrupalPractice rulesets to check my custom modules. But I definitly don't want the 80 char line length limit, and phpcs is spamming the report ...
0
votes
1
answer
638
views
"Missing class doc comment" when using attribute on class
I recently updated PHP from 8.1 to 8.2.
This resulted in a bunch of deprecation notices about use of dynamic properties in PHPUnit tests.
So, based on the docs, I added the AllowDynamicProperties ...
4
votes
2
answers
573
views
Naming conventions for permissions
Looking through the Drupal coding conventions on Coding standards / Naming Conventions, I have seen module, variable, function, and many other areas but not for permission names.
Is there any ...
1
vote
0
answers
122
views
How do I document union type variables?
My module has a class with a union type member variable
/**
* This is a PHPv8 union type variable that can be a string OR null.
*
* @var string|null
*/
protected string|null $myVar;
However, when ...
6
votes
0
answers
70
views
How to override the typehint for entityTypeManager with a more specific one?
I'm often frustrated by entityTypeManager because when I load entities, the typehint for the return value of the load methods is EntityInterface, but I want it to be a more specific interface (which I ...
1
vote
0
answers
80
views
Interfaces for entities? [closed]
The Drupal code standards explains that I should use interfaces. And I always did that. But I never understood why.
because it allows more flexibility in extending code later
Why does an interface ...
3
votes
0
answers
75
views
Array type hints conflict between Drupal and Symfony
In an event subscriber, I have:
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
public static ...
2
votes
0
answers
192
views
Where to place the utility and internal functions for a custom module? [duplicate]
I have a custom module with some utility (can be used for other modules) and internal (can be used only for the module) functions inside, so I decide to place this functions in another file.
But ...
4
votes
2
answers
580
views
Can custom module function names contain characters between 7F and FF?
PHP defines valid function names as [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
I have already been bitten by Drupal name conflicts when using hook_form_alter clashing with my module name when it ...
0
votes
1
answer
61
views
Why are non-standard file extension used for PHP files?
Is there any reason why Drupal decided to use non-standard file extensions, for example node.install, node.module, simple.profile, case.test? Why doesn't Drupal use .php as extension for PHP files?
5
votes
2
answers
6k
views
Are there any naming conventions for helper functions in modules?
I'm writing a module and I need to create an auxiliary (helper) function, but I don't know which is the best name to my function.
Should I name my function like:
function _MODULE_my_function
or:
...
1
vote
2
answers
63
views
PAReview.sh error, Expected "switch (...) {\n"; found "switch(...) {\n"
In my theme's template.php file, I have a custom function as:
function _MYTHEME_content_grid($columns = 1) {
$class = FALSE;
switch($columns) {
// No sidebars, just content.
case 1:{
...
4
votes
1
answer
109
views
Breaking on to new lines?
If I have a long line of code that exceeds 80 characters, like this:
$last_response = '<strong>' . t('Last response: ') . '</strong>' . format_interval((time() - $last), 1) . t(' ago');
...
10
votes
3
answers
1k
views
How to fix "Hook implementations should not duplicate @param documentation."?
I am using PhpStorm to experiment with cleaning up my code in a custom module. For the following function, I'm getting this warning:
Hook implementations should not duplicate @param documentation.
...
5
votes
3
answers
622
views
Best practice for avoiding module / theme / profile name clashes?
We currently have our code managed such that each site has its own theme and install profile.
These have naturally evolved in such a way that the names of these items (and their directories) tend to ...
4
votes
1
answer
109
views
When to use a php function vs a drupal_ equivalent function?
I come from update my Coder module to the v1.3 and when I go to check one custom module I see the following message:
Line 71: in most cases, replace the string function with the drupal_
...
1
vote
1
answer
2k
views
/README.txt: UTF-8 Unicode (with BOM) English text, with CRLF line terminators
I have been working on a Drupal.org project and I want to make sure that pass all the coding standards. For some reason I got these problem with the README.txt encoding and I dont know how to solve it....
3
votes
2
answers
203
views
PHP switch and translations
Given a PHP switch statement, would it be a good (or bad) idea to use t() in case statements?
switch ($button_value) {
case t('Clear'):
// …
break;
case t('Search'):
// …
break;
}
2
votes
1
answer
681
views
Coding standards for long strings?
I've been looking for some official guidelines on how to format long strings in module code, but the closest thing I've found is an ambiguous section on indenting long SQL queries which suggests any ...
4
votes
1
answer
421
views
Where to write public, common, utility functions in Drupal?
At times you have to write little utility functions which will have a general scope (not restricted to the module you are working on) so that it can be called from anywhere in the project.
I know I ...
0
votes
1
answer
572
views
Parameter comment must be on the next line at position 1
I review my module code with http://pareview.sh and it returns an error:
Line #94 - ERROR | Parameter comment must be on the next line at position 1
/**
* Fetch node object properties. //<--- ...
2
votes
1
answer
319
views
Would PHPLint's advice be beneficial to our community?
I recently found this static PHP code analyzer called PHPLint. One of the things it can do, is look at code like this:
/**
* @return string
*/
function getText() {
return 5;
}
And complain that ...