Elektrine lite

← Feed

@m1ke@phpc.social

Post #759732

2026-03-24 17:30 UTC

For PHP static analysers, what's the best way to handle a method that takes a list of object-like-arrays, adds a known new key to each, and returns the list? eg https://psalm.dev/r/68a894d26d https://phpstan.org/r/5edc07eb-466d-4456-b675-bfba9ee8ee54

Replies (3)

  • @m1ke@phpc.social 2026-03-24 17:36

    It feels like a "generics" problem?

    Open ##1515010

  • @zimzat@mastodon.social 2026-03-24 18:25

    @m1ke I've run into this numerous times working in existing codebases that extensively used shaped arrays and unfortunately there's no good out-of-the-box answer. PHPStan won't include helpers like `Partial` and doesn't support `TInput&array{b: int}` syntax. The next best solution is creating a custom phpdoc type parser. https://github.com/phpstan/phpstan/discussions/9102 I've had limited success creating multiple phpstan types that copy-pastes base definitions instead of using generics.

    Open ##1515011

  • @OndrejMirtes@phpc.social 2026-03-24 20:01

    @m1ke Custom dynamic return type extension. Or come up with a design that relies less on arrays like that 😊

    Open ##1515013