This problem seemed obvious at first to me but I've been struggling for a while and I can't find any useful information on internet/stack overflow regarding this.
I have a function that looks like this :
myfunction(string $parameter, array $optionalParameter = []) {
}
and in my call, something like that :
myfunction('text', BOOL ?? $array);
The problem is that, from what I understand, passing null as the second argument isn't the same as having no second argument, so $optionalParameter isn't initialized (I use phpstan and it tells me that null is incompatible with array).
The only solution I can envision would be to duplicate the function call, but it's really not a good idea considering my actual function has way more than 2 parameters.
TLDR; I'm looking for a way to turn the return type of BOOL ?? $array from array|null to array|(nothing) if that makes sense
nullfor your second parameter, use?arrayorarray|nullinstead ofarray. Then, in the first lines of your function, use something like$optionalParameter = (optionalParameter === null ? [] : optionalParameter).?in front of thearray $optionalParameterin the function argument and then add one single line in the top of the function:$optionalParameter = $optionalParameter ?? [];. That would allow you to pass in an array, null or don't pass anything in. Not sure how to make it easier than that? If that's not what you're asking, then you need to update the question with some real examples