tool for backporting PHP7 scripts to PHP5
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.
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.
Currently, these tranformations of PHP 7 structures are supported:
$foo ?? $bar;
becomes
isset($foo) ? $foo : $bar;
42 ?? $bar;
becomes
!is_null(42) ? 42 : $bar;
function foo() : SomeClass {}
becomes
function foo() {}
function foo(string $x, SomeClass $y) {}
becomes
function foo($x, SomeClass $y) {}
$foo <=> $bar;
becomes
$foo > $bar ? 1 : ($foo < $bar ? -1 : 0);
$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(10, 3);
becomes
(int) floor(10 / 3);
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));
}
- 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.