GithubHelp home page GithubHelp logo

athiwatp / 7to5 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spatie/7to5

0.0 1.0 0.0 79 KB

Convert PHP 7 code to PHP 5 code

Home Page: https://spatie.be/opensource/php

License: MIT License

PHP 100.00%

7to5's Introduction

Convert PHP 7 code to PHP 5 code

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Total Downloads

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

You can convert an entire directory with PHP 7 code with a the console command:

$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code}

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

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:

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.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

If you plan on use the console command we recommend installing the package globally:

$ composer global require spatie/7to5

If you want to integrate the package in your own code require the package like usual:

$ composer require spatie/7to5

The conversion process

This package converts PHP 7 code to equivalent PHP 5 code by:

  • 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.

Using the console command

This package provides a console command php7to5 to convert files and directories.

This is how a entire directory can be converted:

$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code}

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

$ 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:

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

Programmatically convert files

You can convert a single file by running this code:

$converter = new Converter($pathToPhp7Code);

$converter->saveAsPhp5($pathToWherePhp5CodeShouldBeSaved);

An entire directory can be converted as well:

$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.

If you only want to copy over the php files do this:

$converter = new DirectoryConverter($sourceDirectory);

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

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

Original idea: Jens Segers

About Spatie

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

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.