Happy Sunday, #Drupal and #PHPStan friends.

Question: when trying to get past PHPStan rule level 5, I get complaints about arrays when used as param or return type.

➡️ https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type

Is there a Drupally consensus for typehinting these?

e.g this makes PHPStan happy but what do I know?

/**
* Implements hook_form_alter().
*
* @param $form string[]
*/
mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) { ... }

Solving PHPStan error "No value type specified in iterable type"

@jpoesen if in doubt, I tend to look elsewhere in the core. Follow patterns already established

@rachel thanks Rachel.

If I understand correctly (big if), core only attempts to satisfy level 2.

re: my example, here's how core does it:

@param array $form

This doesn't make it past level 5, and triggers:

"[... ]parameter $form with no value type specified in iterable type array"

So I wonder if there's another common practice approach to satisfy level 5+, or if we're happy to ignore this particular error, or simply stick to level 5...

@rachel But you did give me a Good Idea (TM)!

D11's /core/phpstan.neon.dist contains a directive to ignore this particular error:

ignoreErrors:
- identifier: missingType.iterableValue

So that's one problem sorted + a reminder to refer to core more often.

Thanks!

@jpoesen @rachel I'm using `@phpstan-param mixed[] $form` on form methods (and alter hook implementations). This tells PHPStan that `$form` is an array but we do not have any idea what to expect - which is true actually. In form alters, PHPstan will flag any non-validated access to `$form` members. That forces me to use `assert()` and check the form structure before modifying it. That way I caught a bug in a form alter hook which wasn't covered with any tests just a couple of days ago.