2017 © Pedro Peláez
 

library php-parser

A PHP parser written in PHP

image

nikic/php-parser

A PHP parser written in PHP

  • Sunday, July 22, 2018
  • by nikic
  • Repository
  • 217 Watchers
  • 7002 Stars
  • 58,759,605 Installations
  • PHP
  • 582 Dependents
  • 9 Suggesters
  • 510 Forks
  • 30 Open issues
  • 57 Versions
  • 9 % Grown

The README.md

PHP Parser

Coverage Status, (*1)

This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and manipulation., (*2)

Documentation for version 5.x (current; for running on PHP >= 7.4; for parsing PHP 7.0 to PHP 8.3, with limited support for parsing PHP 5.x)., (*3)

Documentation for version 4.x (supported; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.3)., (*4)

Features

The main features provided by this library are:, (*5)

  • Parsing PHP 7, and PHP 8 code into an abstract syntax tree (AST).
    • Invalid code can be parsed into a partial AST.
    • The AST contains accurate location information.
  • Dumping the AST in human-readable form.
  • Converting an AST back to PHP code.
    • Formatting can be preserved for partially changed ASTs.
  • Infrastructure to traverse and modify ASTs.
  • Resolution of namespaced names.
  • Evaluation of constant expressions.
  • Builders to simplify AST construction for code generation.
  • Converting an AST into JSON and back.

Quick Start

Install the library using composer:, (*6)

php composer.phar require nikic/php-parser

Parse some PHP code into an AST and dump the result in human-readable form:, (*7)

<?php
use PhpParser\Error;
use PhpParser\NodeDumper;
use PhpParser\ParserFactory;

$code = <<<'CODE'
<?php

function test($foo)
{
    var_dump($foo);
}
CODE;

$parser = (new ParserFactory())->createForNewestSupportedVersion();
try {
    $ast = $parser->parse($code);
} catch (Error $error) {
    echo "Parse error: {$error->getMessage()}\n";
    return;
}

$dumper = new NodeDumper;
echo $dumper->dump($ast) . "\n";

This dumps an AST looking something like this:, (*8)

array(
    0: Stmt_Function(
        attrGroups: array(
        )
        byRef: false
        name: Identifier(
            name: test
        )
        params: array(
            0: Param(
                attrGroups: array(
                )
                flags: 0
                type: null
                byRef: false
                variadic: false
                var: Expr_Variable(
                    name: foo
                )
                default: null
            )
        )
        returnType: null
        stmts: array(
            0: Stmt_Expression(
                expr: Expr_FuncCall(
                    name: Name(
                        name: var_dump
                    )
                    args: array(
                        0: Arg(
                            name: null
                            value: Expr_Variable(
                                name: foo
                            )
                            byRef: false
                            unpack: false
                        )
                    )
                )
            )
        )
    )
)

Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:, (*9)

use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;

$traverser = new NodeTraverser();
$traverser->addVisitor(new class extends NodeVisitorAbstract {
    public function enterNode(Node $node) {
        if ($node instanceof Function_) {
            // Clean out the function body
            $node->stmts = [];
        }
    }
});

$ast = $traverser->traverse($ast);
echo $dumper->dump($ast) . "\n";

This gives us an AST where the Function_::$stmts are empty:, (*10)

array(
    0: Stmt_Function(
        attrGroups: array(
        )
        byRef: false
        name: Identifier(
            name: test
        )
        params: array(
            0: Param(
                attrGroups: array(
                )
                type: null
                byRef: false
                variadic: false
                var: Expr_Variable(
                    name: foo
                )
                default: null
            )
        )
        returnType: null
        stmts: array(
        )
    )
)

Finally, we can convert the new AST back to PHP code:, (*11)

use PhpParser\PrettyPrinter;

$prettyPrinter = new PrettyPrinter\Standard;
echo $prettyPrinter->prettyPrintFile($ast);

This gives us our original code, minus the var_dump() call inside the function:, (*12)

<?php

function test($foo)
{
}

For a more comprehensive introduction, see the documentation., (*13)

Documentation

  1. Introduction
  2. Usage of basic components

Component documentation:, (*14)

  • Walking the AST
    • Node visitors
    • Modifying the AST from a visitor
    • Short-circuiting traversals
    • Interleaved visitors
    • Simple node finding API
    • Parent and sibling references
  • Name resolution
    • Name resolver options
    • Name resolution context
  • Pretty printing
    • Converting AST back to PHP code
    • Customizing formatting
    • Formatting-preserving code transformations
  • AST builders
    • Fluent builders for AST nodes
  • Lexer
    • Emulation
    • Tokens, positions and attributes
  • Error handling
    • Column information for errors
    • Error recovery (parsing of syntactically incorrect code)
  • Constant expression evaluation
    • Evaluating constant/property/etc initializers
    • Handling errors and unsupported expressions
  • JSON representation
    • JSON encoding and decoding of ASTs
  • Performance
    • Disabling Xdebug
    • Reusing objects
    • Garbage collection impact
  • Frequently asked questions
    • Parent and sibling references

The Versions

22/07 2018

dev-master

9999999-dev

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=7.0

 

The Development Requires

by Nikita Popov

parser php

15/07 2018

v4.0.3

4.0.3.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

03/06 2018

v4.0.2

4.0.2.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

25/03 2018

v4.0.1

4.0.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

28/02 2018

v4.0.0

4.0.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

28/02 2018

3.x-dev

3.9999999.9999999.9999999-dev

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

28/02 2018

v3.1.5

3.1.5.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

27/01 2018

v4.0.0beta1

4.0.0.0-beta1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

25/01 2018

v3.1.4

3.1.4.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

26/12 2017

v4.0.0alpha3

4.0.0.0-alpha3

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

26/12 2017

v3.1.3

3.1.3.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

10/11 2017

v4.0.0alpha2

4.0.0.0-alpha2

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

04/11 2017

v3.1.2

3.1.2.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

18/10 2017

v4.0.0alpha1

4.0.0.0-alpha1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

02/09 2017

v3.1.1

3.1.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

28/07 2017

v3.1.0

3.1.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

28/06 2017

v3.0.6

3.0.6.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

05/03 2017

v3.0.5

3.0.5.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

10/02 2017

v3.0.4

3.0.4.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

03/02 2017

v3.0.3

3.0.3.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

26/12 2016

dev-formatPreservingPrint

dev-formatPreservingPrint

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

23/12 2016

dev-identifiersAsNodes

dev-identifiersAsNodes

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

06/12 2016

v3.0.2

3.0.2.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

01/12 2016

v3.0.1

3.0.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

30/11 2016

v3.0.0

3.0.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

29/10 2016

v3.0.0beta2

3.0.0.0-beta2

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

22/10 2016

dev-nameString

dev-nameString

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

19/10 2016

dev-attributes

dev-attributes

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

16/09 2016

v3.0.0beta1

3.0.0.0-beta1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

16/09 2016

2.x-dev

2.9999999.9999999.9999999-dev

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

16/09 2016

v2.1.1

2.1.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

25/07 2016

v3.0.0alpha1

3.0.0.0-alpha1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

19/04 2016

v2.1.0

2.1.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

28/02 2016

v2.0.1

2.0.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

15/01 2016

1.x-dev

1.9999999.9999999.9999999-dev

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

04/12 2015

v2.0.0

2.0.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

21/10 2015

v2.0.0beta1

2.0.0.0-beta1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

The Development Requires

by Nikita Popov

parser php

19/09 2015

v1.4.1

1.4.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

14/07 2015

v2.0.0alpha1

2.0.0.0-alpha1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-tokenizer *

 

by Nikita Popov

parser php

14/07 2015

v1.4.0

1.4.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

02/05 2015

v1.3.0

1.3.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

03/04 2015

v1.2.2

1.2.2.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

24/03 2015

v1.2.1

1.2.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

22/03 2015

v1.2.0

1.2.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

18/01 2015

v1.1.0

1.1.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

04/11 2014

v1.0.2

1.0.2.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

14/10 2014

v1.0.1

1.0.1.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

12/09 2014

v1.0.0

1.0.0.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

31/08 2014

v1.0.0beta2

1.0.0.0-beta2

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

23/07 2014

0.9.x-dev

0.9.9999999.9999999-dev

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.2
  • ext-tokenizer *

 

by Nikita Popov

parser php

23/07 2014

v0.9.5

0.9.5.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.2
  • ext-tokenizer *

 

by Nikita Popov

parser php

27/03 2014

v1.0.0beta1

1.0.0.0-beta1

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3
  • ext-tokenizer *

 

by Nikita Popov

parser php

25/08 2013

v0.9.4

0.9.4.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.2

 

by Nikita Popov

parser php

22/11 2012

v0.9.3

0.9.3.0

A PHP parser written in PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.2

 

by Nikita Popov

parser php

07/07 2012

v0.9.2

0.9.2.0

A PHP parser written in PHP

  Sources   Download

BSD

The Requires

  • php >=5.2

 

by Nikita Popov

parser php

24/04 2012

v0.9.1

0.9.1.0

A PHP parser written in PHP

  Sources   Download

BSD

The Requires

  • php >=5.2

 

by Nikita Popov

parser php

05/01 2012

v0.9.0

0.9.0.0

A PHP parser written in PHP

  Sources   Download

BSD

The Requires

  • php >=5.2

 

by Nikita Popov

parser php