You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-7Lines changed: 36 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,14 +51,34 @@ throwing exceptions properly when an error is encountered. The "safe" functions
51
51
functions, except they are in the `Safe` namespace.
52
52
53
53
```php
54
-
use Safe\file_get_contents;
55
-
use Safe\json_decode;
54
+
use function Safe\file_get_contents;
55
+
use function Safe\json_decode;
56
56
57
57
// This code is both safe and simple!
58
58
$content = file_get_contents('foobar.json');
59
59
$foobar = json_decode($content);
60
60
```
61
61
62
+
## PHPStan integration
63
+
64
+
> Yeah... but I must explicitly think about importing the "safe" variant of the function, for each and every file of my application.
65
+
> I'm sure I will forget some "use function" statements!
66
+
67
+
Fear not! thecodingmachine/safe comes with a PHPStan rule.
68
+
69
+
Never heard of [PHPStan](https://github.com/phpstan/phpstan) before?
70
+
Check it out, it's an amazing code analyzer for PHP.
71
+
72
+
Simply install the Safe rule in your PHPStan setup and PHPStan will let you know each time you are using an "unsafe" function:
73
+
74
+
The code below will trigger this warning:
75
+
76
+
```php
77
+
$content = file_get_contents('foobar.json');
78
+
```
79
+
80
+
> Function file_get_contents is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\file_get_contents;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.
81
+
62
82
## Installation
63
83
64
84
Use composer to install Safe-PHP:
@@ -67,16 +87,25 @@ Use composer to install Safe-PHP:
67
87
$ composer require thecodingmachine/safe
68
88
```
69
89
90
+
*Highly recommended*: install PHPStan and PHPStan extension:
0 commit comments