spatie/7to5
Abandoned. Converts PHP 7.0 code to PHP 5, useful when developing on PHP 7 but deploying to PHP 5. Provides a CLI to convert entire directories and rewrites features like anonymous classes, type hints, return types, and null coalescing.
Installation:
composer require spatie/7to5 --dev
Add the service provider to config/app.php under providers:
Spatie\SevenToFive\SevenToFiveServiceProvider::class,
Basic Usage: Run the Artisan command to convert a single file:
php artisan 7to5:convert path/to/YourClass.php
Or process an entire directory recursively:
php artisan 7to5:convert path/to/directory
First Use Case:
src/SevenToFive.php for core logic and src/Commands/ConvertCommand.php for CLI behavior.tests/ directory for edge cases and expected outputs.Incremental Conversion:
--dry-run flag to preview changes:
php artisan 7to5:convert path/to/file --dry-run
Integration with CI/CD:
- name: Convert PHP 7 to PHP 5
run: php artisan 7to5:convert app/
Custom Rules:
Spatie\SevenToFive\SevenTo5 and overriding methods like convertFile() or convertClass().class CustomSevenToFive extends SevenToFive {
protected function convertArrowFunction($node) {
// Custom logic for arrow functions
return parent::convertArrowFunction($node);
}
}
Excluding Files/Directories:
--exclude flag to skip specific files or directories:
php artisan 7to5:convert app/ --exclude="app/Tests/"
Legacy Codebase Migration:
Vendor/Library Compatibility:
Hybrid Development:
main on PHP 5.x until full migration.laravel-mix, ensure your webpack.mix.js targets PHP 5.x if needed (though this is rare).docker run --rm -v $(pwd):/app -w /app php:5.6-cli phpunit
False Positives:
null coalescing).Backward Incompatibility:
array() instead of []).--overwrite cautiously and test thoroughly.Performance Overhead:
Namespace Conflicts:
Git Conflicts:
Verbose Output: Enable debug mode to see detailed conversion steps:
php artisan 7to5:convert path/to/file --verbose
Log File: Redirect output to a log file for large conversions:
php artisan 7to5:convert app/ > conversion.log 2>&1
Test Cases:
phpunit) to validate behavior for specific syntax:
php artisan test --filter=SevenToFiveTest
No Config File:
Hardcoded Rules:
=> in arrays) are hardcoded.Custom Node Handlers:
Override methods in SevenToFive to handle unsupported syntax:
protected function convertUnsupportedNode($node) {
// Fallback logic for unsupported features
return $node->getCode();
}
Pre/Post-Processing:
Use Laravel’s booted event to run custom logic before/after conversion:
SevenToFive::booted(function ($converter) {
$converter->addCustomRule(...);
});
Integration with Laravel Events: Trigger events after conversion to log or notify:
event(new FileConverted($filePath, $convertedCode));
php-cs-fixer to standardize code after conversion:
php artisan 7to5:convert app/ && php-cs-fixer fix app/
How can I help you explore Laravel packages today?