1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 01:09:38 +01:00
psalm/docs/running_psalm/issues/MixedAssignment.md

39 lines
494 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
2020-03-19 17:32:49 +01:00
$a = $_GET['foo'];
```
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
2020-03-19 17:32:49 +01:00
$a = $_GET['foo'];
assert(is_string($a));
```
or by adding an explicit cast:
```php
2020-03-21 00:13:46 +01:00
<?php
2020-03-19 17:32:49 +01:00
$a = (string) $_GET['foo'];
```
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 */
$a = $_GET['foo'];
```