comparison vendor/nikic/php-parser/README.md @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 PHP Parser 1 PHP Parser
2 ========== 2 ==========
3 3
4 [![Build Status](https://travis-ci.org/nikic/PHP-Parser.svg?branch=master)](https://travis-ci.org/nikic/PHP-Parser) [![Coverage Status](https://coveralls.io/repos/github/nikic/PHP-Parser/badge.svg?branch=master)](https://coveralls.io/github/nikic/PHP-Parser?branch=master) 4 [![Build Status](https://travis-ci.org/nikic/PHP-Parser.svg?branch=master)](https://travis-ci.org/nikic/PHP-Parser) [![Coverage Status](https://coveralls.io/repos/github/nikic/PHP-Parser/badge.svg?branch=master)](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
5 5
6 This is a PHP 5.2 to PHP 7.1 parser written in PHP. Its purpose is to simplify static code analysis and 6 This is a PHP 5.2 to PHP 7.2 parser written in PHP. Its purpose is to simplify static code analysis and
7 manipulation. 7 manipulation.
8 8
9 [**Documentation for version 3.x**][doc_master] (stable; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.1). 9 [**Documentation for version 4.x**][doc_master] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.2).
10 10
11 [Documentation for version 2.x][doc_2_x] (stable; for running on PHP >= 5.4; for parsing PHP 5.2 to PHP 7.0). 11 [Documentation for version 3.x][doc_3_x] (stable; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
12 12
13 [Documentation for version 1.x][doc_1_x] (unsupported; for running on PHP >= 5.3; for parsing PHP 5.2 to PHP 5.6). 13 Features
14 14 --------
15 In a Nutshell 15
16 ------------- 16 The main features provided by this library are:
17 17
18 The parser turns PHP source code into an abstract syntax tree. For example, if you pass the following code into the 18 * Parsing PHP 5 and PHP 7 code into an abstract syntax tree (AST).
19 parser: 19 * Invalid code can be parsed into a partial AST.
20 * The AST contains accurate location information.
21 * Dumping the AST in human-readable form.
22 * Converting an AST back to PHP code.
23 * Experimental: Formatting can be preserved for partially changed ASTs.
24 * Infrastructure to traverse and modify ASTs.
25 * Resolution of namespaced names.
26 * Evaluation of constant expressions.
27 * Builders to simplify AST construction for code generation.
28 * Converting an AST into JSON and back.
29
30 Quick Start
31 -----------
32
33 Install the library using [composer](https://getcomposer.org):
34
35 php composer.phar require nikic/php-parser
36
37 Parse some PHP code into an AST and dump the result in human-readable form:
20 38
21 ```php 39 ```php
22 <?php 40 <?php
23 echo 'Hi', 'World'; 41 use PhpParser\Error;
24 hello\world('foo', 'bar' . 'baz'); 42 use PhpParser\NodeDumper;
25 ``` 43 use PhpParser\ParserFactory;
26 44
27 You'll get a syntax tree looking roughly like this: 45 $code = <<<'CODE'
28 46 <?php
29 ```php 47
48 function test($foo)
49 {
50 var_dump($foo);
51 }
52 CODE;
53
54 $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
55 try {
56 $ast = $parser->parse($code);
57 } catch (Error $error) {
58 echo "Parse error: {$error->getMessage()}\n";
59 return;
60 }
61
62 $dumper = new NodeDumper;
63 echo $dumper->dump($ast) . "\n";
64 ```
65
66 This dumps an AST looking something like this:
67
68 ```
30 array( 69 array(
31 0: Stmt_Echo( 70 0: Stmt_Function(
32 exprs: array( 71 byRef: false
33 0: Scalar_String( 72 name: Identifier(
34 value: Hi 73 name: test
74 )
75 params: array(
76 0: Param(
77 type: null
78 byRef: false
79 variadic: false
80 var: Expr_Variable(
81 name: foo
82 )
83 default: null
35 ) 84 )
36 1: Scalar_String( 85 )
37 value: World 86 returnType: null
38 ) 87 stmts: array(
39 ) 88 0: Stmt_Expression(
40 ) 89 expr: Expr_FuncCall(
41 1: Expr_FuncCall( 90 name: Name(
42 name: Name( 91 parts: array(
43 parts: array( 92 0: var_dump
44 0: hello 93 )
45 1: world
46 )
47 )
48 args: array(
49 0: Arg(
50 value: Scalar_String(
51 value: foo
52 )
53 byRef: false
54 )
55 1: Arg(
56 value: Expr_Concat(
57 left: Scalar_String(
58 value: bar
59 ) 94 )
60 right: Scalar_String( 95 args: array(
61 value: baz 96 0: Arg(
97 value: Expr_Variable(
98 name: foo
99 )
100 byRef: false
101 unpack: false
102 )
62 ) 103 )
63 ) 104 )
64 byRef: false
65 ) 105 )
66 ) 106 )
67 ) 107 )
68 ) 108 )
69 ``` 109 ```
70 110
71 You can then work with this syntax tree, for example to statically analyze the code (e.g. to find 111 Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
72 programming errors or security issues). 112
73 113 ```php
74 Additionally, you can convert a syntax tree back to PHP code. This allows you to do code preprocessing 114 use PhpParser\Node;
75 (like automatedly porting code to older PHP versions). 115 use PhpParser\Node\Stmt\Function_;
76 116 use PhpParser\NodeTraverser;
77 Installation 117 use PhpParser\NodeVisitorAbstract;
78 ------------ 118
79 119 $traverser = new NodeTraverser();
80 The preferred installation method is [composer](https://getcomposer.org): 120 $traverser->addVisitor(new class extends NodeVisitorAbstract {
81 121 public function enterNode(Node $node) {
82 php composer.phar require nikic/php-parser 122 if ($node instanceof Function_) {
123 // Clean out the function body
124 $node->stmts = [];
125 }
126 }
127 });
128
129 $ast = $traverser->traverse($ast);
130 echo $dumper->dump($ast) . "\n";
131 ```
132
133 This gives us an AST where the `Function_::$stmts` are empty:
134
135 ```
136 array(
137 0: Stmt_Function(
138 byRef: false
139 name: Identifier(
140 name: test
141 )
142 params: array(
143 0: Param(
144 type: null
145 byRef: false
146 variadic: false
147 var: Expr_Variable(
148 name: foo
149 )
150 default: null
151 )
152 )
153 returnType: null
154 stmts: array(
155 )
156 )
157 )
158 ```
159
160 Finally, we can convert the new AST back to PHP code:
161
162 ```php
163 use PhpParser\PrettyPrinter;
164
165 $prettyPrinter = new PrettyPrinter\Standard;
166 echo $prettyPrinter->prettyPrintFile($ast);
167 ```
168
169 This gives us our original code, minus the `var_dump()` call inside the function:
170
171 ```php
172 <?php
173
174 function test($foo)
175 {
176 }
177 ```
178
179 For a more comprehensive introduction, see the documentation.
83 180
84 Documentation 181 Documentation
85 ------------- 182 -------------
86 183
87 1. [Introduction](doc/0_Introduction.markdown) 184 1. [Introduction](doc/0_Introduction.markdown)
88 2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown) 185 2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
89 3. [Other node tree representations](doc/3_Other_node_tree_representations.markdown)
90 4. [Code generation](doc/4_Code_generation.markdown)
91 186
92 Component documentation: 187 Component documentation:
93 188
94 1. [Error handling](doc/component/Error_handling.markdown) 189 * [Walking the AST](doc/component/Walking_the_AST.markdown)
95 2. [Lexer](doc/component/Lexer.markdown) 190 * Node visitors
96 191 * Modifying the AST from a visitor
97 [doc_1_x]: https://github.com/nikic/PHP-Parser/tree/1.x/doc 192 * Short-circuiting traversals
98 [doc_2_x]: https://github.com/nikic/PHP-Parser/tree/2.x/doc 193 * Interleaved visitors
194 * Simple node finding API
195 * Parent and sibling references
196 * [Name resolution](doc/component/Name_resolution.markdown)
197 * Name resolver options
198 * Name resolution context
199 * [Pretty printing](doc/component/Pretty_printing.markdown)
200 * Converting AST back to PHP code
201 * Customizing formatting
202 * Formatting-preserving code transformations
203 * [AST builders](component/AST_builders.markdown)
204 * Fluent builders for AST nodes
205 * [Lexer](doc/component/Lexer.markdown)
206 * Lexer options
207 * Token and file positions for nodes
208 * Custom attributes
209 * [Error handling](doc/component/Error_handling.markdown)
210 * Column information for errors
211 * Error recovery (parsing of syntactically incorrect code)
212 * [Constant expression evaluation](component/Constant_expression_evaluation.markdown)
213 * Evaluating constant/property/etc initializers
214 * Handling errors and unsupported expressions
215 * [JSON representation](component/JSON_representation.markdown)
216 * JSON encoding and decoding of ASTs
217 * [Performance](doc/component/Performance.markdown)
218 * Disabling XDebug
219 * Reusing objects
220 * Garbage collection impact
221 * [Frequently asked questions](component/FAQ.markdown)
222 * Parent and sibling references
223
224 [doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
99 [doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc 225 [doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc