Syntax parser visitors with back porting down to previous versions.
PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
composer.lock
phpunit.xml

README.md

PHP Parser Build Status

Syntax parser with back porting down to previous versions.

This library contains custom traversal visitors for use with nikic/PHP-Parser.

Table of contents

Install

This library may be consumed by using composer.

In your terminal, run:

$ composer require bhittani/php-parser

Usage

To utilize this library make sure you understand how nikic/PHP-Parser parses code.

This library contains a set of node visitors to manipulate php code.

Group import to individual imports

Back ports php 7+ syntax code.

use Bhittani\PhpParser\GroupToSingleImportsVisitor;

$traverser->addVisitor(new GroupToSingleImportsVisitor);

which will convert

use Grouped\Imports\{Acme, Foo\Bar}

into

use Grouped\Imports\Acme;
use Grouped\Imports\Foo\Bar;

Splat calls to call_user_func_array

Back ports php 5.6+ syntax code.

use Bhittani\PhpParser\SplatToCallUserFuncArrayVisitor;

$traverser->addVisitor(new SplatToCallUserFuncArrayVisitor);

which will convert

$val = my_func($a, 'b', ...$params);

into

$val = call_user_func_array('my_func', array_merge(array(
    $a, 'b'
), $params));

Class constants to strings

Back ports php 5.5+ syntax code.

use Bhittani\PhpParser\ClassConstToStrVisitor;

$traverser->addVisitor(new ClassConstToStrVisitor);

which will convert

use Acme\Foo\Bar;

$barClass = Bar::class;

into

use Acme\Foo\Bar;

$barClass = 'Acme\Foo\Bar';

Variadic to func_get_args

Back ports php 5.6+ syntax code.

use Bhittani\PhpParser\VariadicToFuncGetArgsVisitor;

$traverser->addVisitor(new VariadicToFuncGetArgsVisitor);

which will convert

function my_func($a, $b, ...$params)
{
    // my_func code
}

into

function my_func()
{
    $params = func_get_args();
    $a = array_shift($params);
    $b = array_shift($params);

    // my_func code
}

Remove imports

Removes all import statements.

use Bhittani\PhpParser\RemoveImportsVisitor;

$traverser->addVisitor(new RemoveImportsVisitor);

which will remove all use statements.

Append suffix

Appends a suffix to all imports, classes, traits, and interfaces.

use Bhittani\PhpParser\AppendSuffixVisitor;

$traverser->addVisitor(new AppendSuffixVisitor('_1'));

which will convert

<?php

namespace Company\Package;

use Acme\Foo;
use Acme\Bar\{Beep, Boop};

interface Contract {}

abstract class AnAbstract {}

trait OurTrait {}

class Person extends AnAbstract implements Contract, AnotherContract
{
    use OurTrait;

    use Their\Trait;

    public function handle(Beep $beep, Model\User $age)
    {
        $foo = new Foo();
        $bar = new Bar();
        if ($beep->boop()) {
            throw new Exception('Error thrown.');
        }
    }
}

into

<?php

namespace Company\Package;

use Acme\Foo_1 as Foo;
use Acme\Bar\{Beep_1 as Beep, Boop_1 as Boop};

interface Contract_1 {}

abstract class AnAbstract_1 {}

trait OurTrait_1 {}

class Person_1 extends AnAbstract_1 implements Contract_1, AnotherContract_1
{
    use OurTrait_1;

    use Their\Trait_1;

    public function handle(Beep $beep, Model\User_1 $age)
    {
        $foo = new Foo();
        $bar = new Bar_1();
        if ($beep->boop()) {
            throw new Exception('Error thrown.');
        }
    }
}

Notice that php's built-in entities won't be suffixed.

Test

Make sure you first CD into the library's root directory.

Do a composer install.

$ composer install

Run the tests.

$ vendor/bin/phpunit tests

or

$ composer test

Credits

This library would not be possible without making use of nikic/PHP-Parser.

License

This library is released under the MIT License.