I have the following code:
$request->headers->get('Accept-Language', 'en');
I provide a default value but Psalm thinks it's potentially null since ->get()
declares that return a nullable string:
// vendor/symfony/http-foundation/HeaderBag.php
/**
* Returns a header value by name.
*
* @return string|null The first header value or default value
*/
public function get(string $key, string $default = null) { /* */ }
How can I fix this so psalm knows it's not null?
Since you cannot control the annotation in the upstream library, you'll have to provide the missing information to Psalm in your own code.
A couple of ways to go about it:
Cast to string, so Psalm has no doubt what type
get()
is getting you:Yup, the cast is redundant, but it's clear and concise. I usually do this just for economy.
You could explicitly declare that the variable that result of this
get()
call is a string:Finally, you can simply suppress the
PossiblyNullArgument
wherever you need it:See all these working here.
You can also combine some of the above with your own wrapper method to deal with getting values from the request, making sure you always return string. If you do that, you should probably
throw
an exception if the parameter is not found.