1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/docs/running_psalm/issues/MixedAssignment.md

39 lines
506 B
Markdown
Raw Normal View History

2020-03-19 17:32:49 +01:00
# MixedAssignment
Emitted when assigning an unannotated variable to a value for which Psalm
cannot infer a type more specific than `mixed`.
```php
2020-03-21 00:13:46 +01:00
<?php
2022-09-10 13:06:17 +02:00
$a = $GLOBALS['foo'];
2020-03-19 17:32:49 +01:00
```
2020-03-19 17:42:41 +01:00
## How to fix
2020-03-19 17:32:49 +01:00
The above example can be fixed in a few ways by adding an `assert` call:
```php
2020-03-21 00:13:46 +01:00
<?php
2022-09-10 13:06:17 +02:00
$a = $GLOBALS['foo'];
2020-03-19 17:32:49 +01:00
assert(is_string($a));
```
or by adding an explicit cast:
```php
2020-03-21 00:13:46 +01:00
<?php
2022-09-10 13:06:17 +02:00
$a = (string) $GLOBALS['foo'];
2020-03-19 17:32:49 +01:00
```
or by adding a docblock
```php
2020-03-21 00:13:46 +01:00
<?php
2020-03-19 17:32:49 +01:00
/** @var string */
2022-09-10 13:06:17 +02:00
$a = $GLOBALS['foo'];
2020-03-19 17:32:49 +01:00
```