Skip to content

Commit ba3290a

Browse files
committed
Improving doc and adding alias to 0.1
1 parent da7f084 commit ba3290a

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

README.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,34 @@ throwing exceptions properly when an error is encountered. The "safe" functions
5151
functions, except they are in the `Safe` namespace.
5252

5353
```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;
5656

5757
// This code is both safe and simple!
5858
$content = file_get_contents('foobar.json');
5959
$foobar = json_decode($content);
6060
```
6161

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+
6282
## Installation
6383

6484
Use composer to install Safe-PHP:
@@ -67,16 +87,25 @@ Use composer to install Safe-PHP:
6787
$ composer require thecodingmachine/safe
6888
```
6989

90+
*Highly recommended*: install PHPStan and PHPStan extension:
7091

92+
```bash
93+
$ composer require --dev thecodingmachine/phpstan-safe-rule
94+
```
95+
96+
Now, edit your `phpstan.neon` file and add these rules:
7197

72-
## TODO:
98+
```yml
99+
includes:
100+
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon
101+
```
73102
74-
- handle objects methods overloading
75-
- develop a PHPStan extension
76103
104+
## Work in progress
77105
106+
There are a number of issues withstanding [before releasing 1.0](https://github.com/thecodingmachine/safe/milestone/1)
78107
79108
## Contributing
80109
81-
The `lib.php` file that contains all the functions is auto-generated from the PHP doc.
82-
Read the [CONTRIBUTING.md](CONTRIBUTING.md) file to learn how to regenerate it and to contribute to this library.
110+
The files that contains all the functions are auto-generated from the PHP doc.
111+
Read the [CONTRIBUTING.md](CONTRIBUTING.md) file to learn how to regenerate these files and to contribute to this library.

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"php": ">=7.1",
1616
"ext-simplexml": "*",
1717
"phpunit/phpunit": "^7",
18-
"phpstan/phpstan": "^0.10.2",
19-
"thecodingmachine/phpstan-strict-rules": "^0.10",
18+
"phpstan/phpstan": "^0.10.3",
19+
"thecodingmachine/phpstan-strict-rules": "^0.10.3",
2020
"phpoffice/phpspreadsheet": "^1.4",
2121
"ext-json": "^1.5",
2222
"symfony/console": "^4.1.4",
@@ -28,6 +28,9 @@
2828
"cs-fix": "phpcbf",
2929
"cs-check": "phpcs"
3030
},
31-
"require": {
31+
"extra": {
32+
"branch-alias": {
33+
"dev-master": "0.1-dev"
34+
}
3235
}
3336
}

0 commit comments

Comments
 (0)