2020-03-19 17:32:49 +01:00
# PossiblyNullArgument
Emitted when calling a function with a value that’ s possibly null when the function does not expect it
```php
2022-06-21 22:45:45 +02:00
< ?php
2022-06-21 20:23:23 +02:00
function foo(string $s): void {}
2020-03-19 17:32:49 +01:00
foo(rand(0, 1) ? "hello" : null);
```
2022-06-21 20:23:23 +02:00
## Common Problem Cases
### Using a Function Call inside `if`
```php
2022-11-10 16:59:04 +01:00
< ?php
2022-06-21 20:23:23 +02:00
if (is_string($cat->getName()) {
foo($cat->getName());
}
```
This fails since it's not guaranteed that subsequent calls to `$cat->getName()` always give the same result.
#### Possible Solutions
2022-11-10 20:19:02 +01:00
Use a variable:
```php
< ?php
$catName = $cat->getName();
if (is_string($catName) {
foo($catName);
}
unset($catName);
```
Or add [`@psalm-mutation-free` ](../../annotating_code/supported_annotations.md#psalm-mutation-free ) to the declaration of the function
2022-06-21 20:23:23 +02:00
### Calling Another Function After `if`
```php
2022-11-10 16:59:04 +01:00
< ?php
2022-06-21 20:23:23 +02:00
if (is_string($cat->getName()) {
changeCat();
foo($cat->getName());
}
```
This fails since psalm cannot know if `changeCat()` does actually modify `$cat` .
#### Possible Solutions
2022-06-21 21:55:32 +02:00
* Add [`@psalm-mutation-free` ](../../annotating_code/supported_annotations.md#psalm-mutation-free ) to the declaration of the other function (here: `changeCat()` ) too
2022-06-21 20:23:23 +02:00
* Use a variable: See above