drupal-code-generator
drupal-code-generator copied to clipboard
[3.x] Decide on coding standard for generated code
Drupal 10 will require PHP 8.1 which means we can make use of all new PHP features available at this moment. However, Drupal coding standards weren't updated yet to reflect this change and will unlikely be updated in the foreseeable future. Furthermore, they don't cover PHP 7 features yet. So far, all code generated by DCG follows Drupal core coding standards. Which means if something is not covered in Drupal coding standards it uses Drupal core code base as a referencing point. Nowadays, the generated code looks very outdated as most of Drupal core code is written using PHP 5 syntax only.
For custom projects it's not a problem to establish own standards for features that are missing in the Drupal coding standards. Example: https://github.com/Chi-teck/drupal-coder-extension
However, for generated code that is going to be part of other projects it needs sort of community approval.
Here is an example of the currently generated code. https://github.com/Chi-teck/drupal-code-generator/blob/3.0.0-alpha2/tests/functional/Generator/_controller/src/Controller/FooController.php
The following example represents proposed updates.
<?php declare(strict_types = 1);
namespace Drupal\example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns responses for Example routes.
*/
final class ExampleController extends ControllerBase {
/**
* The controller constructor.
*/
public function __construct(
private readonly EntityTypeManagerInterface $entity_type_manager,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new self($container->get('entity_type.manager'));
}
/**
* Builds the response.
*/
public function build(): array {
$build['content'] = [
'#type' => 'item',
'#markup' => $this->t('It works!'),
];
return $build;
}
}
Here is an overview of the changes:
- Declare strict types. #3250827
- Use
final
/private
/self
keywords to define non-abstract classes. #3019332 - Use constructor property promotion. #3278431
- Use type hints wherever it is possible. #3154762
- Omit
@var
,@param
,@return
tags for strictly typed values. #3238192 - Mark class properties as
readonly
if they are not ever updated. - Use trailing commas for multiline arrays, methods and annotations.
Great summary 👍 Moreover PHP 8.2 (coming in November) will be supported by 10.0 so makes sense to start discuss its features too
I support generating code that takes advantage of all those new language features.
I would be cautious with introducing final
and private
because if they end up in the wrong places then they just make life difficult for site builders. But everything else would be really nice to have.
Laravel embracing new PHP features when generating code https://laravel-news.com/laravel-10-type-declarations
I think, this can be closed. I've already updated about half of the generators. Example: https://github.com/Chi-teck/drupal-code-generator/blob/3.x/tests/functional/Generator/_controller/src/Controller/FooController.php
Hopefully the remaining generators will be updated soon. That's the only blocker for DCG 3.0.
Note that DCG 3.x allows modifying generated assets through event subscribers. So modules will be able to alter the assets as needed. I played a bit with rector, php-cs-fixer and regexp. It worked well for me.