Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Decide on coding standard for generated code #87

Closed
Chi-teck opened this issue Jul 9, 2022 · 6 comments
Closed

[3.x] Decide on coding standard for generated code #87

Chi-teck opened this issue Jul 9, 2022 · 6 comments

Comments

@Chi-teck
Copy link
Owner

Chi-teck commented Jul 9, 2022

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:

  1. Declare strict types. #3250827
  2. Use final/private/self keywords to define non-abstract classes. #3019332
  3. Use constructor property promotion. #3278431
  4. Use type hints wherever it is possible. #3154762
  5. Omit @var, @param, @return tags for strictly typed values. #3238192
  6. Mark class properties as readonly if they are not ever updated.
  7. Use trailing commas for multiline arrays, methods and annotations.
@andypost
Copy link
Contributor

andypost commented Jul 9, 2022

Great summary 👍
Moreover PHP 8.2 (coming in November) will be supported by 10.0 so makes sense to start discuss its features too

@weitzman
Copy link
Contributor

weitzman commented Jul 9, 2022

I support generating code that takes advantage of all those new language features.

@pfrenssen
Copy link
Contributor

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.

@weitzman
Copy link
Contributor

Laravel embracing new PHP features when generating code https://laravel-news.com/laravel-10-type-declarations

@Chi-teck
Copy link
Owner Author

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.

@Chi-teck
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants