vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.56k stars 660 forks source link

Feature: Documenting that a function adds a key to an array #9111

Open ciaranmcnulty opened 1 year ago

ciaranmcnulty commented 1 year ago

There doesn't seem to be a good syntax for this - the prior knowledge about the type is lost:

https://psalm.dev/r/1bd9096017

Ideally Psalm would be able to know that the baz key is retained

Maybe something like intersections for arrays would work in future?

/**
 * @template T of array
 * @param-out T&array{foo:string,...} $a
 */

Although on Slack @orklah said if T already contained the foo offset with another type, then the intersection will give you never

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/1bd9096017 ```php 100]; addKey($x); /** @psalm-trace $x */ ``` ``` Psalm output (using commit 3e66c6b): INFO: Trace - 13:23 - $x: array{foo: string, ...} ```