2017 © Pedro Peláez
 

library 7to5

Convert PHP 7 code to PHP 5 code

image

spatie/7to5

Convert PHP 7 code to PHP 5 code

  • Monday, May 7, 2018
  • by Spatie
  • Repository
  • 9 Watchers
  • 132 Stars
  • 8,821 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 18 Forks
  • 0 Open issues
  • 13 Versions
  • 18 % Grown

The README.md

This package has been abandoned, (*1)

We are not maintaining this package any more. Feel free to fork our code and maintain your own copy., (*2)

Convert PHP 7.0 code to PHP 5 code

Latest Version on Packagist Software License Build Status Quality Score Total Downloads, (*3)

This package can convert PHP 7.0 code to PHP 5. This can be handy when you are running PHP 7 in development, but PHP 5 in production., (*4)

You can convert an entire directory with PHP 7.0 code with a the console command:, (*5)

php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code}

Here's an example of what it can do. It'll convert this code with PHP 7 features:, (*6)

class Test
{
    public function test()
    {
        $class = new class() {
            public function method(string $parameter = '') : string {
                return $parameter ?? 'no parameter set';
            }
        };

        $class->method();

        $anotherClass = new class() {
            public function anotherMethod(int $integer) : int {
                return $integer > 3;
            }
        };
    }

}

to this equivalent PHP 5 code:, (*7)

class AnonymousClass0
{
    public function method($parameter = '')
    {
        return isset($parameter) ? $parameter : 'no parameter set';
    }
}
class AnonymousClass1
{
    public function anotherMethod($integer)
    {
        return $integer < 3 ? -1 : ($integer == 3 ? 0 : 1);
    }
}
class Test
{
    public function test()
    {
        $class = new AnonymousClass0();
        $class->method();
        $anotherClass = new AnonymousClass1();
    }
}

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*8)

Installation

If you plan on use the console command we recommend installing the package globally:, (*9)

``` bash $ composer global require spatie/7to5, (*10)


If you want to [integrate the package in your own code](#programmatically-convert-files) require the package like usual: ``` bash $ composer require spatie/7to5

The conversion process

This package converts PHP 7 code to equivalent PHP 5 code by:, (*11)

  • removing scalar type hints
  • removing return type hints
  • removing the strict type declaration
  • replacing the spaceship operator by an equivalent PHP 5 code
  • replacing null coalesce statements by equivalent PHP 5 code
  • replacing group use declarations by equivalent PHP 5 code
  • replacing defined arrays by equivalent PHP 5 code
  • converting anonymous classes to regular classes

Because there are a lot of things that cannot be detected and/or converted properly we do not guarantee that the converted code will work. We highly recommend running your automated tests against the converted code to determine if it works., (*12)

Using the console command

This package provides a console command php7to5 to convert files and directories., (*13)

This is how a entire directory can be converted:, (*14)

$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code}

Want to convert a single file? That's cool too! You can use the same command., (*15)

$ php7to5 convert {$sourceFileWithPHP7Code} {$destinationFileWithPHP5Code}

By default the command will only copy over php-files. Want to copy over all files? Use the copy-all option:, (*16)

$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code} --copy-all

By default the command will only convert files with a php extension, but you can customize that by using the --extension option., (*17)

$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code} --extension=php --extension=phtml

If necessary, you can exclude directories / files., (*18)

$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code} --exсlude=cache

Programmatically convert files

You can convert a single file by running this code:, (*19)

$converter = new Converter($pathToPhp7Code);

$converter->saveAsPhp5($pathToWherePhp5CodeShouldBeSaved);

An entire directory can be converted as well:, (*20)

$converter = new DirectoryConverter($sourceDirectory);

$converter->savePhp5FilesTo($destinationDirectory);

By default this will recursively copy all to files to the destination directory, even the non php files., (*21)

If you only want to copy over the php files do this:, (*22)

$converter = new DirectoryConverter($sourceDirectory);

$converter
   ->doNotCopyNonPhpFiles()
   ->savePhp5FilesTo($destinationDirectory);

Changelog

Please see CHANGELOG for more information what has changed recently., (*23)

Testing

bash $ composer test, (*24)

Contributing

Please see CONTRIBUTING for details., (*25)

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker., (*26)

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*27)

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium., (*28)

We publish all received postcards on our company website., (*29)

Credits

Original idea: Jens Segers, (*30)

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*31)

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff., (*32)

License

The MIT License (MIT). Please see License File for more information., (*33)

The Versions

01/12 2017
25/09 2017
25/09 2017
17/04 2017
28/03 2016

1.0.0

1.0.0.0 https://github.com/spatie/7to5

Convert PHP 7 code to PHP 5 code

  Sources   Download

MIT

The Requires

 

The Development Requires

abstract php7 convert tree syntax spatie 7to5

26/03 2016

0.0.3

0.0.3.0 https://github.com/spatie/7to5

Convert PHP 7 code to PHP 5 code

  Sources   Download

MIT

The Requires

 

The Development Requires

abstract php7 convert tree syntax spatie 7to5

26/03 2016

0.0.2

0.0.2.0 https://github.com/spatie/7to5

Convert PHP 7 code to PHP 5 code

  Sources   Download

MIT

The Requires

 

The Development Requires

abstract php7 convert tree syntax spatie 7to5

20/02 2016

0.0.1

0.0.1.0 https://github.com/spatie/7to5

Convert PHP 7 code to PHP 5 code

  Sources   Download

MIT

The Requires

 

The Development Requires

abstract php7 convert tree syntax spatie 7to5

19/02 2016

dev-fixed-tests

dev-fixed-tests https://github.com/spatie/7to5

Convert PHP 7 code to PHP 5

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie 7to5