thecodingmachine/safe
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Thecodingmachine/safe is a set of PHP core functions re-written to throw exceptions instead of returning false when an error is encountered. This offers a safer and more predictable handling of PHP functions.
To use thecodingmachine/safe, firstly, you would need to use composer for installation by running $ composer require thecodingmachine/safe
. Then, in the PHP files, you need to import the "safe" variant of the functionality needed. For example, to use file_get_contents
and json_decode
functions, you would write:
use function Safe\file_get_contents;
use function Safe\json_decode;
$content = file_get_contents('foobar.json');
$foobar = json_decode($content);
It's highly recommended to also install PHPStan and PHPStan extension, which come with a rule to let you know each time you are using an "unsafe" PHP core function. To do this, run $ composer require --dev thecodingmachine/phpstan-safe-rule
and add the required rules to your phpstan.neon
file.
In addition, for larger projects, you can automate the refactoring using a tool called Rector. The specific guidance for the installation and usage of Rector with Safe is detailed in the readme.
Documentation for thecodingmachine/safe can be found in the readme document on its GitHub repository at https://github.com/thecodingmachine/safe. The readme provides detailed guidance on how to install, use, and contribute to Safe. Further instructions for performance testing and the list of core PHP functions that are re-written by Safe can also be found through the readme linked in the repository. To learn more about what triggered the development of Safe, you can read the release article on TheCodingMachine's blog linked in the readme as well.