Add typehints to hook definitions

Created on 21 August 2021, over 3 years ago
Updated 27 February 2024, about 1 year ago

Problem/Motivation

Hooks are documented in *.api.php files. Developers tend to copy hooks from those files or from api.drupal.org which is bullt upon the code documentation. On the other hand drush generate and PhpStorm use these definitions as templates for generating hooks. Unfortunately, the hook definitions largely does not use typehints. As a result developers have to update the generated code manually or just do not use typehints at all.

Proposed resolution

Add proper typehints to hooks in all *.api.php files.
The changes would look like this.

- function hook_contextual_links_view_alter(&$element, $items) {
+ function hook_contextual_links_view_alter(array &$element, array $items): void {

Remaining tasks

Discuss, create a mega patch, review and commit it.

API changes

None.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
DocumentationΒ  β†’

Last updated 2 days ago

No maintainer
Created by

πŸ‡·πŸ‡ΊRussia Chi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • I think it's a good idea. I've been using type hints in my hook implementations, even if the hook itself doesn't specify types. I'm trying to use stricter coding standards.

  • Status changed to Closed: duplicate about 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    This is a duplicate as noted in the other issue.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Re-opening and moving to a child of πŸ“Œ [META] Add return types to hook implementations Active because there are a lot of hook definitions (in .api.php files) that were not updated in some of the wildcard issues, like all info hooks. I think we can use this to identify and fix any leftovers.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Postponing on πŸ“Œ Add void return to @ingroup entity_crud hook implementations Active and πŸ“Œ Add return type to remaining hook implementations Active . Added steps to reproduce. Added a draft MR with all the alter hooks updated.

  • Merge request !11170Alter hooks β†’ (Open) created by mstrelan
  • Pipeline finished with Failed
    about 1 month ago
    Total: 594s
    #420546
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    No longer postponed. Just realised that the IS is talking about both params and return types. I think initially we should limit this to return types and split off params to another ticket.

  • Pipeline finished with Success
    24 days ago
    Total: 460s
    #428004
  • πŸ‡¦πŸ‡ΊAustralia mstrelan
    $ grep -rn --include="*.api.php" -E 'function hook_.*\(.*\) \{' core
    core/lib/Drupal/Core/Extension/module.api.php:782:function hook_update_N(&$sandbox) {
    core/lib/Drupal/Core/Extension/module.api.php:784:  // function hook_update_N() {
    core/lib/Drupal/Core/Extension/module.api.php:892:function hook_post_update_NAME(&$sandbox) {
    core/lib/Drupal/Core/Extension/module.api.php:1303:function hook_update_requirements() {
    core/modules/shortcut/shortcut.api.php:40:function hook_shortcut_default_set(AccountInterface $account) {
    core/modules/jsonapi/jsonapi.api.php:276:function hook_jsonapi_entity_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
    core/modules/field/field.api.php:394:function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mode) {
    
  • Pipeline finished with Success
    24 days ago
    Total: 339s
    #428021
  • First commit to issue fork.
  • Pipeline finished with Success
    17 days ago
    Total: 6803s
    #433276
Production build 0.71.5 2024