andy-blum / drupal-smart-snippets

This extension adds rich language support for Drupal Hooks, Services, and Render Elements to VS Code.
https://marketplace.visualstudio.com/items?itemName=andrewdavidblum.drupal-smart-snippets
MIT License
21 stars 4 forks source link

Add types to hook declarations #14

Open lostcarpark opened 1 year ago

lostcarpark commented 1 year ago

Not sure if the metadata exists to produce this, but it would be great if the hook templates could include more types. An example might be:

function modulename_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
}

It would be great if this could be expanded to:

function modulename_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, string $form_id): void {
}
andy-blum commented 1 year ago

This may not be possible. The type definitions that are currently used come directly from the function definition. Some add parameter types to the docblock, but others do not. For example, hook_form_alter does not dictate that $form is an array or that $form_id is a string anywhere:

/**
 * ...
 *
 * @param $form
 *   Nested array of form elements that comprise the form.
 * @param $form_state
 *   The current state of the form. The arguments that
 *   \Drupal::formBuilder()->getForm() was originally called with are available
 *   in the array $form_state->getBuildInfo()['args'].
 * @param $form_id
 *   A string that is the unique ID of the form, set by
 *   Drupal\Core\Form\FormInterface::getFormId().
 *
 * @see hook_form_BASE_FORM_ID_alter()
 * @see hook_form_FORM_ID_alter()
 *
 * @ingroup form_api
 */
function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  ...
}