PHP code example of bhittani / php-parser

1. Go to this page and download the library: Download bhittani/php-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bhittani / php-parser example snippets


use Bhittani\PhpParser\GroupToSingleImportsVisitor;

$traverser->addVisitor(new GroupToSingleImportsVisitor);

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

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

use Bhittani\PhpParser\SplatToCallUserFuncArrayVisitor;

$traverser->addVisitor(new SplatToCallUserFuncArrayVisitor);

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

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

use Bhittani\PhpParser\ClassConstToStrVisitor;

$traverser->addVisitor(new ClassConstToStrVisitor);

use Acme\Foo\Bar;

$barClass = Bar::class;

use Acme\Foo\Bar;

$barClass = 'Acme\Foo\Bar';

use Bhittani\PhpParser\VariadicToFuncGetArgsVisitor;

$traverser->addVisitor(new VariadicToFuncGetArgsVisitor);

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

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

    // my_func code
}

use Bhittani\PhpParser\RemoveImportsVisitor;

$traverser->addVisitor(new RemoveImportsVisitor);

use Bhittani\PhpParser\AppendSuffixVisitor;

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



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.');
        }
    }
}



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.');
        }
    }
}
shell
$ composer