Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

7To5 Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require spatie/7to5 --dev
    

    Add the service provider to config/app.php under providers:

    Spatie\SevenToFive\SevenToFiveServiceProvider::class,
    
  2. 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
    
  3. First Use Case:

    • Useful for maintaining legacy PHP 5.x codebases while gradually upgrading to PHP 7+.
    • Convert a single file to verify compatibility before full migration.

Where to Look First

  • Documentation: GitHub README (limited but sufficient).
  • Source Code: Focus on src/SevenToFive.php for core logic and src/Commands/ConvertCommand.php for CLI behavior.
  • Tests: tests/ directory for edge cases and expected outputs.

Implementation Patterns

Usage Patterns

  1. Incremental Conversion:

    • Convert files one-by-one to test changes without breaking the entire codebase.
    • Use --dry-run flag to preview changes:
      php artisan 7to5:convert path/to/file --dry-run
      
  2. Integration with CI/CD:

    • Add to a pre-commit hook or CI pipeline to enforce PHP 5 compatibility for legacy branches.
    • Example GitHub Actions step:
      - name: Convert PHP 7 to PHP 5
        run: php artisan 7to5:convert app/
      
  3. Custom Rules:

    • Extend the converter by subclassing Spatie\SevenToFive\SevenTo5 and overriding methods like convertFile() or convertClass().
    • Example:
      class CustomSevenToFive extends SevenToFive {
          protected function convertArrowFunction($node) {
              // Custom logic for arrow functions
              return parent::convertArrowFunction($node);
          }
      }
      
  4. Excluding Files/Directories:

    • Use --exclude flag to skip specific files or directories:
      php artisan 7to5:convert app/ --exclude="app/Tests/"
      

Workflows

  1. Legacy Codebase Migration:

    • Step 1: Convert all files to PHP 5 syntax.
    • Step 2: Fix compilation errors manually.
    • Step 3: Gradually refactor to PHP 7+ features.
  2. Vendor/Library Compatibility:

    • Convert third-party libraries (if allowed) to ensure compatibility with PHP 5.x environments.
  3. Hybrid Development:

    • Maintain a branch with PHP 7+ syntax while keeping main on PHP 5.x until full migration.

Integration Tips

  • Laravel Mix/Webpack: If using laravel-mix, ensure your webpack.mix.js targets PHP 5.x if needed (though this is rare).
  • PHPUnit: Run tests in a PHP 5.x container (e.g., Docker) to validate converted code:
    docker run --rm -v $(pwd):/app -w /app php:5.6-cli phpunit
    
  • IDE Support: Configure your IDE (e.g., PHPStorm) to parse converted files as PHP 5.x for accurate autocompletion.

Gotchas and Tips

Pitfalls

  1. False Positives:

    • The converter may not handle all PHP 7+ syntax correctly (e.g., complex type hints, null coalescing).
    • Fix: Manually review converted files, especially for edge cases.
  2. Backward Incompatibility:

    • Some converted code may introduce PHP 5.x-specific quirks (e.g., array() instead of []).
    • Fix: Use --overwrite cautiously and test thoroughly.
  3. Performance Overhead:

    • The package is outdated (last release in 2018) and may not handle modern PHP 8+ syntax.
    • Fix: Avoid using this for PHP 7.4+ code; focus on PHP 5.6–7.3.
  4. Namespace Conflicts:

    • If your code uses namespaces, ensure the converter preserves them correctly.
    • Fix: Test with a sample file first.
  5. Git Conflicts:

    • Converting files in a shared repo may cause merge conflicts.
    • Fix: Coordinate with the team or use feature branches.

Debugging

  1. Verbose Output: Enable debug mode to see detailed conversion steps:

    php artisan 7to5:convert path/to/file --verbose
    
  2. Log File: Redirect output to a log file for large conversions:

    php artisan 7to5:convert app/ > conversion.log 2>&1
    
  3. Test Cases:

    • Use the package’s tests (phpunit) to validate behavior for specific syntax:
      php artisan test --filter=SevenToFiveTest
      

Config Quirks

  1. No Config File:

    • The package has no configuration file; all settings are CLI-based.
    • Workaround: Create a custom wrapper class to store settings (e.g., excluded directories).
  2. Hardcoded Rules:

    • Some conversion rules (e.g., for => in arrays) are hardcoded.
    • Workaround: Extend the class to add custom rules (see "Custom Rules" above).

Extension Points

  1. Custom Node Handlers: Override methods in SevenToFive to handle unsupported syntax:

    protected function convertUnsupportedNode($node) {
        // Fallback logic for unsupported features
        return $node->getCode();
    }
    
  2. Pre/Post-Processing: Use Laravel’s booted event to run custom logic before/after conversion:

    SevenToFive::booted(function ($converter) {
        $converter->addCustomRule(...);
    });
    
  3. Integration with Laravel Events: Trigger events after conversion to log or notify:

    event(new FileConverted($filePath, $convertedCode));
    

Pro Tips

  • Backup First: Always back up files before running the converter, especially in production-like environments.
  • PHP Version Testing: Use tools like PHPCompatibility to validate converted code against PHP 5.x.
  • Pair with Other Tools: Combine with php-cs-fixer to standardize code after conversion:
    php artisan 7to5:convert app/ && php-cs-fixer fix app/
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport