1

I am new to PHPStan and I encountered an issue that I can't understand.

So I have a function, that supposed to return an array that looks like this: array<a|b|c|d..., mixed> (a,b,c,d are all class constants), but in the function, the array looks something like this:

/**
 * @return array<SomeConstants::*, mixed>
 */
function doSomething(){

return [
'key1'=>'value1'
'key2'=>'value2'
...
];

}

I get the following error:

Method doSomething() should return array<a|b|c|d...,mixed> but returns array('key1'=>SomeObject|null,'key2'=>SomeObject2|null)

How can I make PHPStan "believe" that this array "looks like" the array that is defined in the @return doc tag?

What have I tried?

  • /**@var array<SomeConstants::*> $array**/
  • /**@phpstan-var array<SomeConstants::*> $array**/

Both works fine, but according to my code reviewer, we just force PHPStan believe that keys that are not present in the array are present, so it's a "dirty" solution...

0 Answers0