PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.1
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.1
0.5.7 0.5.6 0.5.5 0.5.4 0.5.3 0.5.2 0.5.1 0.5.0 0.4.9 0.4.8 0.4.7 0.4.6 trunk 0.0.1 0.0.2 0.2.8 0.2.9 0.3.0 0.3.1 0.3.2 0.3.3 0.3.4 0.3.5 0.3.6 0.3.7 All 33 releases
code-engine / vendor / nikic / php-parser / README.md

README.md in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.4.1, at vendor/nikic/php-parser/README.md

234 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 PHP Parser
2 ==========
3
4 [](https://coveralls.io/github/nikic/PHP-Parser?branch=master![Coverage Status](https://coveralls.io/repos/github/nikic/PHP-Parser/badge.svg?branch=master)](https://coveralls.io/github/nikic/PHP-Parser?branch=master](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
5
6 This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and
7 manipulation.
8
9 [**Documentation for version 5.x**][doc_master] (current; for running on PHP >= 7.4; for parsing PHP 7.0 to PHP 8.3, with limited support for parsing PHP 5.x).
10
11 [Documentation for version 4.x][doc_4_x] (supported; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.3).
12
13 Features
14 --------
15
16 The main features provided by this library are:
17
18 * Parsing PHP 7, and PHP 8 code into an abstract syntax tree (AST).
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 * 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 [](https://getcomposer.orgcomposer](https://getcomposer.org](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:
38
39 ```php
40 <?php
41 use PhpParser\Error;
42 use PhpParser\NodeDumper;
43 use PhpParser\ParserFactory;
44
45 $code = <<<'CODE'
46 <?php
47
48 function test($foo)
49 {
50 var_dump($foo);
51 }
52 CODE;
53
54 $parser = (new ParserFactory())->createForNewestSupportedVersion();
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 ```
69 array(
70 0: Stmt_Function(
71 attrGroups: array(
72 )
73 byRef: false
74 name: Identifier(
75 name: test
76 )
77 params: array(
78 0: Param(
79 attrGroups: array(
80 )
81 flags: 0
82 type: null
83 byRef: false
84 variadic: false
85 var: Expr_Variable(
86 name: foo
87 )
88 default: null
89 )
90 )
91 returnType: null
92 stmts: array(
93 0: Stmt_Expression(
94 expr: Expr_FuncCall(
95 name: Name(
96 name: var_dump
97 )
98 args: array(
99 0: Arg(
100 name: null
101 value: Expr_Variable(
102 name: foo
103 )
104 byRef: false
105 unpack: false
106 )
107 )
108 )
109 )
110 )
111 )
112 )
113 ```
114
115 Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
116
117 ```php
118 use PhpParser\Node;
119 use PhpParser\Node\Stmt\Function_;
120 use PhpParser\NodeTraverser;
121 use PhpParser\NodeVisitorAbstract;
122
123 $traverser = new NodeTraverser();
124 $traverser->addVisitor(new class extends NodeVisitorAbstract {
125 public function enterNode(Node $node) {
126 if ($node instanceof Function_) {
127 // Clean out the function body
128 $node->stmts = [];
129 }
130 }
131 });
132
133 $ast = $traverser->traverse($ast);
134 echo $dumper->dump($ast) . "\n";
135 ```
136
137 This gives us an AST where the `Function_::$stmts` are empty:
138
139 ```
140 array(
141 0: Stmt_Function(
142 attrGroups: array(
143 )
144 byRef: false
145 name: Identifier(
146 name: test
147 )
148 params: array(
149 0: Param(
150 attrGroups: array(
151 )
152 type: null
153 byRef: false
154 variadic: false
155 var: Expr_Variable(
156 name: foo
157 )
158 default: null
159 )
160 )
161 returnType: null
162 stmts: array(
163 )
164 )
165 )
166 ```
167
168 Finally, we can convert the new AST back to PHP code:
169
170 ```php
171 use PhpParser\PrettyPrinter;
172
173 $prettyPrinter = new PrettyPrinter\Standard;
174 echo $prettyPrinter->prettyPrintFile($ast);
175 ```
176
177 This gives us our original code, minus the `var_dump()` call inside the function:
178
179 ```php
180 <?php
181
182 function test($foo)
183 {
184 }
185 ```
186
187 For a more comprehensive introduction, see the documentation.
188
189 Documentation
190 -------------
191
192 1. [](doc/0_Introduction.markdownIntroduction](doc/0_Introduction.markdown](doc/0_Introduction.markdown)
193 2. [](doc/2_Usage_of_basic_components.markdownUsage of basic components](doc/2_Usage_of_basic_components.markdown](doc/2_Usage_of_basic_components.markdown)
194
195 Component documentation:
196
197 * [](doc/component/Walking_the_AST.markdownWalking the AST](doc/component/Walking_the_AST.markdown](doc/component/Walking_the_AST.markdown)
198 * Node visitors
199 * Modifying the AST from a visitor
200 * Short-circuiting traversals
201 * Interleaved visitors
202 * Simple node finding API
203 * Parent and sibling references
204 * [](doc/component/Name_resolution.markdownName resolution](doc/component/Name_resolution.markdown](doc/component/Name_resolution.markdown)
205 * Name resolver options
206 * Name resolution context
207 * [](doc/component/Pretty_printing.markdownPretty printing](doc/component/Pretty_printing.markdown](doc/component/Pretty_printing.markdown)
208 * Converting AST back to PHP code
209 * Customizing formatting
210 * Formatting-preserving code transformations
211 * [](doc/component/AST_builders.markdownAST builders](doc/component/AST_builders.markdown](doc/component/AST_builders.markdown)
212 * Fluent builders for AST nodes
213 * [](doc/component/Lexer.markdownLexer](doc/component/Lexer.markdown](doc/component/Lexer.markdown)
214 * Emulation
215 * Tokens, positions and attributes
216 * [](doc/component/Error_handling.markdownError handling](doc/component/Error_handling.markdown](doc/component/Error_handling.markdown)
217 * Column information for errors
218 * Error recovery (parsing of syntactically incorrect code)
219 * [](doc/component/Constant_expression_evaluation.markdownConstant expression evaluation](doc/component/Constant_expression_evaluation.markdown](doc/component/Constant_expression_evaluation.markdown)
220 * Evaluating constant/property/etc initializers
221 * Handling errors and unsupported expressions
222 * [](doc/component/JSON_representation.markdownJSON representation](doc/component/JSON_representation.markdown](doc/component/JSON_representation.markdown)
223 * JSON encoding and decoding of ASTs
224 * [](doc/component/Performance.markdownPerformance](doc/component/Performance.markdown](doc/component/Performance.markdown)
225 * Disabling Xdebug
226 * Reusing objects
227 * Garbage collection impact
228 * [](doc/component/FAQ.markdownFrequently asked questions](doc/component/FAQ.markdown](doc/component/FAQ.markdown)
229 * Parent and sibling references
230
231 [doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
232 [doc_4_x]: https://github.com/nikic/PHP-Parser/tree/4.x/doc
233 [doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc
234