spatie / 7to5 Goto Github PK
View Code? Open in Web Editor NEWConvert PHP 7 code to PHP 5 code
Home Page: https://spatie.be/opensource/php
License: MIT License
Convert PHP 7 code to PHP 5 code
Home Page: https://spatie.be/opensource/php
License: MIT License
Convert scalar type hints to additional PHP 5 checks. They are currently just removed.
For example, function:
function myFunction(string $name = 'World')
{
return 'Hello ' . $name;
}
should be converted to something like:
function myFunction($name = 'World')
{
if (!is_string($name)) {
throw new InvalidArgumentException('Argument `$name` is not string');
}
return 'Hello ' . $name;
}
Exception type should be InvalidArgumentException
or TypeError
. It's message should be something like Argument 2 passed to sum() must be of the type integer, string given, ...
.
The following code (in a test):
return [
[
'SELECT 1 AS foo', 0, [], [1]
],
[
'SELECT 1 AS foo, 2 AS bar', 0, [], [1]
],
[
'SELECT 1 AS foo, 2 AS bar', 1, [], [2]
],
[
'SELECT 1 AS foo, 2 AS bar UNION SELECT 3 AS foo, 4 AS bar', 0, [], [1,3]
],
[
'SELECT 1 AS foo, 2 AS bar UNION SELECT 3 AS foo, 4 AS bar', 1, [], [2,4]
],
[
'SELECT ? AS foo, ? AS bar UNION SELECT ? AS foo, ? AS bar', 0, [1, 2, 3, 4], [1, 3]
],
[
'SELECT ? AS foo, ? AS bar UNION SELECT ? AS foo, ? AS bar', 1, [1, 2, 3, 4], [2, 4]
]
];
Becomes this after reformatting:
return [['SELECT 1 AS foo', 0, [], [1]], ['SELECT 1 AS foo, 2 AS bar', 0, [], [1]], ['SELECT 1 AS foo, 2 AS bar', 1, [], [2]], ['SELECT 1 AS foo, 2 AS bar UNION SELECT 3 AS foo, 4 AS bar', 0, [], [1, 3]], ['SELECT 1 AS foo, 2 AS bar UNION SELECT 3 AS foo, 4 AS bar', 1, [], [2, 4]], ['SELECT ? AS foo, ? AS bar UNION SELECT ? AS foo, ? AS bar', 0, [1, 2, 3, 4], [1, 3]], ['SELECT ? AS foo, ? AS bar UNION SELECT ? AS foo, ? AS bar', 1, [1, 2, 3, 4], [2, 4]]];
Hi.
This a great package. Please can you add support for following syntax reverting:
Variadic functions via ...
Argument unpacking via ...
Class name resolution via ::class
thanks
I tried this approach for replacing anonymous classes:
$foo = new class {
public function bar()
{
return 'bar';
}
}
$foo->bar();
$foo = call_user_func(function () {
$thisObject = new \stdClass();
$thisObject->bar = ()
{
return 'bar';
}
return $thisObject;
}
$foo->bar();
But this will never work. More reading:
http://stackoverflow.com/questions/11503212/add-method-in-an-std-object-in-php
http://stackoverflow.com/questions/2938004/how-to-add-a-new-method-to-a-php-object-on-the-fly
TLDR: you cannot add methods on new stdClass
object to be invoke via the object->method()
syntax.
You can with call_user_func($object->method)
but it would be too hard to find all places where a method will be invoke on that object.
So I tried with defining a new class, give it a long name, and it works:
$foo = call_user_func_array(function () {
class Spatie_Php7to5_Anonymous_ABCDEF1234
{
public function bar()
{
return 'bar';
}
}
$reflection = new \ReflectionClass('Spatie_Php7to5_Anonymous_ABCDEF1234');
return $reflection->newInstanceArgs(func_get_args());
}, []);
This allows you to copy the entire class body and invoke a new object. Question is: do we want this?
PS: wrapping the declaration in a namespace with namespace Spatie\7to5\Anon { class ... {} }
is impossible (http://php.net/manual/en/language.namespaces.definitionmultiple.php)
Another way to do this would be to provide a special class:
$foo = new Spatie\7to5\Helper\AnonymousClassWrapper($constructor, $methods, $attributes, $constructorArguments);
Which uses the __call
method and __get
to expose all public attributes and methods. This would require a lot of rewriting the AST, though.
What do you think?
I have read the other closed Issues but it doesn't help me.
The error is
PHP Fatal error: Uncaught Error: Call to undefined function Spatie\Php7to5\Console\ends_with() in C:\Users......\Composer\vendor\spatie\7to5\src\Console\ConvertCommand.php:136
I did use composer global require
to install the bundle.
I saw that into Composer\Vendor folder there is not any illuminate/support subdir.
How can I resolve this isssue?
Any help is appreciated, thanks
I was wondering... Since it's transforming source code to run on PHP 5.x, shouldn't the tool be capable of running on PHP 5.x? Otherwise you'll need to transform it, then use a different PHP installation to run it.
I tried to install version 1.2.1 on a blank system. This failed because there seems to be a dependency on symfony/finder
which is not covered by the composer.json.
composer global require spatie/7to5
php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code}
Everything works
PHP Fatal error: Uncaught Error: Class 'Symfony\Component\Finder\Finder' not found in /home/user/.config/composer/vendor/spatie/7to5/src/DirectoryConverter.php:98
Stack trace:
#0 /home/user/.config/composer/vendor/spatie/7to5/src/DirectoryConverter.php(85): Spatie\Php7to5\DirectoryConverter->copyDirectory('src/lib/', 'src/lib5')
#1 /home/user/.config/composer/vendor/spatie/7to5/src/Console/ConvertCommand.php(120): Spatie\Php7to5\DirectoryConverter->savePhp5FilesTo('src/lib5')
#2 /home/user/.config/composer/vendor/spatie/7to5/src/Console/ConvertCommand.php(79): Spatie\Php7to5\Console\ConvertCommand->convertPHPFilesInDirectory(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /home/user/.config/composer/vendor/symfony/console/Command/Command.php(252): Spatie\Php7to5\Console\ConvertCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /home/user/.config/composer/vendor/symfony/console/A in /home/user/.config/composer/vendor/spatie/7to5/src/DirectoryConverter.php on line 98
The package symfony/finder
was not installed on my machine. Running composer global require symfony/finder
solved the problem.
Example:
(function(){
echo 'hello';
})();
should be replaced by something like:
$_=function(){
echo 'hello';
}
$_();
//or
function _(){
echo 'hello';
}
_();
Looks like class constants visibility (added in PHP 7.1) are not converted (ie. removed).
This is invalid in PHP <7.1
class Foo
{
protected const FOO = 'foo';
public const BAR = 'bar';
private const BAZ = 'baz';
}
Or maybe this is out of the scope of this project (because it is 7.1 feature)?
e.g. (from https://github.com/voku/portable-utf8/blob/master/src/voku/helper/UTF8.php)
private static $BOM = [
"\xef\xbb\xbf" => 3, // UTF-8 BOM
'' => 6, // UTF-8 BOM as "WINDOWS-1252" (one char has [maybe] more then one byte ...)
"\x00\x00\xfe\xff" => 4, // UTF-32 (BE) BOM
' þÿ' => 6, // UTF-32 (BE) BOM as "WINDOWS-1252"
"\xff\xfe\x00\x00" => 4, // UTF-32 (LE) BOM
'ÿþ ' => 6, // UTF-32 (LE) BOM as "WINDOWS-1252"
"\xfe\xff" => 2, // UTF-16 (BE) BOM
'þÿ' => 4, // UTF-16 (BE) BOM as "WINDOWS-1252"
"\xff\xfe" => 2, // UTF-16 (LE) BOM
'ÿþ' => 4, // UTF-16 (LE) BOM as "WINDOWS-1252"
];
... becomes ...
private static $BOM = [
"" => 3,
// UTF-8 BOM
'' => 6,
// UTF-8 BOM as "WINDOWS-1252" (one char has [maybe] more then one byte ...)
"\0\0��" => 4,
// UTF-32 (BE) BOM
' þÿ' => 6,
// UTF-32 (BE) BOM as "WINDOWS-1252"
"��\0\0" => 4,
// UTF-32 (LE) BOM
'ÿþ ' => 6,
// UTF-32 (LE) BOM as "WINDOWS-1252"
"��" => 2,
// UTF-16 (BE) BOM
'þÿ' => 4,
// UTF-16 (BE) BOM as "WINDOWS-1252"
"��" => 2,
// UTF-16 (LE) BOM
'ÿþ' => 4,
];
We could tag the package 1.0.0 if we can make it crash on all features that currently are not supported.
I'm thinking of anonymous classes, deprecated functions, throwable and so on.
We can add more features with point releases.
Thoughts @hannesvdvreken?
Scalar type hints for arguments and return value could be converted into checks within the function itself, e.g:
function foo(string $bar) : string {
if (is_string($bar)) {
throw new \InvalidArgumentException(__FUNCTION__.' expects paramater 0 to be a string');
}
$return_val = 1;
if (!is_string($return_val)) {
throw new \UnexpectedValueException(__FUNCTION__.' expects return value to be a string');
}
return $return_val;
}
The package would be much easier to use if there was a console command.
I'm thinking something in the lines of:
7to5 convert <sourceDirectory> <destinationDirectory>
Right now the library can only convert one file. It would be very nice if an entire directory can be converted as well.
It only checks for the override flag for single files
line 91
for directories it eventually encounters the isEqual function
line 105 & 106
Just do it :-)
Is it OK to use this package for production or it has some negative implications? Are there some performance or security issues when comparing to "native" PHP 5 code?
Also, is PHP 7.2 supported and what version of PHP 5 is this (PHP 5.5, PHP 5.6)?
Hello!
Unfortunately, php7to5 spits out this error:
Call to undefined function Spatie\Php7to5\Console\ends_with() [...]
I looked into the code, there is no such function. I think that should be a quick fix..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.