vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Understand extended phpdoc types like `list`

Open nandocs opened this issue 4 years ago • 5 comments

Feature description or problem with existing feature I've got a list, but it expected an array. Every list is always an array, but intelephense thinks list is something separate from array.

Describe the solution you'd like Teach intelephense that every list is an array, but not every array is a list.

Additional context

/** @var list<array{id:string}>*/
$test = $conn->select($query);
$test2 = array_map(static fn($x) => hydrateRow($x), $test);
//                                                  ^^^^^^

intelephense(1006) expected type array found list

nandocs avatar Oct 01 '20 08:10 nandocs

I just updated to the latest version of Doctrine DBAL, which uses list extensively for return types. The result is that my code is now littered with Expected type 'array'. Found 'Doctrine\DBAL\list<array<string, mixed>>' errors wherever it takes the rows returned from a database query and treats them as an array e.g. by passing them to array_map.

Looking at the Psalm documentation for list, it does seem safe to treat lists as arrays in Intelephense, and Doctrine is sufficiently widely used that it would save a lot of grief.

NorthNick avatar Dec 12 '21 17:12 NorthNick

Any workaround for this until it becomes supported?

rzvc avatar May 10 '22 02:05 rzvc

+1 for this - as list<Type> gets more and more widely used now (and is supported by PHPStorm for example).

fritzmg avatar May 12 '22 12:05 fritzmg

Yes, please. list<Type> is widely used now. Especially in Doctrine.

image

Thank you for your great tool!

palansher avatar Jul 11 '22 12:07 palansher

Please add my name to this list of those that would love to have support for this added!

Expected type 'Countable|array'. Found 'Doctrine\DBAL\list<array<string, mixed>>

Iron27 avatar Jul 26 '22 20:07 Iron27

Just wanted to say thanks for fixing this - my code looks a whole lot less red now.

NorthNick avatar Jan 01 '23 18:01 NorthNick

@bmewburn The Intelephense is still best tool for PHP in VSCODE. Thank you, my savior! Please keep going. I you feel disappointed , don't be confused to ask money for new major version. Although I am homeless coder from ex Russia, but want to be thankful somehow. So if you have a donation page I did notice, pls let me know.

palansher avatar Jan 01 '23 18:01 palansher