Skip to content

Commit

Permalink
documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Dec 5, 2020
1 parent 6d61d2e commit 43b848e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,42 @@ $ vendor/bin/psalm
```

Explanation:

This plugin aims to avoid code like this:
```php
function a(string $potential_int){
$int = (int) $potential_int;
//...
}
```
This cast is performed on a string that could have any value from a static analysis point of view.

The issue can be resolved in a few ways that will force you to have a better confidence in your variables types.

- You can check that the variable is indeed numeric:
```php
function a(string $potential_int){
if(is_numeric($potential_int)){
$int = (int) $potential_int;
}
else{
//throw
}
//...
}
```
```php
function a(string $potential_int){
Assert::numeric($potential_int);
$int = (int) $potential_int;
//...
}
```
- You can make psalm understand that the function expects a numeric (this will force you to correctly type any input to this function):
```php
/** @psalm-param numeric-string $potential_int */
function a(string $potential_int){
$int = (int) $potential_int;
//...
}
```

0 comments on commit 43b848e

Please sign in to comment.