### Start Docker Environment with Blackfire for Profiling Source: https://github.com/thecodingmachine/safe/blob/master/performance/README.md Starts the PHP 7.2 Docker environment with Blackfire enabled using `docker-compose`. Replace `[xyz]` and `[abc]` with your Blackfire server ID and token respectively. This setup allows for profiling the performance of Composer autoloading with and without the Safe library. ```bash BLACKFIRE_SERVER_ID=[xyz] BLACKFIRE_SERVER_TOKEN=[abc] docker-compose up ``` -------------------------------- ### Install Composer Dependencies for Safe Performance Tests Source: https://github.com/thecodingmachine/safe/blob/master/performance/README.md These bash commands are used to install the necessary Composer dependencies for the Safe library's performance testing. Navigate into the respective test directories (`test_with_safe` and `test_without_safe`) and run `composer install`. ```bash cd test_with_safe composer install cd .. cd test_without_safe composer install cd .. ``` -------------------------------- ### Install Composer Dependencies Source: https://github.com/thecodingmachine/safe/blob/master/CONTRIBUTING.md Steps to install the necessary PHP dependencies for the Safe-PHP generator using Composer. This is required before running generation commands. ```bash cd generator composer install ``` -------------------------------- ### Install Safe-PHP via Composer Source: https://github.com/thecodingmachine/safe/blob/master/README.md Provides the Composer command to install the Safe-PHP library into a project. This is the standard method for managing PHP dependencies. ```Bash composer require thecodingmachine/safe ``` -------------------------------- ### Install Rector for Refactoring Source: https://github.com/thecodingmachine/safe/blob/master/README.md Installs the Rector tool as a development dependency using Composer. This is the first step to enable automated refactoring. ```bash composer require --dev rector/rector ``` -------------------------------- ### Install PHPStan Extension for Safe-PHP Source: https://github.com/thecodingmachine/safe/blob/master/README.md Shows the Composer command to install the PHPStan extension for Safe-PHP, which enables static analysis for detecting unsafe function usage. This is recommended for development environments. ```Bash composer require --dev thecodingmachine/phpstan-safe-rule ``` -------------------------------- ### Demonstrate Unsafe PHP File Operations Source: https://github.com/thecodingmachine/safe/blob/master/README.md Illustrates the common problem in PHP where functions like file_get_contents and json_decode return false on error, requiring explicit checks. This example shows the potential for bugs when these checks are omitted. ```PHP $content = file_get_contents('foobar.json'); $foobar = json_decode($content); ``` -------------------------------- ### Download PHP Documentation Source: https://github.com/thecodingmachine/safe/blob/master/CONTRIBUTING.md Command to download the official PHP documentation repository, which is a prerequisite for using the Safe-PHP generator. ```bash $ generator/safe.php download-docs ``` -------------------------------- ### Run Rector Refactoring with Safe-PHP Config Source: https://github.com/thecodingmachine/safe/blob/master/README.md Executes the Rector tool to process your source files, applying the Safe-PHP migration rules. Ensure to replace 'src/' with your project's source directory. ```bash vendor/bin/rector process src/ --config vendor/thecodingmachine/safe/rector-migrate.php ``` -------------------------------- ### Generate Safe PHP Functions Source: https://github.com/thecodingmachine/safe/blob/master/CONTRIBUTING.md Command to execute the Safe-PHP generator, which parses PHP documentation and creates safe wrapper functions. This is a core step in the development workflow. ```bash cd generator php ./safe.php generate ``` -------------------------------- ### Generate Safe Files with PHP Source: https://github.com/thecodingmachine/safe/blob/master/CONTRIBUTING.md Command to regenerate files for the safe project. This is typically run before submitting a Pull Request to ensure all functions and checks are up-to-date. ```CLI php ./safe.php generate ``` -------------------------------- ### Configure PHPStan to Use Safe-PHP Rule Source: https://github.com/thecodingmachine/safe/blob/master/README.md Details how to configure PHPStan by including the Safe-PHP rule in the `phpstan.neon` configuration file. This activates the static analysis checks for unsafe PHP functions. ```YAML includes: - vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon ``` -------------------------------- ### Safe PHP File Operations with Exceptions Source: https://github.com/thecodingmachine/safe/blob/master/README.md Shows the 'safe' way to perform file operations using the Safe-PHP library. Functions like Safe\file_get_contents and Safe\json_decode throw exceptions on error, making code cleaner and more reliable. ```PHP use function Safe\file_get_contents; use function Safe\json_decode; $content = file_get_contents('foobar.json'); $foobar = json_decode($content); ``` -------------------------------- ### Manual Refactoring of Error Handling Source: https://github.com/thecodingmachine/safe/blob/master/README.md Demonstrates the manual adjustment needed for error handling after Rector refactors a function call. The original 'if' check needs to be replaced with a try-catch block to handle potential exceptions thrown by Safe-PHP functions. ```php /* * Original code before refactoring: */ if (!mkdir($dirPath)) { // Do something on error } /* * Code after Rector refactoring (automatic): */ if (!\Safe\mkdir($dirPath)) { // Do something on error } /* * Code after manual refactoring (required): */ try { \Safe\mkdir($dirPath); } catch (\Safe\FilesystemException $e) { // Do something on error } ``` -------------------------------- ### PHPStan Rule for Unsafe Function Usage Source: https://github.com/thecodingmachine/safe/blob/master/README.md Demonstrates a PHPStan warning generated when an 'unsafe' PHP function (one that can return false) is used without importing the Safe-PHP equivalent. This encourages the adoption of safer coding practices. ```PHP $content = file_get_contents('foobar.json'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.