Code Monkey home page Code Monkey logo

php7backport's Introduction

php7backport

tool for backporting PHP7 scripts to PHP5

Build Status

What is it for?

It is a tool for backporting PHP 7 code into PHP 5. It works by parsing the source script (with nikic/php-parser) and transforming the new PHP 7 features into equivalent expressions using PHP 5 syntax. The transformed parts are patched into the original code, so as to retain as much of the original formatting as possible. For example when changing the header of function (type hints, return type), only the modified header is patched back to the original code.

Included is a script for recursive conversion of whole directory.

Usage

Clone repo, run composer install, run php convert.php <source dir> <destination dir>. All *.php files from source dir will be copied to destination dir and backported, retaining the folder structure. No other files will be copied or converted.

Transformations

Currently, these tranformations of PHP 7 structures are supported:

Null Coalesce Operator

Example 1

$foo ?? $bar;

becomes

isset($foo) ? $foo : $bar;

Example 2

42 ?? $bar;

becomes

!is_null(42) ? 42 : $bar;

Return Type Declarations

Example

function foo() : SomeClass {}

becomes

function foo() {}

Scalar Type Declarations

Example

function foo(string $x, SomeClass $y) {}

becomes

function foo($x, SomeClass $y) {}

Spaceship operator (Combined Comparison Operator)

Example

$foo <=> $bar;

becomes

$foo > $bar ? 1 : ($foo < $bar ? -1 : 0);

Anonymous Classes

Example

$util->setLogger(new class("test.log") {
    function __construct($file) {}
    public function log($msg)
    {
        echo $msg;
    }
});

becomes

$util->setLogger(new AnonymousClass_1('test.log'));

class AnonymousClass_1
{
    function __construct($file)
    {
    }
    public function log($msg)
    {
        echo $msg;
    }
}

Class declaration is appended to the end of source file. Multiple anonymous classes are numbered accordingly.

intdiv() function

Example

intdiv(10, 3);

becomes

(int) floor(10 / 3);

Complex example

Transformations are applied using operator precedence etc. and even more complex code is transformed correctly.

function foo(string $x, SomeClass $y) : int
{
    return $foo ?? $one <=> $two;
}

becomes

function foo($x, SomeClass $y)
{
    return isset($foo) ? $foo : ($one > $two ? 1 : ($one < $two ? -1 : 0));
}

What is missing?

  • Unicode Codepoint Escape Syntax
  • Closure call() Method
  • Filtered unserialize()
  • IntlChar Class
  • Expectations
  • Group use Declarations
  • Generator Return Expressions
  • Generator Delegation

and other features... Some of them are not trivial to implement.

php7backport's People

Contributors

athorcis avatar mihaeu avatar vrana avatar

Stargazers

 avatar Oleg Petrov avatar Alessio Felicioni avatar  avatar Lenix avatar Recca Tsai avatar Elie avatar Frugan avatar Mike Meyer avatar Honza Lilák avatar dmill avatar Oleg avatar Sebastian Wasser avatar Zi-long Qiu avatar Marek Mako avatar  avatar Javier Eguiluz avatar Maxim avatar Andrew avatar Ivan avatar sasezaki avatar Aleksandr Manichev avatar  avatar Kernel Jackson avatar Daniel Plakinger avatar Alex Litvinenko avatar Muhammad Yusuf avatar Nicholas Jones avatar Vašek Purchart avatar Raymond Rutjes avatar Vladimir Bloshchitsyn avatar Konstantin avatar Mark Penner avatar  avatar Michael Yoo avatar Matthieu Napoli avatar Kennedy Tedesco avatar Trevor N. Suarez avatar Martin avatar Hari K T avatar

Watchers

dmill avatar Raymond Rutjes avatar Ondřej Bouda avatar

Forkers

vrana ronmi mihaeu

php7backport's Issues

Generated identifiers are unique only within one source file

Identifiers used for generated names (e.g. anonymous classes) use sequence which is reset for each converted file. Conflicts could arise.

Proposed solution: add some kind of hash to the generated name (hashed source file maybe?) to ensure uniqueness

TODO

  • return changed node from visitor - problem with constructors, where the input node is class, but changed node is ClassMethod
  • better handling of invalidating nodes - post traversal maybe?
  • fix situation, when a node changes twice (e.g. constructor + return type) - then only one of them renders

Directory not created before file_put_contents in PHP 5.6 when running convert.php

Source files to be converted:

  • src/A.php
  • src/B/B.php

Command I use:

php convert.php src /tmp

It will produce warning like: file_put_contents(/tmp/B/B.php): failed to open stream: No such file or directory

By adding var_dump($file); to DirectoryBackporter.php Line 28, it dumps src/A.php, src/B/B.php, src/B. So the order is not what we expected.

I made some change to DirectoryBackporter.php, which can fix this bug in 5.6. I can send you PR if you think my code is okay to use.

Some Warning and a Fatal Error

I cloned the repo into the Composer/vendor folder. I run on Command Prompt

php converter.php "C:/User/..../..../ Php7CodeFolder" "C:/Users/.../..../Php5CodeFolder"

It give me some "Warning" -->

mkdir(): Invalid argument in C:\Users..........\Composer\vendor\php7backport\src\Bouda\Php7Backport\DirectoryBackporter.php on line 31

and a "Fatal Error" -->

Uncaught Error: Class 'PhpParser\Lexer\Emulative' not found in C:\Users.........\Composer\vendor\php7backport\src\Bouda\Php7Backport\Backporter.php:21

I don't understand why and how to resolve these errors.

Can you give me some help?

Change to PHP 7.1

Hey there,

thanks for your work!

I just noticed that you're not using PHP 7 for the project's source code. Would you consider a PR adding PHP 7 features and a backported version for users of old versions (branch or phar)?

Cheers,
Mike

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.