Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Psalm possibly null value provided with request->get()

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?

like image 730
michael Avatar asked Oct 20 '25 03:10

michael


1 Answers

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:

$a = (string) $request->headers->get('Accept-Language', 'en');

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:

/** @var string $acceptLanguage **/
$acceptLanguage = $request->headers->get('Accept-Language', 'en');

Finally, you can simply suppress the PossiblyNullArgument wherever you need it:

/** @psalm-suppress PossiblyNullArgument */
iWantAString($request->headers->get('Accept-Language', 'en'));

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.

like image 65
yivi Avatar answered Oct 21 '25 19:10

yivi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!