Giter VIP home page Giter VIP logo

7to5's People

Contributors

akoepcke avatar alexbowers avatar carusogabriel avatar fluxuator avatar freekmurze avatar hannesvdvreken avatar lasserafn avatar sebastiandedeyne avatar tamaspanczel avatar tjoosten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

7to5's Issues

Convert scalar type hints to additional PHP 5 checks

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

Arrays get reformatted

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]]];

Anonymous classes

I tried this approach for replacing anonymous classes:

$foo = new class {
    public function bar()
    {
        return 'bar';
    }
}

$foo->bar();

First attempt: JS style

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

Second attempt: declare a new class anyway, but give it a prefixed name

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)

Maybe another option: wrapping in one class with magic methods

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?

Undefined Function

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

Change minimum req to PHP 5.6

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.

Missing "symfony/finder" requirement

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.

Steps to reproduce

  • Download Composer from getcomposer.org and install it globally
  • Run composer global require spatie/7to5
  • Run php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code}

Expected result

Everything works

Actual result

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

Cause / Workaround

The package symfony/finder was not installed on my machine. Running composer global require symfony/finder solved the problem.

Class constants visibility

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)?

unicode characters are not handled correctly

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,
    ];

Suggestion: Convert type hints to checks in method

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;
}

Add console command

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>

Convert an entire directory

Right now the library can only convert one file. It would be very nice if an entire directory can be converted as well.

Usage in production

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)?

Undefined function ends_with

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

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.