phpstan-drupal
phpstan-drupal copied to clipboard
Fail if validate hander or submit handler does not exist
Feature request
Let's say I'm altering a form and I'm adding:
function MYMODULE_form_alter(array &$form, FormStateInterface $form_state) {
$form['#validate'][] = 'this_function_does_not_exist';
}
it would be nice if I got an error if this_function_does_not_exist() is not a function. Same for the submit hander:
function MYMODULE_form_alter(array &$form, FormStateInterface $form_state) {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'this_function_does_not_exist_either';
}
}
}
Would be nice if we throw an error if this_function_does_not_exist_either() does not exist.
This should be possible and is similar to RenderCallbackRule which checks ArrayItem.
However, I don't think it's been tested with the same value assignment that usually happens with submit handlers and validate handlers in Form API:
$form['#validate'][]
So the rule would need to check on ArrayItem and then check that all assigne values are callable, which is what RenderCallbackRule does.
public function prepareCallback($callback) {
if (is_string($callback) && substr($callback, 0, 2) == '::') {
$callback = [$this->getFormObject(), substr($callback, 2)];
}
return $callback;
}
Looks like service name patterns are not supported. But the shorthand ::METHOD_NAME is, which is the same as Class::METHOD_NAME but invoked as an instance method not statically.