mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-06-18 13:50:20 +02:00
Compare commits
178 Commits
v4.0.0alph
...
v4.2.2
Author | SHA1 | Date | |
---|---|---|---|
1bd73cc04c | |||
94d93f27a5 | |||
a167aa2061 | |||
993f29906b | |||
9d44edf85d | |||
aa97a9bb69 | |||
aa72c5d674 | |||
60d025a914 | |||
b2cecec6bc | |||
8012faea54 | |||
f3b19c19ef | |||
78d9985d11 | |||
57b8673ea7 | |||
5221f49a60 | |||
ae4e90d558 | |||
9de96821f7 | |||
b7e6361536 | |||
594bcae1fc | |||
d5180f0d95 | |||
1f95f9215c | |||
6b9dd7afe7 | |||
ba092652fe | |||
ec0d834c5f | |||
ea3657fc5f | |||
af8c729603 | |||
0ef61b49bb | |||
90ee36a7fc | |||
8aae5b59b8 | |||
49d73e829f | |||
80ead71da2 | |||
294b93fbca | |||
bc3ac5e5ea | |||
382ca0128f | |||
f42bbc2403 | |||
ebf6b1c33b | |||
a74b54ce8b | |||
8e4f972036 | |||
3d0f7843d4 | |||
69068722b7 | |||
fb3d89e463 | |||
d0230c5c77 | |||
dc323458b4 | |||
674c5610fb | |||
4fb48c0e18 | |||
d638dd9b25 | |||
c5316487a4 | |||
eebaa94647 | |||
0ed9065b4c | |||
5f73c4de80 | |||
146411bb86 | |||
e12891a9db | |||
a5c726bfbf | |||
fd93690705 | |||
83766c8c0e | |||
fa6ee28600 | |||
16c5d107ca | |||
d7d581c1d2 | |||
1973b6633a | |||
7f96481c80 | |||
41408081d7 | |||
4d6825c460 | |||
d4a5a6e84b | |||
c55798ccc6 | |||
82e8b33814 | |||
8d9ae46597 | |||
f6fc248ca3 | |||
84dcd179d2 | |||
3e02eb7108 | |||
2f67429ec9 | |||
0cd7207ca6 | |||
6751ac3c9d | |||
5aeb884ab5 | |||
b855c1ca23 | |||
53c732a96d | |||
bd088dc940 | |||
8b1c9c0409 | |||
df64d86cf4 | |||
aac539ef0a | |||
f967b867d5 | |||
6bbdaf6c16 | |||
35b8caf75e | |||
6526ea2497 | |||
3ce5e628a7 | |||
ee870e3464 | |||
21d27527ed | |||
7b201b63d2 | |||
01e85a26c6 | |||
e6452e8d15 | |||
9202d87f53 | |||
fa99c3fbfb | |||
2b0bd657bb | |||
81b7be3ba6 | |||
6121001576 | |||
7208b1c7ac | |||
e4a54fa90a | |||
3125b54e5e | |||
7f8ff1b9a4 | |||
91a462ce76 | |||
610617fe93 | |||
ff2d85dc6b | |||
b998d1e9b5 | |||
6aba7624ed | |||
9c18e3db49 | |||
ae52aadb43 | |||
9cea94000a | |||
bb87e28e7d | |||
7484acb88b | |||
bc5ba47b28 | |||
b9996315a6 | |||
de3470190c | |||
1c3eabb000 | |||
8d1e86b47f | |||
ff10cc9d08 | |||
14454326e9 | |||
3a45c1a121 | |||
ad9c42b66a | |||
08215e7646 | |||
c18bb27723 | |||
dd0adcc96c | |||
e4505de346 | |||
a513ccabb7 | |||
d817818b5d | |||
6a273c9fbd | |||
c2d3ecad35 | |||
e57b3a0978 | |||
1cdb280a30 | |||
d01fafcb40 | |||
67df02c844 | |||
b85b6b3519 | |||
94c715d97e | |||
4dacbb8d39 | |||
aa685e711a | |||
edafeb85c4 | |||
68d07c4662 | |||
8fae99aafe | |||
c7ada124d0 | |||
f6617e6d25 | |||
2499534729 | |||
e0a2043089 | |||
e6e52abae7 | |||
7f72c84122 | |||
fc8ac71e76 | |||
a8968caa5b | |||
5285df8f22 | |||
4366aa2fb0 | |||
bf7d811cda | |||
248b29ecf6 | |||
bcb45d31eb | |||
3bc31488ce | |||
92b39e3d1f | |||
1c7fd314d1 | |||
fb8175567e | |||
63abf9cb3f | |||
ceb4932ca8 | |||
aa289c9694 | |||
579f4ce846 | |||
8d3cb5f57b | |||
de82a40d04 | |||
a86151f24f | |||
e6e8791848 | |||
83b958763f | |||
4dbb02c57b | |||
4fcdac40d1 | |||
04feb90d79 | |||
dc3ace55c3 | |||
e5453f0d46 | |||
b507fa43da | |||
336a49b428 | |||
fa174b093f | |||
94ca9a7ab9 | |||
0bb74e03aa | |||
8d3f48ab75 | |||
1c11626f0a | |||
05e2cd287e | |||
bac91b426e | |||
a75164c77e | |||
c59e75f873 | |||
4392a7b164 |
@ -1,5 +1,5 @@
|
||||
language: php
|
||||
dist: trusty
|
||||
dist: xenial
|
||||
sudo: false
|
||||
|
||||
cache:
|
||||
@ -10,11 +10,13 @@ php:
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
- 7.3
|
||||
- 7.4snapshot
|
||||
- nightly
|
||||
|
||||
install:
|
||||
- if [ $TRAVIS_PHP_VERSION = '7.0' ]; then composer require satooshi/php-coveralls '~1.0'; fi
|
||||
- composer install --prefer-dist
|
||||
- composer install --prefer-dist --ignore-platform-reqs
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
@ -23,7 +25,7 @@ matrix:
|
||||
|
||||
script:
|
||||
- if [ $TRAVIS_PHP_VERSION = '7.0' ]; then vendor/bin/phpunit --coverage-clover build/logs/clover.xml; else vendor/bin/phpunit; fi
|
||||
- if [ $TRAVIS_PHP_VERSION = '7.1' ]; then test_old/run-php-src.sh; fi
|
||||
- if [ $TRAVIS_PHP_VERSION = '7.2' ]; then test_old/run-php-src.sh; fi
|
||||
|
||||
after_success:
|
||||
- if [ $TRAVIS_PHP_VERSION = '7.0' ]; then php vendor/bin/coveralls; fi
|
||||
|
194
CHANGELOG.md
194
CHANGELOG.md
@ -1,8 +1,170 @@
|
||||
Version 4.0.0-dev
|
||||
Version 4.2.3-dev
|
||||
-----------------
|
||||
|
||||
Nothing yet.
|
||||
|
||||
Version 4.2.2 (2019-05-25)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for arrow functions using a new `Expr\ArrowFunction` node. (#602)
|
||||
* [PHP 7.4] Add support for array spreads, using a new `unpack` subnode on `ArrayItem`. (#609)
|
||||
* Added support for inserting into empty list nodes in the formatting preserving pretty printer.
|
||||
|
||||
### Changed
|
||||
|
||||
* `php-parse` will now print messages to stderr, so that stdout only contains the actual result of
|
||||
the operation (such as a JSON dump). (#605)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed attribute assignment for zero-length nop statements, and a related assertion failure in
|
||||
the formatting-preserving pretty printer. (#589)
|
||||
|
||||
Version 4.2.1 (2019-02-16)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for `??=` operator through a new `AssignOp\Coalesce` node. (#575)
|
||||
|
||||
Version 4.2.0 (2019-01-12)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for typed properties through a new `type` subnode of `Stmt\Property`.
|
||||
Additionally `Builder\Property` now has a `setType()` method. (#567)
|
||||
* Add `kind` attribute to `Cast\Double_`, which allows to distinguish between `(float)`,
|
||||
`(double)` and `(real)`. The form of the cast will be preserved by the pretty printer. (#565)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Remove assertion when pretty printing anonymous class with a name (#554).
|
||||
|
||||
Version 4.1.1 (2018-12-26)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fix "undefined offset" notice when parsing specific malformed code (#551).
|
||||
|
||||
### Added
|
||||
|
||||
* Support error recovery for missing return type (`function foo() : {}`) (#544).
|
||||
|
||||
Version 4.1.0 (2018-10-10)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for PHP 7.3 flexible heredoc/nowdoc strings, completing support for PHP 7.3. There
|
||||
are two caveats for this feature:
|
||||
* In some rare, pathological cases flexible heredoc/nowdoc strings change the interpretation of
|
||||
existing doc strings. PHP-Parser will now use the new interpretation.
|
||||
* Flexible heredoc/nowdoc strings require special support from the lexer. Because this is not
|
||||
available on PHP versions before 7.3, support has to be emulated. This emulation is not perfect
|
||||
and some cases which we do not expect to occur in practice (such as flexible doc strings being
|
||||
nested within each other through abuse of variable-variable interpolation syntax) may not be
|
||||
recognized correctly.
|
||||
* Added `DONT_TRAVERSE_CURRENT_AND_CHILDREN` to `NodeTraverser` to skip both traversal of child
|
||||
nodes, and prevent subsequent visitors from visiting the current node.
|
||||
|
||||
Version 4.0.4 (2018-09-18)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* The following methods have been added to `BuilderFactory`:
|
||||
* `useTrait()` (fluent builder)
|
||||
* `traitUseAdaptation()` (fluent builder)
|
||||
* `useFunction()` (fluent builder)
|
||||
* `useConst()` (fluent builder)
|
||||
* `var()`
|
||||
* `propertyFetch()`
|
||||
|
||||
### Deprecated
|
||||
|
||||
* `Builder\Param::setTypeHint()` has been deprecated in favor of the newly introduced
|
||||
`Builder\Param::setType()`.
|
||||
|
||||
Version 4.0.3 (2018-07-15)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed possible undefined offset notice in formatting-preserving printer. (#513)
|
||||
|
||||
### Added
|
||||
|
||||
* Improved error recovery inside arrays.
|
||||
* Preserve trailing comment inside classes. **Note:** This change is possibly BC breaking if your
|
||||
code validates that classes can only contain certain statement types. After this change, classes
|
||||
can also contain Nop statements, while this was not previously possible. (#509)
|
||||
|
||||
Version 4.0.2 (2018-06-03)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Improved error recovery inside classes.
|
||||
* Support error recovery for `foreach` without `as`.
|
||||
* Support error recovery for parameters without variable (`function (Type ) {}`).
|
||||
* Support error recovery for functions without body (`function ($foo)`).
|
||||
|
||||
Version 4.0.1 (2018-03-25)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.3] Added support for trailing commas in function calls.
|
||||
* [PHP 7.3] Added support for by-reference array destructuring.
|
||||
* Added checks to node traverser to prevent replacing a statement with an expression or vice versa.
|
||||
This should prevent common mistakes in the implementation of node visitors.
|
||||
* Added the following methods to `BuilderFactory`, to simplify creation of expressions:
|
||||
* `funcCall()`
|
||||
* `methodCall()`
|
||||
* `staticCall()`
|
||||
* `new()`
|
||||
* `constFetch()`
|
||||
* `classConstFetch()`
|
||||
|
||||
Version 4.0.0 (2018-02-28)
|
||||
--------------------------
|
||||
|
||||
* No significant code changes since the beta 1 release.
|
||||
|
||||
Version 4.0.0-beta1 (2018-01-27)
|
||||
--------------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* In formatting-preserving pretty printer: Fixed indentation when inserting into lists. (#466)
|
||||
|
||||
### Added
|
||||
|
||||
* In formatting-preserving pretty printer: Improved formatting of elements inserted into multi-line
|
||||
arrays.
|
||||
|
||||
### Removed
|
||||
|
||||
* The `Autoloader` class has been removed. It is now required to use the Composer autoloader.
|
||||
|
||||
Version 4.0.0-alpha3 (2017-12-26)
|
||||
---------------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* In the formatting-preserving pretty printer:
|
||||
* Fixed comment indentation.
|
||||
* Fixed handling of inline HTML in the fallback case.
|
||||
* Fixed insertion into list nodes that require creation of a code block.
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for inserting at the start of list nodes in formatting-preserving pretty printer.
|
||||
|
||||
Version 4.0.0-alpha2 (2017-11-10)
|
||||
---------------------------------
|
||||
|
||||
@ -78,10 +240,28 @@ Version 4.0.0-alpha1 (2017-10-18)
|
||||
* The `BuilderAbstract` class has been removed. It's functionality is moved into `BuilderHelpers`.
|
||||
However, this is an internal class and should not be used directly.
|
||||
|
||||
Version 3.1.3-dev
|
||||
-----------------
|
||||
Version 3.1.5 (2018-02-28)
|
||||
--------------------------
|
||||
|
||||
Nothing yet.
|
||||
### Fixed
|
||||
|
||||
* Fixed duplicate comment assignment in switch statements. (#469)
|
||||
* Improve compatibility with PHP-Scoper. (#477)
|
||||
|
||||
Version 3.1.4 (2018-01-25)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed pretty printing of `-(-$x)` and `+(+$x)`. (#459)
|
||||
|
||||
Version 3.1.3 (2017-12-26)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Improve compatibility with php-scoper, by supporting prefixed namespaces in
|
||||
`NodeAbstract::getType()`.
|
||||
|
||||
Version 3.1.2 (2017-11-04)
|
||||
--------------------------
|
||||
@ -112,7 +292,7 @@ Version 3.1.0 (2017-07-28)
|
||||
* [PHP 7.2] Added support for trailing comma in group use statements.
|
||||
* [PHP 7.2] Added support for `object` type. This means `object` types will now be represented as a
|
||||
builtin type (a simple `"object"` string), rather than a class `Name`.
|
||||
|
||||
|
||||
### Fixed
|
||||
|
||||
* Floating-point numbers are now printed correctly if the LC_NUMERIC locale uses a comma as decimal
|
||||
@ -232,7 +412,7 @@ This release primarily improves our support for error recovery.
|
||||
`NameResolver::__construct()`.
|
||||
* The `NameResolver` now adds a `namespacedName` attribute on name nodes that cannot be statically
|
||||
resolved (unqualified unaliased function or constant names in namespaces).
|
||||
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed attribute assignment for `GroupUse` prefix and variables in interpolated strings.
|
||||
@ -476,4 +656,4 @@ A more detailed description of backwards incompatible changes can be found in th
|
||||
|
||||
**This changelog only includes changes from the 2.0 series. For older changes see the
|
||||
[1.x series changelog](https://github.com/nikic/PHP-Parser/blob/1.x/CHANGELOG.md) and the
|
||||
[0.9 series changelog](https://github.com/nikic/PHP-Parser/blob/0.9/CHANGELOG.md).**
|
||||
[0.9 series changelog](https://github.com/nikic/PHP-Parser/blob/0.9/CHANGELOG.md).**
|
||||
|
4
LICENSE
4
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright (c) 2011 by Nikita Popov.
|
||||
Copyright (c) 2011-2018 by Nikita Popov.
|
||||
|
||||
Some rights reserved.
|
||||
|
||||
@ -28,4 +28,4 @@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
25
README.md
25
README.md
@ -3,12 +3,12 @@ PHP Parser
|
||||
|
||||
[](https://travis-ci.org/nikic/PHP-Parser) [](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
|
||||
|
||||
This is a PHP 5.2 to PHP 7.2 parser written in PHP. Its purpose is to simplify static code analysis and
|
||||
This is a PHP 5.2 to PHP 7.3 parser written in PHP. Its purpose is to simplify static code analysis and
|
||||
manipulation.
|
||||
|
||||
[**Documentation for version 3.x**][doc_3_x] (stable; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
|
||||
[**Documentation for version 4.x**][doc_master] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.3).
|
||||
|
||||
[Documentation for version 4.x][doc_master] (development; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.2).
|
||||
[Documentation for version 3.x][doc_3_x] (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
|
||||
|
||||
Features
|
||||
--------
|
||||
@ -183,12 +183,16 @@ Documentation
|
||||
|
||||
1. [Introduction](doc/0_Introduction.markdown)
|
||||
2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
|
||||
3. [Other node tree representations](doc/3_Other_node_tree_representations.markdown)
|
||||
4. [Code generation](doc/4_Code_generation.markdown)
|
||||
5. [Frequently asked questions](doc/5_FAQ.markdown)
|
||||
|
||||
Component documentation:
|
||||
|
||||
* [Walking the AST](doc/component/Walking_the_AST.markdown)
|
||||
* Node visitors
|
||||
* Modifying the AST from a visitor
|
||||
* Short-circuiting traversals
|
||||
* Interleaved visitors
|
||||
* Simple node finding API
|
||||
* Parent and sibling references
|
||||
* [Name resolution](doc/component/Name_resolution.markdown)
|
||||
* Name resolver options
|
||||
* Name resolution context
|
||||
@ -196,6 +200,8 @@ Component documentation:
|
||||
* Converting AST back to PHP code
|
||||
* Customizing formatting
|
||||
* Formatting-preserving code transformations
|
||||
* [AST builders](doc/component/AST_builders.markdown)
|
||||
* Fluent builders for AST nodes
|
||||
* [Lexer](doc/component/Lexer.markdown)
|
||||
* Lexer options
|
||||
* Token and file positions for nodes
|
||||
@ -203,10 +209,17 @@ Component documentation:
|
||||
* [Error handling](doc/component/Error_handling.markdown)
|
||||
* Column information for errors
|
||||
* Error recovery (parsing of syntactically incorrect code)
|
||||
* [Constant expression evaluation](doc/component/Constant_expression_evaluation.markdown)
|
||||
* Evaluating constant/property/etc initializers
|
||||
* Handling errors and unsupported expressions
|
||||
* [JSON representation](doc/component/JSON_representation.markdown)
|
||||
* JSON encoding and decoding of ASTs
|
||||
* [Performance](doc/component/Performance.markdown)
|
||||
* Disabling XDebug
|
||||
* Reusing objects
|
||||
* Garbage collection impact
|
||||
* [Frequently asked questions](doc/component/FAQ.markdown)
|
||||
* Parent and sibling references
|
||||
|
||||
[doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
|
||||
[doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc
|
||||
|
@ -4,7 +4,7 @@ Upgrading from PHP-Parser 2.x to 3.0
|
||||
The backwards-incompatible changes in this release may be summarized as follows:
|
||||
|
||||
* The specific details of the node representation have changed in some cases, primarily to
|
||||
accomodate new PHP 7.1 features.
|
||||
accommodate new PHP 7.1 features.
|
||||
* There have been significant changes to the error recovery implementation. This may affect you,
|
||||
if you used the error recovery mode or have a custom lexer implementation.
|
||||
* A number of deprecated methods were removed.
|
||||
|
@ -6,7 +6,7 @@ Upgrading from PHP-Parser 3.x to 4.0
|
||||
PHP-Parser now requires PHP 7.0 or newer to run. It is however still possible to *parse* PHP 5.2-5.6
|
||||
source code, while running on a newer version.
|
||||
|
||||
Because HHVM does not support PHP 7, HHVM is no longer supported.
|
||||
HHVM is no longer actively supported.
|
||||
|
||||
### Changes to the node structure
|
||||
|
||||
@ -17,7 +17,7 @@ Because HHVM does not support PHP 7, HHVM is no longer supported.
|
||||
comparisons or strict-mode may require adjustment. The following is an exhaustive list of all
|
||||
affected subnodes:
|
||||
|
||||
* `Const::$name`
|
||||
* `Const_::$name`
|
||||
* `NullableType::$type` (for simple types)
|
||||
* `Param::$type` (for simple types)
|
||||
* `Expr\ClassConstFetch::$name`
|
||||
@ -29,8 +29,8 @@ Because HHVM does not support PHP 7, HHVM is no longer supported.
|
||||
* `Stmt\Class_::$name`
|
||||
* `Stmt\ClassMethod::$name`
|
||||
* `Stmt\ClassMethod::$returnType` (for simple types)
|
||||
* `Stmt\Function::$name`
|
||||
* `Stmt\Function::$returnType` (for simple types)
|
||||
* `Stmt\Function_::$name`
|
||||
* `Stmt\Function_::$returnType` (for simple types)
|
||||
* `Stmt\Goto_::$name`
|
||||
* `Stmt\Interface_::$name`
|
||||
* `Stmt\Label::$name`
|
||||
@ -48,11 +48,11 @@ Because HHVM does not support PHP 7, HHVM is no longer supported.
|
||||
* The `name` subnode of `StaticVar` has been renamed to `var` and now contains a `Variable` rather
|
||||
than a plain string.
|
||||
* The `var` subnode of `ClosureUse` now contains a `Variable` rather than a plain string.
|
||||
* The `var` subnode of `Catch` now contains a `Variable` rather than a plain string.
|
||||
* The `var` subnode of `Catch_` now contains a `Variable` rather than a plain string.
|
||||
* The `alias` subnode of `UseUse` is now `null` if no explicit alias is given. As such,
|
||||
`use Foo\Bar` and `use Foo\Bar as Bar` are now represented differently. The `getAlias()` method
|
||||
can be used to get the effective alias, even if it is not explicitly given.
|
||||
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
* The indentation handling in the pretty printer has been changed (this is only relevant if you
|
||||
@ -62,7 +62,7 @@ Because HHVM does not support PHP 7, HHVM is no longer supported.
|
||||
|
||||
### Removed functionality
|
||||
|
||||
* Removed `type` subnode on `Class`, `ClassMethod` and `Property` nodes. Use `flags` instead.
|
||||
* Removed `type` subnode on `Class_`, `ClassMethod` and `Property` nodes. Use `flags` instead.
|
||||
* The `ClassConst::isStatic()` method has been removed. Constants cannot have a static modifier.
|
||||
* The `NodeTraverser` no longer accepts `false` as a return value from a `leaveNode()` method.
|
||||
`NodeTraverser::REMOVE_NODE` should be returned instead.
|
||||
@ -73,4 +73,5 @@ Because HHVM does not support PHP 7, HHVM is no longer supported.
|
||||
* The XML serializer has been removed. As such, the classes `Serializer\XML`, and
|
||||
`Unserializer\XML`, as well as the interfaces `Serializer` and `Unserializer` no longer exist.
|
||||
* The `BuilderAbstract` class has been removed. It's functionality is moved into `BuilderHelpers`.
|
||||
However, this is an internal class and should not be used directly.
|
||||
However, this is an internal class and should not be used directly.
|
||||
* The `Autoloader` class has been removed in favor of relying on the Composer autoloader.
|
||||
|
@ -45,14 +45,15 @@ $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
|
||||
foreach ($files as $file) {
|
||||
if (strpos($file, '<?php') === 0) {
|
||||
$code = $file;
|
||||
echo "====> Code $code\n";
|
||||
fwrite(STDERR, "====> Code $code\n");
|
||||
} else {
|
||||
if (!file_exists($file)) {
|
||||
die("File $file does not exist.\n");
|
||||
fwrite(STDERR, "File $file does not exist.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$code = file_get_contents($file);
|
||||
echo "====> File $file:\n";
|
||||
fwrite(STDERR, "====> File $file:\n");
|
||||
}
|
||||
|
||||
if ($attributes['with-recovery']) {
|
||||
@ -60,7 +61,7 @@ foreach ($files as $file) {
|
||||
$stmts = $parser->parse($code, $errorHandler);
|
||||
foreach ($errorHandler->getErrors() as $error) {
|
||||
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
|
||||
echo $message . "\n";
|
||||
fwrite(STDERR, $message . "\n");
|
||||
}
|
||||
if (null === $stmts) {
|
||||
continue;
|
||||
@ -70,22 +71,26 @@ foreach ($files as $file) {
|
||||
$stmts = $parser->parse($code);
|
||||
} catch (PhpParser\Error $error) {
|
||||
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
|
||||
die($message . "\n");
|
||||
fwrite(STDERR, $message . "\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($operations as $operation) {
|
||||
if ('dump' === $operation) {
|
||||
echo "==> Node dump:\n";
|
||||
fwrite(STDERR, "==> Node dump:\n");
|
||||
echo $dumper->dump($stmts, $code), "\n";
|
||||
} elseif ('pretty-print' === $operation) {
|
||||
echo "==> Pretty print:\n";
|
||||
fwrite(STDERR, "==> Pretty print:\n");
|
||||
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
|
||||
} elseif ('json-dump' === $operation) {
|
||||
fwrite(STDERR, "==> JSON dump:\n");
|
||||
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
||||
} elseif ('var-dump' === $operation) {
|
||||
echo "==> var_dump():\n";
|
||||
fwrite(STDERR, "==> var_dump():\n");
|
||||
var_dump($stmts);
|
||||
} elseif ('resolve-names' === $operation) {
|
||||
echo "==> Resolved names.\n";
|
||||
fwrite(STDERR, "==> Resolved names.\n");
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
}
|
||||
}
|
||||
@ -101,9 +106,9 @@ function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
|
||||
|
||||
function showHelp($error = '') {
|
||||
if ($error) {
|
||||
echo $error . "\n\n";
|
||||
fwrite(STDERR, $error . "\n\n");
|
||||
}
|
||||
die(<<<OUTPUT
|
||||
fwrite($error ? STDERR : STDOUT, <<<OUTPUT
|
||||
Usage: php-parse [operations] file1.php [file2.php ...]
|
||||
or: php-parse [operations] "<?php code"
|
||||
Turn PHP source code into an abstract syntax tree.
|
||||
@ -112,6 +117,7 @@ Operations is a list of the following options (--dump by default):
|
||||
|
||||
-d, --dump Dump nodes using NodeDumper
|
||||
-p, --pretty-print Pretty print file using PrettyPrinter\Standard
|
||||
-j, --json-dump Print json_encode() result
|
||||
--var-dump var_dump() nodes (for exact structure)
|
||||
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
|
||||
-c, --with-column-info Show column-numbers for errors (if available)
|
||||
@ -127,6 +133,7 @@ Example:
|
||||
|
||||
OUTPUT
|
||||
);
|
||||
exit($error ? 1 : 0);
|
||||
}
|
||||
|
||||
function parseArgs($args) {
|
||||
@ -155,8 +162,9 @@ function parseArgs($args) {
|
||||
case '-p':
|
||||
$operations[] = 'pretty-print';
|
||||
break;
|
||||
case '--serialize-xml':
|
||||
$operations[] = 'serialize-xml';
|
||||
case '--json-dump':
|
||||
case '-j':
|
||||
$operations[] = 'json-dump';
|
||||
break;
|
||||
case '--var-dump':
|
||||
$operations[] = 'var-dump';
|
||||
|
@ -1,8 +1,11 @@
|
||||
{
|
||||
"name": "nikic/php-parser",
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": ["php", "parser"],
|
||||
"type": "library",
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": [
|
||||
"php",
|
||||
"parser"
|
||||
],
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
@ -14,17 +17,24 @@
|
||||
"ext-tokenizer": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6"
|
||||
"phpunit/phpunit": "^6.5 || ^7.0"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "4.2-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhpParser\\": "lib/PhpParser"
|
||||
}
|
||||
},
|
||||
"bin": ["bin/php-parse"],
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "4.0-dev"
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"PhpParser\\": "test/PhpParser/"
|
||||
}
|
||||
}
|
||||
},
|
||||
"bin": [
|
||||
"bin/php-parse"
|
||||
]
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
Introduction
|
||||
============
|
||||
|
||||
This project is a PHP 5.2 to PHP 7.1 parser **written in PHP itself**.
|
||||
This project is a PHP 5.2 to PHP 7.3 parser **written in PHP itself**.
|
||||
|
||||
What is this for?
|
||||
-----------------
|
||||
@ -14,7 +14,7 @@ There are other ways of processing source code. One that PHP supports natively i
|
||||
token stream generated by [`token_get_all`][2]. The token stream is much more low level than
|
||||
the AST and thus has different applications: It allows to also analyze the exact formatting of
|
||||
a file. On the other hand the token stream is much harder to deal with for more complex analysis.
|
||||
For example an AST abstracts away the fact that in PHP variables can be written as `$foo`, but also
|
||||
For example, an AST abstracts away the fact that, in PHP, variables can be written as `$foo`, but also
|
||||
as `$$bar`, `${'foobar'}` or even `${!${''}=barfoo()}`. You don't have to worry about recognizing
|
||||
all the different syntaxes from a stream of tokens.
|
||||
|
||||
@ -26,17 +26,17 @@ programmatic PHP code analysis are incidentally PHP developers, not C developers
|
||||
What can it parse?
|
||||
------------------
|
||||
|
||||
The parser supports parsing PHP 5.2-5.6 and PHP 7.
|
||||
The parser supports parsing PHP 5.2-7.3.
|
||||
|
||||
As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP
|
||||
version it runs on), additionally a wrapper for emulating tokens from newer versions is provided.
|
||||
This allows to parse PHP 7.1 source code running on PHP 5.5, for example. This emulation is somewhat
|
||||
This allows to parse PHP 7.3 source code running on PHP 7.0, for example. This emulation is somewhat
|
||||
hacky and not perfect, but it should work well on any sane code.
|
||||
|
||||
What output does it produce?
|
||||
----------------------------
|
||||
|
||||
The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks like
|
||||
The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks
|
||||
can best be seen in an example. The program `<?php echo 'Hi', 'World';` will give you a node tree
|
||||
roughly looking like this:
|
||||
|
||||
|
@ -41,7 +41,7 @@ Kind | Behavior
|
||||
`ParserFactory::ONLY_PHP7` | Parse code as PHP 7.
|
||||
`ParserFactory::ONLY_PHP5` | Parse code as PHP 5.
|
||||
|
||||
Unless you have strong reason to use something else, `PREFER_PHP7` is a reasonable default.
|
||||
Unless you have a strong reason to use something else, `PREFER_PHP7` is a reasonable default.
|
||||
|
||||
The `create()` method optionally accepts a `Lexer` instance as the second argument. Some use cases
|
||||
that require customized lexers are discussed in the [lexer documentation](component/Lexer.markdown).
|
||||
@ -50,10 +50,18 @@ Subsequently you can pass PHP code (including the opening `<?php` tag) to the `p
|
||||
create a syntax tree. If a syntax error is encountered, an `PhpParser\Error` exception will be thrown:
|
||||
|
||||
```php
|
||||
<?php
|
||||
use PhpParser\Error;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$code = '<?php // some code';
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
printLine('Hello World!!!');
|
||||
CODE;
|
||||
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
|
||||
try {
|
||||
@ -66,27 +74,68 @@ try {
|
||||
|
||||
A parser instance can be reused to parse multiple files.
|
||||
|
||||
Node tree
|
||||
---------
|
||||
Node dumping
|
||||
------------
|
||||
|
||||
If you use the above code with `$code = "<?php echo 'Hi ', hi\\getTarget();"` the parser will
|
||||
generate a node tree looking like this:
|
||||
To dump the abstact syntax tree in human readable form, a `NodeDumper` can be used:
|
||||
|
||||
```php
|
||||
<?php
|
||||
use PhpParser\NodeDumper;
|
||||
|
||||
$nodeDumper = new NodeDumper;
|
||||
echo $nodeDumper->dump($stmts), "\n";
|
||||
```
|
||||
|
||||
For the sample code from the previous section, this will produce the following output:
|
||||
|
||||
```
|
||||
array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Scalar_String(
|
||||
value: Hi
|
||||
0: Stmt_Function(
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: printLine
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
type: null
|
||||
byRef: false
|
||||
variadic: false
|
||||
var: Expr_Variable(
|
||||
name: msg
|
||||
)
|
||||
default: null
|
||||
)
|
||||
1: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: hi
|
||||
1: getTarget
|
||||
)
|
||||
returnType: null
|
||||
stmts: array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Expr_Variable(
|
||||
name: msg
|
||||
)
|
||||
1: Scalar_String(
|
||||
value:
|
||||
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Stmt_Expression(
|
||||
expr: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: printLine
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Scalar_String(
|
||||
value: Hello World!!!
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -94,10 +143,30 @@ array(
|
||||
)
|
||||
```
|
||||
|
||||
Thus `$stmts` will contain an array with only one node, with this node being an instance of
|
||||
`PhpParser\Node\Stmt\Echo_`.
|
||||
You can also use the `php-parse` script to obtain such a node dump by calling it either with a file
|
||||
name or code string:
|
||||
|
||||
As PHP is a large language there are approximately 140 different nodes. In order to make work
|
||||
```sh
|
||||
vendor/bin/php-parse file.php
|
||||
vendor/bin/php-parse "<?php foo();"
|
||||
```
|
||||
|
||||
This can be very helpful if you want to quickly check how certain syntax is represented in the AST.
|
||||
|
||||
Node tree structure
|
||||
-------------------
|
||||
|
||||
Looking at the node dump above, you can see that `$stmts` for this example code is an array of two
|
||||
nodes, a `Stmt_Function` and a `Stmt_Expression`. The corresponding class names are:
|
||||
|
||||
* `Stmt_Function -> PhpParser\Node\Stmt\Function_`
|
||||
* `Stmt_Expression -> PhpParser\Node\Stmt\Expression`
|
||||
|
||||
The additional `_` at the end of the first class name is necessary, because `Function` is a
|
||||
reserved keyword. Many node class names in this library have a trailing `_` to avoid clashing with
|
||||
a keyword.
|
||||
|
||||
As PHP is a large language there are approximately 140 different nodes. In order to make working
|
||||
with them easier they are grouped into three categories:
|
||||
|
||||
* `PhpParser\Node\Stmt`s are statement nodes, i.e. language constructs that do not return
|
||||
@ -113,8 +182,9 @@ with them easier they are grouped into three categories:
|
||||
* There are some nodes not in either of these groups, for example names (`PhpParser\Node\Name`)
|
||||
and call arguments (`PhpParser\Node\Arg`).
|
||||
|
||||
Some node class names have a trailing `_`. This is used whenever the class name would otherwise clash
|
||||
with a PHP keyword.
|
||||
The `Node\Stmt\Expression` node is somewhat confusing in that it contains both the terms "statement"
|
||||
and "expression". This node distinguishes `expr`, which is a `Node\Expr`, from `expr;`, which is
|
||||
an "expression statement" represented by `Node\Stmt\Expression` and containing `expr` as a sub-node.
|
||||
|
||||
Every node has a (possibly zero) number of subnodes. You can access subnodes by writing
|
||||
`$node->subNodeName`. The `Stmt\Echo_` node has only one subnode `exprs`. So in order to access it
|
||||
@ -173,7 +243,7 @@ try {
|
||||
|
||||
The above code will output:
|
||||
|
||||
<?php echo 'Hello ', hi\getTarget();
|
||||
echo 'Hello ', hi\getTarget();
|
||||
|
||||
As you can see the source code was first parsed using `PhpParser\Parser->parse()`, then changed and then
|
||||
again converted to code using `PhpParser\PrettyPrinter\Standard->prettyPrint()`.
|
||||
@ -184,6 +254,8 @@ single expression using `prettyPrintExpr()`.
|
||||
The `prettyPrintFile()` method can be used to print an entire file. This will include the opening `<?php` tag
|
||||
and handle inline HTML as the first/last statement more gracefully.
|
||||
|
||||
> Read more: [Pretty printing documentation](component/Pretty_printing.markdown)
|
||||
|
||||
Node traversation
|
||||
-----------------
|
||||
|
||||
@ -267,7 +339,8 @@ All four methods can either return the changed node or not return at all (i.e. `
|
||||
case the current node is not changed.
|
||||
|
||||
The `enterNode()` method can additionally return the value `NodeTraverser::DONT_TRAVERSE_CHILDREN`,
|
||||
which instructs the traverser to skip all children of the current node.
|
||||
which instructs the traverser to skip all children of the current node. To furthermore prevent subsequent
|
||||
visitors from visiting the current node, `NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN` can be used instead.
|
||||
|
||||
The `leaveNode()` method can additionally return the value `NodeTraverser::REMOVE_NODE`, in which
|
||||
case the current node will be removed from the parent array. Furthermore it is possible to return
|
||||
@ -278,10 +351,12 @@ be `array(A, X, Y, Z, C)`.
|
||||
Instead of manually implementing the `NodeVisitor` interface you can also extend the `NodeVisitorAbstract`
|
||||
class, which will define empty default implementations for all the above methods.
|
||||
|
||||
> Read more: [Walking the AST](component/Walking_the_AST.markdown)
|
||||
|
||||
The NameResolver node visitor
|
||||
-----------------------------
|
||||
|
||||
One visitor is already bundled with the package: `PhpParser\NodeVisitor\NameResolver`. This visitor
|
||||
One visitor that is already bundled with the package is `PhpParser\NodeVisitor\NameResolver`. This visitor
|
||||
helps you work with namespaced code by trying to resolve most names to fully qualified ones.
|
||||
|
||||
For example, consider the following code:
|
||||
@ -292,7 +367,7 @@ For example, consider the following code:
|
||||
In order to know that `B\C` really is `A\C` you would need to track aliases and namespaces yourself.
|
||||
The `NameResolver` takes care of that and resolves names as far as possible.
|
||||
|
||||
After running it most names will be fully qualified. The only names that will stay unqualified are
|
||||
After running it, most names will be fully qualified. The only names that will stay unqualified are
|
||||
unqualified function and constant names. These are resolved at runtime and thus the visitor can't
|
||||
know which function they are referring to. In most cases this is a non-issue as the global functions
|
||||
are meant.
|
||||
@ -300,6 +375,8 @@ are meant.
|
||||
Also the `NameResolver` adds a `namespacedName` subnode to class, function and constant declarations
|
||||
that contains the namespaced name instead of only the shortname that is available via `name`.
|
||||
|
||||
> Read more: [Name resolution documentation](component/Name_resolution.markdown)
|
||||
|
||||
Example: Converting namespaced code to pseudo namespaces
|
||||
--------------------------------------------------------
|
||||
|
||||
@ -333,7 +410,7 @@ $files = new \RegexIterator($files, '/\.php$/');
|
||||
foreach ($files as $file) {
|
||||
try {
|
||||
// read the file that should be converted
|
||||
$code = file_get_contents($file);
|
||||
$code = file_get_contents($file->getPathName());
|
||||
|
||||
// parse
|
||||
$stmts = $parser->parse($code);
|
||||
|
@ -1,228 +0,0 @@
|
||||
Other node tree representations
|
||||
===============================
|
||||
|
||||
It is possible to convert the AST into several textual representations, which serve different uses.
|
||||
|
||||
Simple serialization
|
||||
--------------------
|
||||
|
||||
It is possible to serialize the node tree using `serialize()` and also unserialize it using
|
||||
`unserialize()`. The output is not human readable and not easily processable from anything
|
||||
but PHP, but it is compact and generates quickly. The main application thus is in caching.
|
||||
|
||||
Human readable dumping
|
||||
----------------------
|
||||
|
||||
Furthermore it is possible to dump nodes into a human readable format using the `dump` method of
|
||||
`PhpParser\NodeDumper`. This can be used for debugging.
|
||||
|
||||
```php
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
|
||||
printLine('Hello World!!!');
|
||||
CODE;
|
||||
|
||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
|
||||
$nodeDumper = new PhpParser\NodeDumper;
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
echo $nodeDumper->dump($stmts), "\n";
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
The above script will have an output looking roughly like this:
|
||||
|
||||
```
|
||||
array(
|
||||
0: Stmt_Function(
|
||||
byRef: false
|
||||
params: array(
|
||||
0: Param(
|
||||
name: msg
|
||||
default: null
|
||||
type: null
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Expr_Variable(
|
||||
name: msg
|
||||
)
|
||||
1: Scalar_String(
|
||||
value:
|
||||
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
name: printLine
|
||||
)
|
||||
1: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: printLine
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Scalar_String(
|
||||
value: Hello World!!!
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
JSON encoding
|
||||
-------------
|
||||
|
||||
Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON
|
||||
encode the AST directly using `json_encode()`:
|
||||
|
||||
```php
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
|
||||
printLine('Hello World!!!');
|
||||
CODE;
|
||||
|
||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
This will result in the following output (which includes attributes):
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"nodeType": "Stmt_Function",
|
||||
"byRef": false,
|
||||
"name": "printLine",
|
||||
"params": [
|
||||
{
|
||||
"nodeType": "Param",
|
||||
"type": null,
|
||||
"byRef": false,
|
||||
"variadic": false,
|
||||
"name": "msg",
|
||||
"default": null,
|
||||
"attributes": {
|
||||
"startLine": 3,
|
||||
"endLine": 3
|
||||
}
|
||||
}
|
||||
],
|
||||
"returnType": null,
|
||||
"stmts": [
|
||||
{
|
||||
"nodeType": "Stmt_Echo",
|
||||
"exprs": [
|
||||
{
|
||||
"nodeType": "Expr_Variable",
|
||||
"name": "msg",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"nodeType": "Scalar_String",
|
||||
"value": "\n",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4,
|
||||
"kind": 2
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 3,
|
||||
"endLine": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"nodeType": "Expr_FuncCall",
|
||||
"name": {
|
||||
"nodeType": "Name",
|
||||
"parts": [
|
||||
"printLine"
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 7,
|
||||
"endLine": 7
|
||||
}
|
||||
},
|
||||
"args": [
|
||||
{
|
||||
"nodeType": "Arg",
|
||||
"value": {
|
||||
"nodeType": "Scalar_String",
|
||||
"value": "Hello World!!!",
|
||||
"attributes": {
|
||||
"startLine": 7,
|
||||
"endLine": 7,
|
||||
"kind": 1
|
||||
}
|
||||
},
|
||||
"byRef": false,
|
||||
"unpack": false,
|
||||
"attributes": {
|
||||
"startLine": 7,
|
||||
"endLine": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 7,
|
||||
"endLine": 7
|
||||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
The JSON representation may be converted back into a node tree using the `JsonDecoder`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
$nodeDecoder = new PhpParser\NodeDecoder();
|
||||
$ast = $nodeDecoder->decode($json);
|
||||
```
|
||||
|
||||
Note that not all ASTs can be represented using JSON. In particular:
|
||||
|
||||
* JSON only supports UTF-8 strings.
|
||||
* JSON does not support non-finite floating-point numbers. This can occur if the original source
|
||||
code contains non-representable floating-pointing literals such as `1e1000`.
|
||||
|
||||
If the node tree is not representable in JSON, the initial `json_encode()` call will fail.
|
@ -1,84 +0,0 @@
|
||||
Code generation
|
||||
===============
|
||||
|
||||
It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the
|
||||
pretty printer to convert it to PHP code. To simplify code generation, the project comes with builders which allow
|
||||
creating node trees using a fluid interface, instead of instantiating all nodes manually. Builders are available for
|
||||
the following syntactic elements:
|
||||
|
||||
* namespaces and use statements
|
||||
* classes, interfaces and traits
|
||||
* methods, functions and parameters
|
||||
* properties
|
||||
|
||||
Here is an example:
|
||||
|
||||
```php
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use PhpParser\Node;
|
||||
|
||||
$factory = new BuilderFactory;
|
||||
$node = $factory->namespace('Name\Space')
|
||||
->addStmt($factory->use('Some\Other\Thingy')->as('SomeOtherClass'))
|
||||
->addStmt($factory->class('SomeOtherClass')
|
||||
->extend('SomeClass')
|
||||
->implement('A\Few', '\Interfaces')
|
||||
->makeAbstract() // ->makeFinal()
|
||||
|
||||
->addStmt($factory->method('someMethod')
|
||||
->makePublic()
|
||||
->makeAbstract() // ->makeFinal()
|
||||
->setReturnType('bool')
|
||||
->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
||||
->setDocComment('/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/')
|
||||
)
|
||||
|
||||
->addStmt($factory->method('anotherMethod')
|
||||
->makeProtected() // ->makePublic() [default], ->makePrivate()
|
||||
->addParam($factory->param('someParam')->setDefault('test'))
|
||||
// it is possible to add manually created nodes
|
||||
->addStmt(new Node\Expr\Print_(new Node\Expr\Variable('someParam')))
|
||||
)
|
||||
|
||||
// properties will be correctly reordered above the methods
|
||||
->addStmt($factory->property('someProperty')->makeProtected())
|
||||
->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
|
||||
)
|
||||
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$stmts = array($node);
|
||||
$prettyPrinter = new PrettyPrinter\Standard();
|
||||
echo $prettyPrinter->prettyPrintFile($stmts);
|
||||
```
|
||||
|
||||
This will produce the following output with the standard pretty printer:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace Name\Space;
|
||||
|
||||
use Some\Other\Thingy as SomeClass;
|
||||
abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
|
||||
{
|
||||
protected $someProperty;
|
||||
private $anotherProperty = array(1, 2, 3);
|
||||
/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/
|
||||
public abstract function someMethod(SomeClass $someParam) : bool;
|
||||
protected function anotherMethod($someParam = 'test')
|
||||
{
|
||||
print $someParam;
|
||||
}
|
||||
}
|
||||
```
|
@ -6,13 +6,17 @@ Guide
|
||||
|
||||
1. [Introduction](0_Introduction.markdown)
|
||||
2. [Usage of basic components](2_Usage_of_basic_components.markdown)
|
||||
3. [Other node tree representations](3_Other_node_tree_representations.markdown)
|
||||
4. [Code generation](4_Code_generation.markdown)
|
||||
5. [Frequently asked questions](5_FAQ.markdown)
|
||||
|
||||
|
||||
Component documentation
|
||||
-----------------------
|
||||
|
||||
|
||||
* [Walking the AST](component/Walking_the_AST.markdown)
|
||||
* Node visitors
|
||||
* Modifying the AST from a visitor
|
||||
* Short-circuiting traversals
|
||||
* Interleaved visitors
|
||||
* Simple node finding API
|
||||
* Parent and sibling references
|
||||
* [Name resolution](component/Name_resolution.markdown)
|
||||
* Name resolver options
|
||||
* Name resolution context
|
||||
@ -20,6 +24,8 @@ Component documentation
|
||||
* Converting AST back to PHP code
|
||||
* Customizing formatting
|
||||
* Formatting-preserving code transformations
|
||||
* [AST builders](component/AST_builders.markdown)
|
||||
* Fluent builders for AST nodes
|
||||
* [Lexer](component/Lexer.markdown)
|
||||
* Lexer options
|
||||
* Token and file positions for nodes
|
||||
@ -27,7 +33,14 @@ Component documentation
|
||||
* [Error handling](component/Error_handling.markdown)
|
||||
* Column information for errors
|
||||
* Error recovery (parsing of syntactically incorrect code)
|
||||
* [Constant expression evaluation](component/Constant_expression_evaluation.markdown)
|
||||
* Evaluating constant/property/etc initializers
|
||||
* Handling errors and unsupported expressions
|
||||
* [JSON representation](component/JSON_representation.markdown)
|
||||
* JSON encoding and decoding of ASTs
|
||||
* [Performance](component/Performance.markdown)
|
||||
* Disabling XDebug
|
||||
* Reusing objects
|
||||
* Garbage collection impact
|
||||
* [Frequently asked questions](component/FAQ.markdown)
|
||||
* Parent and sibling references
|
||||
|
138
doc/component/AST_builders.markdown
Normal file
138
doc/component/AST_builders.markdown
Normal file
@ -0,0 +1,138 @@
|
||||
AST builders
|
||||
============
|
||||
|
||||
When PHP-Parser is used to generate (or modify) code by first creating an Abstract Syntax Tree and
|
||||
then using the [pretty printer](Pretty_printing.markdown) to convert it to PHP code, it can often
|
||||
be tedious to manually construct AST nodes. The project provides a number of utilities to simplify
|
||||
the construction of common AST nodes.
|
||||
|
||||
Fluent builders
|
||||
---------------
|
||||
|
||||
The library comes with a number of builders, which allow creating node trees using a fluent
|
||||
interface. Builders are created using the `BuilderFactory` and the final constructed node is
|
||||
accessed through `getNode()`. Fluent builders are available for
|
||||
the following syntactic elements:
|
||||
|
||||
* namespaces and use statements
|
||||
* classes, interfaces and traits
|
||||
* methods, functions and parameters
|
||||
* properties
|
||||
|
||||
Here is an example:
|
||||
|
||||
```php
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use PhpParser\Node;
|
||||
|
||||
$factory = new BuilderFactory;
|
||||
$node = $factory->namespace('Name\Space')
|
||||
->addStmt($factory->use('Some\Other\Thingy')->as('SomeClass'))
|
||||
->addStmt($factory->useFunction('strlen'))
|
||||
->addStmt($factory->useConst('PHP_VERSION'))
|
||||
->addStmt($factory->class('SomeOtherClass')
|
||||
->extend('SomeClass')
|
||||
->implement('A\Few', '\Interfaces')
|
||||
->makeAbstract() // ->makeFinal()
|
||||
|
||||
->addStmt($factory->useTrait('FirstTrait'))
|
||||
|
||||
->addStmt($factory->useTrait('SecondTrait', 'ThirdTrait')
|
||||
->and('AnotherTrait')
|
||||
->with($factory->traitUseAdaptation('foo')->as('bar'))
|
||||
->with($factory->traitUseAdaptation('AnotherTrait', 'baz')->as('test'))
|
||||
->with($factory->traitUseAdaptation('AnotherTrait', 'func')->insteadof('SecondTrait')))
|
||||
|
||||
->addStmt($factory->method('someMethod')
|
||||
->makePublic()
|
||||
->makeAbstract() // ->makeFinal()
|
||||
->setReturnType('bool') // ->makeReturnByRef()
|
||||
->addParam($factory->param('someParam')->setType('SomeClass'))
|
||||
->setDocComment('/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/')
|
||||
)
|
||||
|
||||
->addStmt($factory->method('anotherMethod')
|
||||
->makeProtected() // ->makePublic() [default], ->makePrivate()
|
||||
->addParam($factory->param('someParam')->setDefault('test'))
|
||||
// it is possible to add manually created nodes
|
||||
->addStmt(new Node\Expr\Print_(new Node\Expr\Variable('someParam')))
|
||||
)
|
||||
|
||||
// properties will be correctly reordered above the methods
|
||||
->addStmt($factory->property('someProperty')->makeProtected())
|
||||
->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
|
||||
)
|
||||
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$stmts = array($node);
|
||||
$prettyPrinter = new PrettyPrinter\Standard();
|
||||
echo $prettyPrinter->prettyPrintFile($stmts);
|
||||
```
|
||||
|
||||
This will produce the following output with the standard pretty printer:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace Name\Space;
|
||||
|
||||
use Some\Other\Thingy as SomeClass;
|
||||
use function strlen;
|
||||
use const PHP_VERSION;
|
||||
abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
|
||||
{
|
||||
use FirstTrait;
|
||||
use SecondTrait, ThirdTrait, AnotherTrait {
|
||||
foo as bar;
|
||||
AnotherTrait::baz as test;
|
||||
AnotherTrait::func insteadof SecondTrait;
|
||||
}
|
||||
protected $someProperty;
|
||||
private $anotherProperty = array(1, 2, 3);
|
||||
/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/
|
||||
public abstract function someMethod(SomeClass $someParam) : bool;
|
||||
protected function anotherMethod($someParam = 'test')
|
||||
{
|
||||
print $someParam;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Additional helper methods
|
||||
-------------------------
|
||||
|
||||
The `BuilderFactory` also provides a number of additional helper methods, which directly return
|
||||
nodes. The following methods are currently available:
|
||||
|
||||
* `val($value)`: Creates an AST node for a literal value like `42` or `[1, 2, 3]`.
|
||||
* `var($name)`: Creates variable node.
|
||||
* `args(array $args)`: Creates an array of function/method arguments, including the required `Arg`
|
||||
wrappers. Also converts literals to AST nodes.
|
||||
* `funcCall($name, array $args = [])`: Create a function call node. Converts `$name` to a `Name`
|
||||
node and normalizes arguments.
|
||||
* `methodCall(Expr $var, $name, array $args = [])`: Create a method call node. Converts `$name` to
|
||||
an `Identifier` node and normalizes arguments.
|
||||
* `staticCall($class, $name, array $args = [])`: Create a static method call node. Converts
|
||||
`$class` to a `Name` node, `$name` to an `Identifier` node and normalizes arguments.
|
||||
* `new($class, array $args = [])`: Create a "new" (object creation) node. Converts `$class` to a
|
||||
`Name` node.
|
||||
* `constFetch($name)`: Create a constant fetch node. Converts `$name` to a `Name` node.
|
||||
* `classConstFetch($class, $name)`: Create a class constant fetch node. Converts `$class` to a
|
||||
`Name` node and `$name` to an `Identifier` node.
|
||||
* `propertyFetch($var, $name)`: Creates a property fetch node. Converts `$name` to an `Identifier`
|
||||
node.
|
||||
* `concat(...$exprs)`: Create a tree of `BinaryOp\Concat` nodes for the given expressions.
|
||||
|
||||
These methods may be expanded on an as-needed basis. Please open an issue or PR if a common
|
||||
operation is missing.
|
115
doc/component/Constant_expression_evaluation.markdown
Normal file
115
doc/component/Constant_expression_evaluation.markdown
Normal file
@ -0,0 +1,115 @@
|
||||
Constant expression evaluation
|
||||
==============================
|
||||
|
||||
Initializers for constants, properties, parameters, etc. have limited support for expressions. For
|
||||
example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class Test {
|
||||
const SECONDS_IN_HOUR = 60 * 60;
|
||||
const SECONDS_IN_DAY = 24 * self::SECONDS_IN_HOUR;
|
||||
}
|
||||
```
|
||||
|
||||
PHP-Parser supports evaluation of such constant expressions through the `ConstExprEvaluator` class:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||
|
||||
$evalutator = new ConstExprEvaluator();
|
||||
try {
|
||||
$value = $evalutator->evaluateSilently($someExpr);
|
||||
} catch (ConstExprEvaluationException $e) {
|
||||
// Either the expression contains unsupported expression types,
|
||||
// or an error occurred during evaluation
|
||||
}
|
||||
```
|
||||
|
||||
Error handling
|
||||
--------------
|
||||
|
||||
The constant evaluator provides two methods, `evaluateDirectly()` and `evaluateSilently()`, which
|
||||
differ in error behavior. `evaluateDirectly()` will evaluate the expression as PHP would, including
|
||||
any generated warnings or Errors. `evaluateSilently()` will instead convert warnings and Errors into
|
||||
a `ConstExprEvaluationException`. For example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||
use PhpParser\Node\{Expr, Scalar};
|
||||
|
||||
$evaluator = new ConstExprEvaluator();
|
||||
|
||||
// 10 / 0
|
||||
$expr = new Expr\BinaryOp\Div(new Scalar\LNumber(10), new Scalar\LNumber(0));
|
||||
|
||||
var_dump($evaluator->evaluateDirectly($expr)); // float(INF)
|
||||
// Warning: Division by zero
|
||||
|
||||
try {
|
||||
$evaluator->evaluateSilently($expr);
|
||||
} catch (ConstExprEvaluationException $e) {
|
||||
var_dump($e->getPrevious()->getMessage()); // Division by zero
|
||||
}
|
||||
```
|
||||
|
||||
For the purposes of static analysis, you will likely want to use `evaluateSilently()` and leave
|
||||
erroring expressions unevaluated.
|
||||
|
||||
Unsupported expressions and evaluator fallback
|
||||
----------------------------------------------
|
||||
|
||||
The constant expression evaluator supports all expression types that are permitted in constant
|
||||
expressions, apart from the following:
|
||||
|
||||
* `Scalar\MagicConst\*`
|
||||
* `Expr\ConstFetch` (only null/false/true are handled)
|
||||
* `Expr\ClassConstFetch`
|
||||
|
||||
Handling these expression types requires non-local information, such as which global constants are
|
||||
defined. By default, the evaluator will throw a `ConstExprEvaluationException` when it encounters
|
||||
an unsupported expression type.
|
||||
|
||||
It is possible to override this behavior and support resolution for these expression types by
|
||||
specifying an evaluation fallback function:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||
use PhpParser\Node\Expr;
|
||||
|
||||
$evalutator = new ConstExprEvaluator(function(Expr $expr) {
|
||||
if ($expr instanceof Expr\ConstFetch) {
|
||||
return fetchConstantSomehow($expr);
|
||||
}
|
||||
if ($expr instanceof Expr\ClassConstFetch) {
|
||||
return fetchClassConstantSomehow($expr);
|
||||
}
|
||||
// etc.
|
||||
throw new ConstExprEvaluationException(
|
||||
"Expression of type {$expr->getType()} cannot be evaluated");
|
||||
});
|
||||
|
||||
try {
|
||||
$evalutator->evaluateSilently($someExpr);
|
||||
} catch (ConstExprEvaluationException $e) {
|
||||
// Handle exception
|
||||
}
|
||||
```
|
||||
|
||||
Implementers are advised to ensure that evaluation of indirect constant references cannot lead to
|
||||
infinite recursion. For example, the following code could lead to infinite recursion if constant
|
||||
lookup is implemented naively.
|
||||
|
||||
```php
|
||||
<?php
|
||||
class Test {
|
||||
const A = self::B;
|
||||
const B = self::A;
|
||||
}
|
||||
```
|
@ -27,7 +27,7 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
Before using column information its availability needs to be checked with `$e->hasColumnInfo()`, as the precise
|
||||
Before using column information, its availability needs to be checked with `$e->hasColumnInfo()`, as the precise
|
||||
location of an error cannot always be determined. The methods for retrieving column information also have to be passed
|
||||
the source code of the parsed file. An example for printing an error:
|
||||
|
||||
|
131
doc/component/JSON_representation.markdown
Normal file
131
doc/component/JSON_representation.markdown
Normal file
@ -0,0 +1,131 @@
|
||||
JSON representation
|
||||
===================
|
||||
|
||||
Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON
|
||||
encode the AST directly using `json_encode()`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
|
||||
/** @param string $msg */
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
CODE;
|
||||
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
This will result in the following output (which includes attributes):
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"nodeType": "Stmt_Function",
|
||||
"byRef": false,
|
||||
"name": {
|
||||
"nodeType": "Identifier",
|
||||
"name": "printLine",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"nodeType": "Param",
|
||||
"type": null,
|
||||
"byRef": false,
|
||||
"variadic": false,
|
||||
"var": {
|
||||
"nodeType": "Expr_Variable",
|
||||
"name": "msg",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
},
|
||||
"default": null,
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"returnType": null,
|
||||
"stmts": [
|
||||
{
|
||||
"nodeType": "Stmt_Echo",
|
||||
"exprs": [
|
||||
{
|
||||
"nodeType": "Expr_Variable",
|
||||
"name": "msg",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"nodeType": "Scalar_String",
|
||||
"value": "\n",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5,
|
||||
"kind": 2
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"comments": [
|
||||
{
|
||||
"nodeType": "Comment_Doc",
|
||||
"text": "\/** @param string $msg *\/",
|
||||
"line": 3,
|
||||
"filePos": 9,
|
||||
"tokenPos": 2
|
||||
}
|
||||
],
|
||||
"endLine": 6
|
||||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
The JSON representation may be converted back into an AST using the `JsonDecoder`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
$jsonDecoder = new PhpParser\JsonDecoder();
|
||||
$ast = $jsonDecoder->decode($json);
|
||||
```
|
||||
|
||||
Note that not all ASTs can be represented using JSON. In particular:
|
||||
|
||||
* JSON only supports UTF-8 strings.
|
||||
* JSON does not support non-finite floating-point numbers. This can occur if the original source
|
||||
code contains non-representable floating-pointing literals such as `1e1000`.
|
||||
|
||||
If the node tree is not representable in JSON, the initial `json_encode()` call will fail.
|
||||
|
||||
From the command line, a JSON dump can be obtained using `vendor/bin/php-parse -j file.php`.
|
@ -107,9 +107,9 @@ function handleHaltCompiler(): string;
|
||||
function getNextToken(string &$value = null, array &$startAttributes = null, array &$endAttributes = null): int;
|
||||
```
|
||||
|
||||
The `startLexing()` method is invoked with the source code that is to be lexed (including the opening tag) whenever the
|
||||
`parse()` method of the parser is called. It can be used to reset state or preprocess the source code or tokens. The
|
||||
passes `ErrorHandler` should be used to report lexing errors.
|
||||
The `startLexing()` method is invoked whenever the `parse()` method of the parser is called and is passed the source
|
||||
code that is to be lexed (including the opening tag). It can be used to reset state or preprocess the source code or tokens. The
|
||||
passed `ErrorHandler` should be used to report lexing errors.
|
||||
|
||||
The `getTokens()` method returns the current token array, in the usual `token_get_all()` format. This method is not
|
||||
used by the parser (which uses `getNextToken()`), but is useful in combination with the token position attributes.
|
||||
|
@ -17,9 +17,9 @@ the `xdebug.default_enable=0` ini option does *not* disable XDebug. The *only* w
|
||||
XDebug is to not load the extension in the first place.
|
||||
|
||||
If you are building a command-line utility for use by developers (who often have XDebug enabled),
|
||||
you may want to consider automatically restarting PHP with XDebug unloaded. See the composer
|
||||
[XdebugHandler](https://github.com/composer/composer/blob/master/src/Composer/XdebugHandler.php)
|
||||
for an implementation of such functionality.
|
||||
you may want to consider automatically restarting PHP with XDebug unloaded. The
|
||||
[composer/xdebug-handler](https://github.com/composer/xdebug-handler) package can be used to do
|
||||
this.
|
||||
|
||||
If you do run with XDebug, you may need to increase the `xdebug.max_nesting_level` option to a
|
||||
higher level, such as 3000. While the parser itself is recursion free, most other code working on
|
||||
|
@ -50,14 +50,13 @@ Formatting-preserving pretty printing
|
||||
|
||||
For automated code refactoring, migration and similar, you will usually only want to modify a small
|
||||
portion of the code and leave the remainder alone. The basic pretty printer is not suitable for
|
||||
this, because it will also reformat parts of the code, which have not been modified.
|
||||
this, because it will also reformat parts of the code which have not been modified.
|
||||
|
||||
Since PHP-Parser 4.0 an experimental formatting-preserving pretty-printing mode is available, which
|
||||
attempts to preserve the formatting of code, those AST nodes have not changed, and only reformat
|
||||
Since PHP-Parser 4.0, an experimental formatting-preserving pretty-printing mode is available, which
|
||||
attempts to preserve the formatting of code (those AST nodes that have not changed) and only reformat
|
||||
code which has been modified or newly inserted.
|
||||
|
||||
Use of the formatting-preservation functionality currently requires some additional preparatory
|
||||
steps:
|
||||
Use of the formatting-preservation functionality requires some additional preparatory steps:
|
||||
|
||||
```php
|
||||
use PhpParser\{Lexer, NodeTraverser, NodeVisitor, Parser, PrettyPrinter};
|
||||
@ -86,6 +85,11 @@ $newStmts = $traverser->traverse($oldStmts);
|
||||
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
||||
```
|
||||
|
||||
If you make use of the name resolution functionality, you will likely want to disable the
|
||||
`replaceNodes` option. This will add resolved names as attributes, instead of directlying modifying
|
||||
the AST and causing spurious changes to the pretty printed code. For more information, see the
|
||||
[name resolution documentation](Name_resolution.markdown).
|
||||
|
||||
This functionality is experimental and not yet fully implemented. It should not provide incorrect
|
||||
code, but it may sometimes reformat more code than necessary. Open issues are tracked in
|
||||
[issue #344](https://github.com/nikic/PHP-Parser/issues/344). If you encounter problems while using
|
||||
|
337
doc/component/Walking_the_AST.markdown
Normal file
337
doc/component/Walking_the_AST.markdown
Normal file
@ -0,0 +1,337 @@
|
||||
Walking the AST
|
||||
===============
|
||||
|
||||
The most common way to work with the AST is by using a node traverser and one or more node visitors.
|
||||
As a basic example, the following code changes all literal integers in the AST into strings (e.g.,
|
||||
`42` becomes `'42'`.)
|
||||
|
||||
```php
|
||||
use PhpParser\{Node, NodeTraverser, NodeVisitorAbstract};
|
||||
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor(new class extends NodeVisitorAbstract {
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Scalar\LNumber) {
|
||||
return new Node\Scalar\String_((string) $node->value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$stmts = ...;
|
||||
$modifiedStmts = $traverser->traverse($stmts);
|
||||
```
|
||||
|
||||
Node visitors
|
||||
-------------
|
||||
|
||||
Each node visitor implements an interface with following four methods:
|
||||
|
||||
```php
|
||||
interface NodeVisitor {
|
||||
public function beforeTraverse(array $nodes);
|
||||
public function enterNode(Node $node);
|
||||
public function leaveNode(Node $node);
|
||||
public function afterTraverse(array $nodes);
|
||||
}
|
||||
```
|
||||
|
||||
The `beforeTraverse()` and `afterTraverse()` methods are called before and after the traversal
|
||||
respectively, and are passed the entire AST. They can be used to perform any necessary state
|
||||
setup or cleanup.
|
||||
|
||||
The `enterNode()` method is called when a node is first encountered, before its children are
|
||||
processed ("preorder"). The `leaveNode()` method is called after all children have been visited
|
||||
("postorder").
|
||||
|
||||
For example, if we have the following excerpt of an AST
|
||||
|
||||
```
|
||||
Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: printLine
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Scalar_String(
|
||||
value: Hello World!!!
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
then the enter/leave methods will be called in the following order:
|
||||
|
||||
```
|
||||
enterNode(Expr_FuncCall)
|
||||
enterNode(Name)
|
||||
leaveNode(Name)
|
||||
enterNode(Arg)
|
||||
enterNode(Scalar_String)
|
||||
leaveNode(Scalar_String)
|
||||
leaveNode(Arg)
|
||||
leaveNode(Expr_FuncCall)
|
||||
```
|
||||
|
||||
A common pattern is that `enterNode` is used to collect some information and then `leaveNode`
|
||||
performs modifications based on that. At the time when `leaveNode` is called, all the code inside
|
||||
the node will have already been visited and necessary information collected.
|
||||
|
||||
As you usually do not want to implement all four methods, it is recommended that you extend
|
||||
`NodeVisitorAbstract` instead of implementing the interface directly. The abstract class provides
|
||||
empty default implementations.
|
||||
|
||||
Modifying the AST
|
||||
-----------------
|
||||
|
||||
There are a number of ways in which the AST can be modified from inside a node visitor. The first
|
||||
and simplest is to simply change AST properties inside the visitor:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Scalar\LNumber) {
|
||||
// increment all integer literals
|
||||
$node->value++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The second is to replace a node entirely by returning a new node:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Expr\BinaryOp\BooleanAnd) {
|
||||
// Convert all $a && $b expressions into !($a && $b)
|
||||
return new Node\Expr\BooleanNot($node);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Doing this is supported both inside enterNode and leaveNode. However, you have to be mindful about
|
||||
where you perform the replacement: If a node is replaced in enterNode, then the recursive traversal
|
||||
will also consider the children of the new node. If you aren't careful, this can lead to infinite
|
||||
recursion. For example, let's take the previous code sample and use enterNode instead:
|
||||
|
||||
```php
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Expr\BinaryOp\BooleanAnd) {
|
||||
// Convert all $a && $b expressions into !($a && $b)
|
||||
return new Node\Expr\BooleanNot($node);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now `$a && $b` will be replaced by `!($a && $b)`. Then the traverser will go into the first (and
|
||||
only) child of `!($a && $b)`, which is `$a && $b`. The transformation applies again and we end up
|
||||
with `!!($a && $b)`. This will continue until PHP hits the memory limit.
|
||||
|
||||
Finally, two special replacement types are supported only by leaveNode. The first is removal of a
|
||||
node:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Return_) {
|
||||
// Remove all return statements
|
||||
return NodeTraverser::REMOVE_NODE;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Node removal only works if the parent structure is an array. This means that usually it only makes
|
||||
sense to remove nodes of type `Node\Stmt`, as they always occur inside statement lists (and a few
|
||||
more node types like `Arg` or `Expr\ArrayItem`, which are also always part of lists).
|
||||
|
||||
On the other hand, removing a `Node\Expr` does not make sense: If you have `$a * $b`, there is no
|
||||
meaningful way in which the `$a` part could be removed. If you want to remove an expression, you
|
||||
generally want to remove it together with a surrounding expression statement:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Expression
|
||||
&& $node->expr instanceof Node\Expr\FuncCall
|
||||
&& $node->expr->name instanceof Node\Name
|
||||
&& $node->expr->name->toString() === 'var_dump'
|
||||
) {
|
||||
return NodeTraverser::REMOVE_NODE;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This example will remove all calls to `var_dump()` which occur as expression statements. This means
|
||||
that `var_dump($a);` will be removed, but `if (var_dump($a))` will not be removed (and there is no
|
||||
obvious way in which it can be removed).
|
||||
|
||||
Next to removing nodes, it is also possible to replace one node with multiple nodes. Again, this
|
||||
only works inside leaveNode and only if the parent structure is an array.
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Return_ && $node->expr !== null) {
|
||||
// Convert "return foo();" into "$retval = foo(); return $retval;"
|
||||
$var = new Node\Expr\Variable('retval');
|
||||
return [
|
||||
new Node\Stmt\Expression(new Node\Expr\Assign($var, $node->expr)),
|
||||
new Node\Stmt\Return_($var),
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Short-circuiting traversal
|
||||
--------------------------
|
||||
|
||||
An AST can easily contain thousands of nodes, and traversing over all of them may be slow,
|
||||
especially if you have more than one visitor. In some cases, it is possible to avoid a full
|
||||
traversal.
|
||||
|
||||
If you are looking for all class declarations in a file (and assuming you're not interested in
|
||||
anonymous classes), you know that once you've seen a class declaration, there is no point in also
|
||||
checking all it's child nodes, because PHP does not allow nesting classes. In this case, you can
|
||||
instruct the traverser to not recurse into the class node:
|
||||
|
||||
```
|
||||
private $classes = [];
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Class_) {
|
||||
$this->classes[] = $node;
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Of course, this option is only available in enterNode, because it's already too late by the time
|
||||
leaveNode is reached.
|
||||
|
||||
If you are only looking for one specific node, it is also possible to abort the traversal entirely
|
||||
after finding it. For example, if you are looking for the node of a class with a certain name (and
|
||||
discounting exotic cases like conditionally defining a class two times), you can stop traversal
|
||||
once you found it:
|
||||
|
||||
```
|
||||
private $class = null;
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Class_ &&
|
||||
$node->namespacedName->toString() === 'Foo\Bar\Baz'
|
||||
) {
|
||||
$this->class = $node;
|
||||
return NodeTraverser::STOP_TRAVERSAL;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This works both in enterNode and leaveNode. Note that this particular case can also be more easily
|
||||
handled using a NodeFinder, which will be introduced below.
|
||||
|
||||
Multiple visitors
|
||||
-----------------
|
||||
|
||||
A single traverser can be used with multiple visitors:
|
||||
|
||||
```php
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitorA);
|
||||
$traverser->addVisitor($visitorB);
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
```
|
||||
|
||||
It is important to understand that if a traverser is run with multiple visitors, the visitors will
|
||||
be interleaved. Given the following AST excerpt
|
||||
|
||||
```
|
||||
Stmt_Return(
|
||||
expr: Expr_Variable(
|
||||
name: foobar
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
the following method calls will be performed:
|
||||
|
||||
```
|
||||
$visitorA->enterNode(Stmt_Return)
|
||||
$visitorB->enterNode(Stmt_Return)
|
||||
$visitorA->enterNode(Expr_Variable)
|
||||
$visitorB->enterNode(Expr_Variable)
|
||||
$visitorA->leaveNode(Expr_Variable)
|
||||
$visitorB->leaveNode(Expr_Variable)
|
||||
$visitorA->leaveNode(Stmt_Return)
|
||||
$visitorB->leaveNode(Stmt_Return)
|
||||
```
|
||||
|
||||
That is, when visiting a node, enterNode and leaveNode will always be called for all visitors.
|
||||
Running multiple visitors in parallel improves performance, as the AST only has to be traversed
|
||||
once. However, it is not always possible to write visitors in a way that allows interleaved
|
||||
execution. In this case, you can always fall back to performing multiple traversals:
|
||||
|
||||
```php
|
||||
$traverserA = new NodeTraverser;
|
||||
$traverserA->addVisitor($visitorA);
|
||||
$traverserB = new NodeTraverser;
|
||||
$traverserB->addVisitor($visitorB);
|
||||
$stmts = $traverserA->traverser($stmts);
|
||||
$stmts = $traverserB->traverser($stmts);
|
||||
```
|
||||
|
||||
When using multiple visitors, it is important to understand how they interact with the various
|
||||
special enterNode/leaveNode return values:
|
||||
|
||||
* If *any* visitor returns `DONT_TRAVERSE_CHILDREN`, the children will be skipped for *all*
|
||||
visitors.
|
||||
* If *any* visitor returns `DONT_TRAVERSE_CURRENT_AND_CHILDREN`, the children will be skipped for *all*
|
||||
visitors, and all *subsequent* visitors will not visit the current node.
|
||||
* If *any* visitor returns `STOP_TRAVERSAL`, traversal is stopped for *all* visitors.
|
||||
* If a visitor returns a replacement node, subsequent visitors will be passed the replacement node,
|
||||
not the original one.
|
||||
* If a visitor returns `REMOVE_NODE`, subsequent visitors will not see this node.
|
||||
* If a visitor returns an array of replacement nodes, subsequent visitors will see neither the node
|
||||
that was replaced, nor the replacement nodes.
|
||||
|
||||
Simple node finding
|
||||
-------------------
|
||||
|
||||
While the node visitor mechanism is very flexible, creating a node visitor can be overly cumbersome
|
||||
for minor tasks. For this reason a `NodeFinder` is provided, which can find AST nodes that either
|
||||
satisfy a certain callback, or which are instanced of a certain node type. A couple of examples are
|
||||
shown in the following:
|
||||
|
||||
```php
|
||||
use PhpParser\{Node, NodeFinder};
|
||||
|
||||
$nodeFinder = new NodeFinder;
|
||||
|
||||
// Find all class nodes.
|
||||
$classes = $nodeFinder->findInstanceOf($stmts, Node\Stmt\Class_::class);
|
||||
|
||||
// Find all classes that extend another class
|
||||
$extendingClasses = $nodeFinder->find($stmts, function(Node $node) {
|
||||
return $node instanceof Node\Stmt\Class_
|
||||
&& $node->extends !== null;
|
||||
});
|
||||
|
||||
// Find first class occuring in the AST. Returns null if no class exists.
|
||||
$class = $nodeFinder->findFirstInstanceOf($stmts, Node\Stmt\Class_::class);
|
||||
|
||||
// Find first class that has name $name
|
||||
$class = $nodeFinder->findFirst($stmts, function(Node $node) use ($name) {
|
||||
return $node instanceof Node\Stmt\Class_
|
||||
&& $node->resolvedName->toString() === $name;
|
||||
});
|
||||
```
|
||||
|
||||
Internally, the `NodeFinder` also uses a node traverser. It only simplifies the interface for a
|
||||
common use case.
|
||||
|
||||
Parent and sibling references
|
||||
-----------------------------
|
||||
|
||||
The node visitor mechanism is somewhat rigid, in that it prescribes an order in which nodes should
|
||||
be accessed: From parents to children. However, it can often be convenient to operate in the
|
||||
reverse direction: When working on a node, you might want to check if the parent node satisfies a
|
||||
certain property.
|
||||
|
||||
PHP-Parser does not add parent (or sibling) references to nodes by itself, but you can easily
|
||||
emulate this with a visitor. See the [FAQ](FAQ.markdown) for more information.
|
@ -32,8 +32,8 @@ class #(-p) extends \PhpParser\ParserAbstract
|
||||
protected $defaultAction = #(YYDEFAULT);
|
||||
protected $unexpectedTokenRule = #(YYUNEXPECTED);
|
||||
|
||||
protected $YY2TBLSTATE = #(YY2TBLSTATE);
|
||||
protected $YYNLSTATES = #(YYNLSTATES);
|
||||
protected $YY2TBLSTATE = #(YY2TBLSTATE);
|
||||
protected $numNonLeafStates = #(YYNLSTATES);
|
||||
|
||||
protected $symbolToName = array(
|
||||
#listvar terminals
|
||||
|
@ -16,7 +16,7 @@ top_statement_list_ex:
|
||||
|
||||
top_statement_list:
|
||||
top_statement_list_ex
|
||||
{ makeNop($nop, $this->lookaheadStartAttributes, $this->endAttributes);
|
||||
{ makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
|
||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||
;
|
||||
|
||||
@ -27,7 +27,7 @@ reserved_non_modifiers:
|
||||
| T_FINALLY | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
|
||||
| T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT
|
||||
| T_BREAK | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
|
||||
| T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER
|
||||
| T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER | T_FN
|
||||
;
|
||||
|
||||
semi_reserved:
|
||||
@ -160,7 +160,7 @@ inner_statement_list_ex:
|
||||
|
||||
inner_statement_list:
|
||||
inner_statement_list_ex
|
||||
{ makeNop($nop, $this->lookaheadStartAttributes, $this->endAttributes);
|
||||
{ makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
|
||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||
;
|
||||
|
||||
@ -335,8 +335,8 @@ case_list:
|
||||
;
|
||||
|
||||
case:
|
||||
T_CASE expr case_separator inner_statement_list { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list { $$ = Stmt\Case_[null, $3]; }
|
||||
T_CASE expr case_separator inner_statement_list_ex { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list_ex { $$ = Stmt\Case_[null, $3]; }
|
||||
;
|
||||
|
||||
case_separator:
|
||||
@ -454,11 +454,17 @@ static_var:
|
||||
| plain_variable '=' static_scalar { $$ = Stmt\StaticVar[$1, $3]; }
|
||||
;
|
||||
|
||||
class_statement_list:
|
||||
class_statement_list class_statement { push($1, $2); }
|
||||
class_statement_list_ex:
|
||||
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } }
|
||||
| /* empty */ { init(); }
|
||||
;
|
||||
|
||||
class_statement_list:
|
||||
class_statement_list_ex
|
||||
{ makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
|
||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||
;
|
||||
|
||||
class_statement:
|
||||
variable_modifiers property_declaration_list ';'
|
||||
{ $$ = Stmt\Property[$1, $2]; $this->checkProperty($$, #1); }
|
||||
@ -573,6 +579,7 @@ expr:
|
||||
| variable T_SL_EQUAL expr { $$ = Expr\AssignOp\ShiftLeft [$1, $3]; }
|
||||
| variable T_SR_EQUAL expr { $$ = Expr\AssignOp\ShiftRight[$1, $3]; }
|
||||
| variable T_POW_EQUAL expr { $$ = Expr\AssignOp\Pow [$1, $3]; }
|
||||
| variable T_COALESCE_EQUAL expr { $$ = Expr\AssignOp\Coalesce [$1, $3]; }
|
||||
| variable T_INC { $$ = Expr\PostInc[$1]; }
|
||||
| T_INC variable { $$ = Expr\PreInc [$2]; }
|
||||
| variable T_DEC { $$ = Expr\PostDec[$1]; }
|
||||
@ -622,7 +629,10 @@ expr:
|
||||
| T_REQUIRE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE]; }
|
||||
| T_REQUIRE_ONCE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE_ONCE]; }
|
||||
| T_INT_CAST expr { $$ = Expr\Cast\Int_ [$2]; }
|
||||
| T_DOUBLE_CAST expr { $$ = Expr\Cast\Double [$2]; }
|
||||
| T_DOUBLE_CAST expr
|
||||
{ $attrs = attributes();
|
||||
$attrs['kind'] = $this->getFloatCastKind($1);
|
||||
$$ = new Expr\Cast\Double($2, $attrs); }
|
||||
| T_STRING_CAST expr { $$ = Expr\Cast\String_ [$2]; }
|
||||
| T_ARRAY_CAST expr { $$ = Expr\Cast\Array_ [$2]; }
|
||||
| T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
|
||||
@ -785,11 +795,9 @@ common_scalar:
|
||||
| T_FUNC_C { $$ = Scalar\MagicConst\Function_[]; }
|
||||
| T_NS_C { $$ = Scalar\MagicConst\Namespace_[]; }
|
||||
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
|
||||
{ $attrs = attributes(); setDocStringAttrs($attrs, $1);
|
||||
$$ = new Scalar\String_(Scalar\String_::parseDocString($1, $2, false), $attrs); }
|
||||
{ $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), false); }
|
||||
| T_START_HEREDOC T_END_HEREDOC
|
||||
{ $attrs = attributes(); setDocStringAttrs($attrs, $1);
|
||||
$$ = new Scalar\String_('', $attrs); }
|
||||
{ $$ = $this->parseDocString($1, '', $2, attributes(), stackAttributes(#2), false); }
|
||||
;
|
||||
|
||||
static_scalar:
|
||||
@ -850,8 +858,7 @@ scalar:
|
||||
{ $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
|
||||
parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
|
||||
| T_START_HEREDOC encaps_list T_END_HEREDOC
|
||||
{ $attrs = attributes(); setDocStringAttrs($attrs, $1);
|
||||
parseEncapsedDoc($2, true); $$ = new Scalar\Encapsed($2, $attrs); }
|
||||
{ $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
|
||||
;
|
||||
|
||||
static_array_pair_list:
|
||||
@ -981,6 +988,7 @@ array_pair:
|
||||
| expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| expr T_DOUBLE_ARROW '&' variable { $$ = Expr\ArrayItem[$4, $1, true]; }
|
||||
| '&' variable { $$ = Expr\ArrayItem[$2, null, true]; }
|
||||
| T_ELLIPSIS expr { $$ = Expr\ArrayItem[$2, null, false, attributes(), true]; }
|
||||
;
|
||||
|
||||
encaps_list:
|
||||
|
@ -16,7 +16,7 @@ top_statement_list_ex:
|
||||
|
||||
top_statement_list:
|
||||
top_statement_list_ex
|
||||
{ makeNop($nop, $this->lookaheadStartAttributes, $this->endAttributes);
|
||||
{ makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
|
||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||
;
|
||||
|
||||
@ -27,7 +27,7 @@ reserved_non_modifiers:
|
||||
| T_FINALLY | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
|
||||
| T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT
|
||||
| T_BREAK | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
|
||||
| T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER
|
||||
| T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER | T_FN
|
||||
;
|
||||
|
||||
semi_reserved:
|
||||
@ -196,7 +196,7 @@ inner_statement_list_ex:
|
||||
|
||||
inner_statement_list:
|
||||
inner_statement_list_ex
|
||||
{ makeNop($nop, $this->lookaheadStartAttributes, $this->endAttributes);
|
||||
{ makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
|
||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||
;
|
||||
|
||||
@ -240,6 +240,8 @@ non_empty_statement:
|
||||
{ $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; }
|
||||
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
|
||||
{ $$ = Stmt\Foreach_[$3, $7[0], ['keyVar' => $5, 'byRef' => $7[1], 'stmts' => $9]]; }
|
||||
| T_FOREACH '(' expr error ')' foreach_statement
|
||||
{ $$ = Stmt\Foreach_[$3, new Expr\Error(stackAttributes(#4)), ['stmts' => $6]]; }
|
||||
| T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt\Declare_[$3, $5]; }
|
||||
| T_TRY '{' inner_statement_list '}' catches optional_finally
|
||||
{ $$ = Stmt\TryCatch[$3, $5, $6]; $this->checkTryCatch($$); }
|
||||
@ -277,7 +279,7 @@ optional_finally:
|
||||
;
|
||||
|
||||
variables_list:
|
||||
non_empty_variables_list no_comma { $$ = $1; }
|
||||
non_empty_variables_list optional_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_variables_list:
|
||||
@ -295,9 +297,14 @@ optional_ellipsis:
|
||||
| T_ELLIPSIS { $$ = true; }
|
||||
;
|
||||
|
||||
block_or_error:
|
||||
'{' inner_statement_list '}' { $$ = $2; }
|
||||
| error { $$ = []; }
|
||||
;
|
||||
|
||||
function_declaration_statement:
|
||||
T_FUNCTION optional_ref identifier '(' parameter_list ')' optional_return_type '{' inner_statement_list '}'
|
||||
{ $$ = Stmt\Function_[$3, ['byRef' => $2, 'params' => $5, 'returnType' => $7, 'stmts' => $9]]; }
|
||||
T_FUNCTION optional_ref identifier '(' parameter_list ')' optional_return_type block_or_error
|
||||
{ $$ = Stmt\Function_[$3, ['byRef' => $2, 'params' => $5, 'returnType' => $7, 'stmts' => $8]]; }
|
||||
;
|
||||
|
||||
class_declaration_statement:
|
||||
@ -383,8 +390,8 @@ case_list:
|
||||
;
|
||||
|
||||
case:
|
||||
T_CASE expr case_separator inner_statement_list { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list { $$ = Stmt\Case_[null, $3]; }
|
||||
T_CASE expr case_separator inner_statement_list_ex { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list_ex { $$ = Stmt\Case_[null, $3]; }
|
||||
;
|
||||
|
||||
case_separator:
|
||||
@ -443,10 +450,12 @@ non_empty_parameter_list:
|
||||
;
|
||||
|
||||
parameter:
|
||||
optional_param_type optional_ref optional_ellipsis plain_variable
|
||||
optional_type optional_ref optional_ellipsis plain_variable
|
||||
{ $$ = Node\Param[$4, null, $1, $2, $3]; $this->checkParam($$); }
|
||||
| optional_param_type optional_ref optional_ellipsis plain_variable '=' expr
|
||||
| optional_type optional_ref optional_ellipsis plain_variable '=' expr
|
||||
{ $$ = Node\Param[$4, $6, $1, $2, $3]; $this->checkParam($$); }
|
||||
| optional_type optional_ref optional_ellipsis error
|
||||
{ $$ = Node\Param[Expr\Error[], null, $1, $2, $3]; }
|
||||
;
|
||||
|
||||
type_expr:
|
||||
@ -460,7 +469,7 @@ type:
|
||||
| T_CALLABLE { $$ = Node\Identifier['callable']; }
|
||||
;
|
||||
|
||||
optional_param_type:
|
||||
optional_type:
|
||||
/* empty */ { $$ = null; }
|
||||
| type_expr { $$ = $1; }
|
||||
;
|
||||
@ -468,11 +477,12 @@ optional_param_type:
|
||||
optional_return_type:
|
||||
/* empty */ { $$ = null; }
|
||||
| ':' type_expr { $$ = $2; }
|
||||
| ':' error { $$ = null; }
|
||||
;
|
||||
|
||||
argument_list:
|
||||
'(' ')' { $$ = array(); }
|
||||
| '(' non_empty_argument_list no_comma ')' { $$ = $2; }
|
||||
| '(' non_empty_argument_list optional_comma ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
non_empty_argument_list:
|
||||
@ -513,20 +523,28 @@ static_var:
|
||||
| plain_variable '=' expr { $$ = Stmt\StaticVar[$1, $3]; }
|
||||
;
|
||||
|
||||
class_statement_list:
|
||||
class_statement_list class_statement { push($1, $2); }
|
||||
class_statement_list_ex:
|
||||
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } }
|
||||
| /* empty */ { init(); }
|
||||
;
|
||||
|
||||
class_statement_list:
|
||||
class_statement_list_ex
|
||||
{ makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
|
||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||
;
|
||||
|
||||
class_statement:
|
||||
variable_modifiers property_declaration_list ';'
|
||||
{ $$ = Stmt\Property[$1, $2]; $this->checkProperty($$, #1); }
|
||||
variable_modifiers optional_type property_declaration_list ';'
|
||||
{ $attrs = attributes();
|
||||
$$ = new Stmt\Property($1, $3, $attrs, $2); $this->checkProperty($$, #1); }
|
||||
| method_modifiers T_CONST class_const_list ';'
|
||||
{ $$ = Stmt\ClassConst[$3, $1]; $this->checkClassConst($$, #1); }
|
||||
| method_modifiers T_FUNCTION optional_ref identifier_ex '(' parameter_list ')' optional_return_type method_body
|
||||
{ $$ = Stmt\ClassMethod[$4, ['type' => $1, 'byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9]];
|
||||
$this->checkClassMethod($$, #1); }
|
||||
| T_USE class_name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
||||
| error { $$ = null; /* will be skipped */ }
|
||||
;
|
||||
|
||||
trait_adaptations:
|
||||
@ -562,7 +580,7 @@ trait_method_reference:
|
||||
|
||||
method_body:
|
||||
';' /* abstract method */ { $$ = null; }
|
||||
| '{' inner_statement_list '}' { $$ = $2; }
|
||||
| block_or_error { $$ = $1; }
|
||||
;
|
||||
|
||||
variable_modifiers:
|
||||
@ -642,6 +660,7 @@ expr:
|
||||
| variable T_SL_EQUAL expr { $$ = Expr\AssignOp\ShiftLeft [$1, $3]; }
|
||||
| variable T_SR_EQUAL expr { $$ = Expr\AssignOp\ShiftRight[$1, $3]; }
|
||||
| variable T_POW_EQUAL expr { $$ = Expr\AssignOp\Pow [$1, $3]; }
|
||||
| variable T_COALESCE_EQUAL expr { $$ = Expr\AssignOp\Coalesce [$1, $3]; }
|
||||
| variable T_INC { $$ = Expr\PostInc[$1]; }
|
||||
| T_INC variable { $$ = Expr\PreInc [$2]; }
|
||||
| variable T_DEC { $$ = Expr\PostDec[$1]; }
|
||||
@ -689,7 +708,10 @@ expr:
|
||||
| T_REQUIRE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE]; }
|
||||
| T_REQUIRE_ONCE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE_ONCE]; }
|
||||
| T_INT_CAST expr { $$ = Expr\Cast\Int_ [$2]; }
|
||||
| T_DOUBLE_CAST expr { $$ = Expr\Cast\Double [$2]; }
|
||||
| T_DOUBLE_CAST expr
|
||||
{ $attrs = attributes();
|
||||
$attrs['kind'] = $this->getFloatCastKind($1);
|
||||
$$ = new Expr\Cast\Double($2, $attrs); }
|
||||
| T_STRING_CAST expr { $$ = Expr\Cast\String_ [$2]; }
|
||||
| T_ARRAY_CAST expr { $$ = Expr\Cast\Array_ [$2]; }
|
||||
| T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
|
||||
@ -707,12 +729,18 @@ expr:
|
||||
| T_YIELD expr { $$ = Expr\Yield_[$2, null]; }
|
||||
| T_YIELD expr T_DOUBLE_ARROW expr { $$ = Expr\Yield_[$4, $2]; }
|
||||
| T_YIELD_FROM expr { $$ = Expr\YieldFrom[$2]; }
|
||||
|
||||
| T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr
|
||||
{ $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $2, 'params' => $4, 'returnType' => $6, 'expr' => $8]]; }
|
||||
| T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr
|
||||
{ $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9]]; }
|
||||
|
||||
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type
|
||||
'{' inner_statement_list '}'
|
||||
{ $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $9]]; }
|
||||
block_or_error
|
||||
{ $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $8]]; }
|
||||
| T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type
|
||||
'{' inner_statement_list '}'
|
||||
{ $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $10]]; }
|
||||
block_or_error
|
||||
{ $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9]]; }
|
||||
;
|
||||
|
||||
anonymous_class:
|
||||
@ -831,17 +859,14 @@ scalar:
|
||||
| dereferencable_scalar { $$ = $1; }
|
||||
| constant { $$ = $1; }
|
||||
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
|
||||
{ $attrs = attributes(); setDocStringAttrs($attrs, $1);
|
||||
$$ = new Scalar\String_(Scalar\String_::parseDocString($1, $2), $attrs); }
|
||||
{ $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
|
||||
| T_START_HEREDOC T_END_HEREDOC
|
||||
{ $attrs = attributes(); setDocStringAttrs($attrs, $1);
|
||||
$$ = new Scalar\String_('', $attrs); }
|
||||
{ $$ = $this->parseDocString($1, '', $2, attributes(), stackAttributes(#2), true); }
|
||||
| '"' encaps_list '"'
|
||||
{ $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
|
||||
parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
|
||||
| T_START_HEREDOC encaps_list T_END_HEREDOC
|
||||
{ $attrs = attributes(); setDocStringAttrs($attrs, $1);
|
||||
parseEncapsedDoc($2, true); $$ = new Scalar\Encapsed($2, $attrs); }
|
||||
{ $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
|
||||
;
|
||||
|
||||
optional_expr:
|
||||
@ -929,19 +954,27 @@ list_expr_elements:
|
||||
|
||||
list_expr_element:
|
||||
variable { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| '&' variable { $$ = Expr\ArrayItem[$2, null, true]; }
|
||||
| list_expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| expr T_DOUBLE_ARROW variable { $$ = Expr\ArrayItem[$3, $1, false]; }
|
||||
| expr T_DOUBLE_ARROW '&' variable { $$ = Expr\ArrayItem[$4, $1, true]; }
|
||||
| expr T_DOUBLE_ARROW list_expr { $$ = Expr\ArrayItem[$3, $1, false]; }
|
||||
| /* empty */ { $$ = null; }
|
||||
;
|
||||
|
||||
array_pair_list:
|
||||
inner_array_pair_list
|
||||
{ $$ = $1; $end = count($$)-1; if ($$[$end] === null) unset($$[$end]); }
|
||||
{ $$ = $1; $end = count($$)-1; if ($$[$end] === null) array_pop($$); }
|
||||
;
|
||||
|
||||
comma_or_error:
|
||||
','
|
||||
| error
|
||||
{ /* do nothing -- prevent default action of $$=$1. See #551. */ }
|
||||
;
|
||||
|
||||
inner_array_pair_list:
|
||||
inner_array_pair_list ',' array_pair { push($1, $3); }
|
||||
inner_array_pair_list comma_or_error array_pair { push($1, $3); }
|
||||
| array_pair { init($1); }
|
||||
;
|
||||
|
||||
@ -950,6 +983,7 @@ array_pair:
|
||||
| expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| expr T_DOUBLE_ARROW '&' variable { $$ = Expr\ArrayItem[$4, $1, true]; }
|
||||
| '&' variable { $$ = Expr\ArrayItem[$2, null, true]; }
|
||||
| T_ELLIPSIS expr { $$ = Expr\ArrayItem[$2, null, false, attributes(), true]; }
|
||||
| /* empty */ { $$ = null; }
|
||||
;
|
||||
|
||||
|
@ -13,9 +13,12 @@ $tmpResultFile = __DIR__ . '/tmp_parser.php';
|
||||
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
||||
$tokensResultsFile = $resultDir . '/Tokens.php';
|
||||
|
||||
// check for kmyacc.exe binary in this directory, otherwise fall back to global name
|
||||
$kmyacc = __DIR__ . '/kmyacc.exe';
|
||||
if (!file_exists($kmyacc)) {
|
||||
// check for kmyacc binary in this directory, otherwise fall back to global name
|
||||
if (file_exists(__DIR__ . '/kmyacc.exe')) {
|
||||
$kmyacc = __DIR__ . '/kmyacc.exe';
|
||||
} else if (file_exists(__DIR__ . '/kmyacc')) {
|
||||
$kmyacc = __DIR__ . '/kmyacc';
|
||||
} else {
|
||||
$kmyacc = 'kmyacc';
|
||||
}
|
||||
|
||||
@ -166,15 +169,6 @@ function resolveMacros($code) {
|
||||
. ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
|
||||
}
|
||||
|
||||
if ('parseEncapsedDoc' == $name) {
|
||||
assertArgs(2, $args, $name);
|
||||
|
||||
return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
|
||||
. ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, null, ' . $args[1] . '); } }'
|
||||
. ' $s->value = preg_replace(\'~(\r\n|\n|\r)\z~\', \'\', $s->value);'
|
||||
. ' if (\'\' === $s->value) array_pop(' . $args[0] . ');';
|
||||
}
|
||||
|
||||
if ('makeNop' == $name) {
|
||||
assertArgs(3, $args, $name);
|
||||
|
||||
@ -184,6 +178,15 @@ function resolveMacros($code) {
|
||||
. ' else { ' . $args[0] . ' = null; }';
|
||||
}
|
||||
|
||||
if ('makeZeroLengthNop' == $name) {
|
||||
assertArgs(2, $args, $name);
|
||||
|
||||
return '$startAttributes = ' . $args[1] . ';'
|
||||
. ' if (isset($startAttributes[\'comments\']))'
|
||||
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); }'
|
||||
. ' else { ' . $args[0] . ' = null; }';
|
||||
}
|
||||
|
||||
if ('strKind' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
@ -192,15 +195,6 @@ function resolveMacros($code) {
|
||||
. '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
|
||||
}
|
||||
|
||||
if ('setDocStringAttrs' == $name) {
|
||||
assertArgs(2, $args, $name);
|
||||
|
||||
return $args[0] . '[\'kind\'] = strpos(' . $args[1] . ', "\'") === false '
|
||||
. '? Scalar\String_::KIND_HEREDOC : Scalar\String_::KIND_NOWDOC; '
|
||||
. 'preg_match(\'/\A[bB]?<<<[ \t]*[\\\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\\\'"]?(?:\r\n|\n|\r)\z/\', ' . $args[1] . ', $matches); '
|
||||
. $args[0] . '[\'docLabel\'] = $matches[1];';
|
||||
}
|
||||
|
||||
if ('prependLeadingComments' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
%right T_YIELD
|
||||
%right T_DOUBLE_ARROW
|
||||
%right T_YIELD_FROM
|
||||
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL
|
||||
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL T_COALESCE_EQUAL
|
||||
%left '?' ':'
|
||||
%right T_COALESCE
|
||||
%left T_BOOLEAN_OR
|
||||
@ -64,6 +64,7 @@
|
||||
%token T_CONTINUE
|
||||
%token T_GOTO
|
||||
%token T_FUNCTION
|
||||
%token T_FN
|
||||
%token T_CONST
|
||||
%token T_RETURN
|
||||
%token T_TRY
|
||||
|
@ -1,40 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class Autoloader
|
||||
{
|
||||
/** @var bool Whether the autoloader has been registered. */
|
||||
private static $registered = false;
|
||||
|
||||
/**
|
||||
* Registers PhpParser\Autoloader as an SPL autoloader.
|
||||
*
|
||||
* @param bool $prepend Whether to prepend the autoloader instead of appending
|
||||
*/
|
||||
static public function register(bool $prepend = false) {
|
||||
if (self::$registered === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
spl_autoload_register([__CLASS__, 'autoload'], true, $prepend);
|
||||
self::$registered = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles autoloading of classes.
|
||||
*
|
||||
* @param string $class A class name.
|
||||
*/
|
||||
static public function autoload(string $class) {
|
||||
if (0 === strpos($class, 'PhpParser\\')) {
|
||||
$fileName = __DIR__ . strtr(substr($class, 9), '\\', '/') . '.php';
|
||||
if (file_exists($fileName)) {
|
||||
require $fileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -10,4 +10,4 @@ interface Builder
|
||||
* @return Node The built node
|
||||
*/
|
||||
public function getNode() : Node;
|
||||
}
|
||||
}
|
||||
|
@ -90,18 +90,18 @@ class Class_ extends Declaration
|
||||
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||
|
||||
$targets = [
|
||||
'Stmt_TraitUse' => &$this->uses,
|
||||
'Stmt_ClassConst' => &$this->constants,
|
||||
'Stmt_Property' => &$this->properties,
|
||||
'Stmt_ClassMethod' => &$this->methods,
|
||||
Stmt\TraitUse::class => &$this->uses,
|
||||
Stmt\ClassConst::class => &$this->constants,
|
||||
Stmt\Property::class => &$this->properties,
|
||||
Stmt\ClassMethod::class => &$this->methods,
|
||||
];
|
||||
|
||||
$type = $stmt->getType();
|
||||
if (!isset($targets[$type])) {
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
|
||||
$class = \get_class($stmt);
|
||||
if (!isset($targets[$class])) {
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||
}
|
||||
|
||||
$targets[$type][] = $stmt;
|
||||
$targets[$class][] = $stmt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -119,4 +119,4 @@ class Class_ extends Declaration
|
||||
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
|
||||
], $this->attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,4 +40,4 @@ abstract class Declaration implements PhpParser\Builder
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,20 +48,14 @@ class Interface_ extends Declaration
|
||||
public function addStmt($stmt) {
|
||||
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||
|
||||
$type = $stmt->getType();
|
||||
switch ($type) {
|
||||
case 'Stmt_ClassConst':
|
||||
$this->constants[] = $stmt;
|
||||
break;
|
||||
|
||||
case 'Stmt_ClassMethod':
|
||||
// we erase all statements in the body of an interface method
|
||||
$stmt->stmts = null;
|
||||
$this->methods[] = $stmt;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
|
||||
if ($stmt instanceof Stmt\ClassConst) {
|
||||
$this->constants[] = $stmt;
|
||||
} elseif ($stmt instanceof Stmt\ClassMethod) {
|
||||
// we erase all statements in the body of an interface method
|
||||
$stmt->stmts = null;
|
||||
$this->methods[] = $stmt;
|
||||
} else {
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -78,4 +72,4 @@ class Interface_ extends Declaration
|
||||
'stmts' => array_merge($this->constants, $this->methods),
|
||||
], $this->attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,13 +42,13 @@ class Param implements PhpParser\Builder
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets type hint for the parameter.
|
||||
* Sets type for the parameter.
|
||||
*
|
||||
* @param string|Node\Name|Node\NullableType $type Type hint to use
|
||||
* @param string|Node\Name|Node\NullableType $type Parameter type
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setTypeHint($type) {
|
||||
public function setType($type) {
|
||||
$this->type = BuilderHelpers::normalizeType($type);
|
||||
if ($this->type == 'void') {
|
||||
throw new \LogicException('Parameter type cannot be void');
|
||||
@ -57,6 +57,19 @@ class Param implements PhpParser\Builder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets type for the parameter.
|
||||
*
|
||||
* @param string|Node\Name|Node\NullableType $type Parameter type
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*
|
||||
* @deprecated Use setType() instead
|
||||
*/
|
||||
public function setTypeHint($type) {
|
||||
return $this->setType($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the parameter accept the value by reference.
|
||||
*
|
||||
|
@ -4,6 +4,9 @@ namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Property implements PhpParser\Builder
|
||||
@ -14,6 +17,9 @@ class Property implements PhpParser\Builder
|
||||
protected $default = null;
|
||||
protected $attributes = [];
|
||||
|
||||
/** @var null|Identifier|Name|NullableType */
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Creates a property builder.
|
||||
*
|
||||
@ -95,6 +101,19 @@ class Property implements PhpParser\Builder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the property type for PHP 7.4+.
|
||||
*
|
||||
* @param string|Name|NullableType|Identifier $type
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setType($type) {
|
||||
$this->type = BuilderHelpers::normalizeType($type);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built class node.
|
||||
*
|
||||
@ -106,7 +125,8 @@ class Property implements PhpParser\Builder
|
||||
[
|
||||
new Stmt\PropertyProperty($this->name, $this->default)
|
||||
],
|
||||
$this->attributes
|
||||
$this->attributes,
|
||||
$this->type
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
64
lib/PhpParser/Builder/TraitUse.php
Normal file
64
lib/PhpParser/Builder/TraitUse.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser\Builder;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class TraitUse implements Builder
|
||||
{
|
||||
protected $traits = [];
|
||||
protected $adaptations = [];
|
||||
|
||||
/**
|
||||
* Creates a trait use builder.
|
||||
*
|
||||
* @param Node\Name|string ...$traits Names of used traits
|
||||
*/
|
||||
public function __construct(...$traits) {
|
||||
foreach ($traits as $trait) {
|
||||
$this->and($trait);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds used trait.
|
||||
*
|
||||
* @param Node\Name|string $trait Trait name
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function and($trait) {
|
||||
$this->traits[] = BuilderHelpers::normalizeName($trait);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds trait adaptation.
|
||||
*
|
||||
* @param Stmt\TraitUseAdaptation|Builder\TraitUseAdaptation $adaptation Trait adaptation
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function with($adaptation) {
|
||||
$adaptation = BuilderHelpers::normalizeNode($adaptation);
|
||||
|
||||
if (!$adaptation instanceof Stmt\TraitUseAdaptation) {
|
||||
throw new \LogicException('Adaptation must have type TraitUseAdaptation');
|
||||
}
|
||||
|
||||
$this->adaptations[] = $adaptation;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built node.
|
||||
*
|
||||
* @return Node The built node
|
||||
*/
|
||||
public function getNode() : Node {
|
||||
return new Stmt\TraitUse($this->traits, $this->adaptations);
|
||||
}
|
||||
}
|
148
lib/PhpParser/Builder/TraitUseAdaptation.php
Normal file
148
lib/PhpParser/Builder/TraitUseAdaptation.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser\Builder;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class TraitUseAdaptation implements Builder
|
||||
{
|
||||
const TYPE_UNDEFINED = 0;
|
||||
const TYPE_ALIAS = 1;
|
||||
const TYPE_PRECEDENCE = 2;
|
||||
|
||||
/** @var int Type of building adaptation */
|
||||
protected $type;
|
||||
|
||||
protected $trait;
|
||||
protected $method;
|
||||
|
||||
protected $modifier = null;
|
||||
protected $alias = null;
|
||||
|
||||
protected $insteadof = [];
|
||||
|
||||
/**
|
||||
* Creates a trait use adaptation builder.
|
||||
*
|
||||
* @param Node\Name|string|null $trait Name of adaptated trait
|
||||
* @param Node\Identifier|string $method Name of adaptated method
|
||||
*/
|
||||
public function __construct($trait, $method) {
|
||||
$this->type = self::TYPE_UNDEFINED;
|
||||
|
||||
$this->trait = is_null($trait)? null: BuilderHelpers::normalizeName($trait);
|
||||
$this->method = BuilderHelpers::normalizeIdentifier($method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets alias of method.
|
||||
*
|
||||
* @param Node\Identifier|string $alias Alias for adaptated method
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function as($alias) {
|
||||
if ($this->type === self::TYPE_UNDEFINED) {
|
||||
$this->type = self::TYPE_ALIAS;
|
||||
}
|
||||
|
||||
if ($this->type !== self::TYPE_ALIAS) {
|
||||
throw new \LogicException('Cannot set alias for not alias adaptation buider');
|
||||
}
|
||||
|
||||
$this->alias = $alias;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets adaptated method public.
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePublic() {
|
||||
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets adaptated method protected.
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeProtected() {
|
||||
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets adaptated method private.
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePrivate() {
|
||||
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds overwritten traits.
|
||||
*
|
||||
* @param Node\Name|string ...$traits Traits for overwrite
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function insteadof(...$traits) {
|
||||
if ($this->type === self::TYPE_UNDEFINED) {
|
||||
if (is_null($this->trait)) {
|
||||
throw new \LogicException('Precedence adaptation must have trait');
|
||||
}
|
||||
|
||||
$this->type = self::TYPE_PRECEDENCE;
|
||||
}
|
||||
|
||||
if ($this->type !== self::TYPE_PRECEDENCE) {
|
||||
throw new \LogicException('Cannot add overwritten traits for not precedence adaptation buider');
|
||||
}
|
||||
|
||||
foreach ($traits as $trait) {
|
||||
$this->insteadof[] = BuilderHelpers::normalizeName($trait);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setModifier(int $modifier) {
|
||||
if ($this->type === self::TYPE_UNDEFINED) {
|
||||
$this->type = self::TYPE_ALIAS;
|
||||
}
|
||||
|
||||
if ($this->type !== self::TYPE_ALIAS) {
|
||||
throw new \LogicException('Cannot set access modifier for not alias adaptation buider');
|
||||
}
|
||||
|
||||
if (is_null($this->modifier)) {
|
||||
$this->modifier = $modifier;
|
||||
} else {
|
||||
throw new \LogicException('Multiple access type modifiers are not allowed');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built node.
|
||||
*
|
||||
* @return Node The built node
|
||||
*/
|
||||
public function getNode() : Node {
|
||||
switch ($this->type) {
|
||||
case self::TYPE_ALIAS:
|
||||
return new Stmt\TraitUseAdaptation\Alias($this->trait, $this->method, $this->modifier, $this->alias);
|
||||
case self::TYPE_PRECEDENCE:
|
||||
return new Stmt\TraitUseAdaptation\Precedence($this->trait, $this->method, $this->insteadof);
|
||||
default:
|
||||
throw new \LogicException('Type of adaptation is not defined');
|
||||
}
|
||||
}
|
||||
}
|
@ -34,9 +34,9 @@ class Trait_ extends Declaration
|
||||
|
||||
if ($stmt instanceof Stmt\Property) {
|
||||
$this->properties[] = $stmt;
|
||||
} else if ($stmt instanceof Stmt\ClassMethod) {
|
||||
} elseif ($stmt instanceof Stmt\ClassMethod) {
|
||||
$this->methods[] = $stmt;
|
||||
} else if ($stmt instanceof Stmt\TraitUse) {
|
||||
} elseif ($stmt instanceof Stmt\TraitUse) {
|
||||
$this->uses[] = $stmt;
|
||||
} else {
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||
|
@ -7,7 +7,8 @@ use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Use_ implements Builder {
|
||||
class Use_ implements Builder
|
||||
{
|
||||
protected $name;
|
||||
protected $type;
|
||||
protected $alias = null;
|
||||
|
@ -5,6 +5,8 @@ namespace PhpParser;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
|
||||
@ -54,6 +56,34 @@ class BuilderFactory
|
||||
return new Builder\Trait_($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a trait use builder.
|
||||
*
|
||||
* @param Node\Name|string ...$traits Trait names
|
||||
*
|
||||
* @return Builder\TraitUse The create trait use builder
|
||||
*/
|
||||
public function useTrait(...$traits) : Builder\TraitUse {
|
||||
return new Builder\TraitUse(...$traits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a trait use adaptation builder.
|
||||
*
|
||||
* @param Node\Name|string|null $trait Trait name
|
||||
* @param Node\Identifier|string $method Method name
|
||||
*
|
||||
* @return Builder\TraitUseAdaptation The create trait use adaptation builder
|
||||
*/
|
||||
public function traitUseAdaptation($trait, $method = null) : Builder\TraitUseAdaptation {
|
||||
if ($method === null) {
|
||||
$method = $trait;
|
||||
$trait = null;
|
||||
}
|
||||
|
||||
return new Builder\TraitUseAdaptation($trait, $method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a method builder.
|
||||
*
|
||||
@ -101,14 +131,36 @@ class BuilderFactory
|
||||
/**
|
||||
* Creates a namespace/class use builder.
|
||||
*
|
||||
* @param string|Node\Name Name to alias
|
||||
* @param Node\Name|string $name Name of the entity (namespace or class) to alias
|
||||
*
|
||||
* @return Builder\Use_ The create use builder
|
||||
* @return Builder\Use_ The created use builder
|
||||
*/
|
||||
public function use($name) : Builder\Use_ {
|
||||
return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function use builder.
|
||||
*
|
||||
* @param Node\Name|string $name Name of the function to alias
|
||||
*
|
||||
* @return Builder\Use_ The created use function builder
|
||||
*/
|
||||
public function useFunction($name) : Builder\Use_ {
|
||||
return new Builder\Use_($name, Use_::TYPE_FUNCTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a constant use builder.
|
||||
*
|
||||
* @param Node\Name|string $name Name of the const to alias
|
||||
*
|
||||
* @return Builder\Use_ The created use const builder
|
||||
*/
|
||||
public function useConst($name) : Builder\Use_ {
|
||||
return new Builder\Use_($name, Use_::TYPE_CONSTANT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates node a for a literal value.
|
||||
*
|
||||
@ -120,6 +172,21 @@ class BuilderFactory
|
||||
return BuilderHelpers::normalizeValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates variable node.
|
||||
*
|
||||
* @param string|Expr $name Name
|
||||
*
|
||||
* @return Expr\Variable
|
||||
*/
|
||||
public function var($name) : Expr\Variable {
|
||||
if (!\is_string($name) && !$name instanceof Expr) {
|
||||
throw new \LogicException('Variable name must be string or Expr');
|
||||
}
|
||||
|
||||
return new Expr\Variable($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes an argument list.
|
||||
*
|
||||
@ -141,6 +208,108 @@ class BuilderFactory
|
||||
return $normalizedArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function call node.
|
||||
*
|
||||
* @param string|Name|Expr $name Function name
|
||||
* @param array $args Function arguments
|
||||
*
|
||||
* @return Expr\FuncCall
|
||||
*/
|
||||
public function funcCall($name, array $args = []) : Expr\FuncCall {
|
||||
return new Expr\FuncCall(
|
||||
BuilderHelpers::normalizeNameOrExpr($name),
|
||||
$this->args($args)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a method call node.
|
||||
*
|
||||
* @param Expr $var Variable the method is called on
|
||||
* @param string|Identifier|Expr $name Method name
|
||||
* @param array $args Method arguments
|
||||
*
|
||||
* @return Expr\MethodCall
|
||||
*/
|
||||
public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
|
||||
return new Expr\MethodCall(
|
||||
$var,
|
||||
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
||||
$this->args($args)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a static method call node.
|
||||
*
|
||||
* @param string|Name|Expr $class Class name
|
||||
* @param string|Identifier|Expr $name Method name
|
||||
* @param array $args Method arguments
|
||||
*
|
||||
* @return Expr\StaticCall
|
||||
*/
|
||||
public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
|
||||
return new Expr\StaticCall(
|
||||
BuilderHelpers::normalizeNameOrExpr($class),
|
||||
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
||||
$this->args($args)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an object creation node.
|
||||
*
|
||||
* @param string|Name|Expr $class Class name
|
||||
* @param array $args Constructor arguments
|
||||
*
|
||||
* @return Expr\New_
|
||||
*/
|
||||
public function new($class, array $args = []) : Expr\New_ {
|
||||
return new Expr\New_(
|
||||
BuilderHelpers::normalizeNameOrExpr($class),
|
||||
$this->args($args)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a constant fetch node.
|
||||
*
|
||||
* @param string|Name $name Constant name
|
||||
*
|
||||
* @return Expr\ConstFetch
|
||||
*/
|
||||
public function constFetch($name) : Expr\ConstFetch {
|
||||
return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a property fetch node.
|
||||
*
|
||||
* @param Expr $var Variable holding object
|
||||
* @param string|Identifier|Expr $name Property name
|
||||
*
|
||||
* @return Expr\PropertyFetch
|
||||
*/
|
||||
public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch {
|
||||
return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a class constant fetch node.
|
||||
*
|
||||
* @param string|Name|Expr $class Class name
|
||||
* @param string|Identifier $name Constant name
|
||||
*
|
||||
* @return Expr\ClassConstFetch
|
||||
*/
|
||||
public function classConstFetch($class, $name): Expr\ClassConstFetch {
|
||||
return new Expr\ClassConstFetch(
|
||||
BuilderHelpers::normalizeNameOrExpr($class),
|
||||
BuilderHelpers::normalizeIdentifier($name)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates nested Concat nodes from a list of expressions.
|
||||
*
|
||||
@ -161,12 +330,16 @@ class BuilderFactory
|
||||
return $lastConcat;
|
||||
}
|
||||
|
||||
private function normalizeStringExpr($expr) {
|
||||
/**
|
||||
* @param string|Expr $expr
|
||||
* @return Expr
|
||||
*/
|
||||
private function normalizeStringExpr($expr) : Expr {
|
||||
if ($expr instanceof Expr) {
|
||||
return $expr;
|
||||
}
|
||||
|
||||
if (is_string($expr)) {
|
||||
if (\is_string($expr)) {
|
||||
return new String_($expr);
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,8 @@ use PhpParser\Node\Stmt;
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class BuilderHelpers {
|
||||
final class BuilderHelpers
|
||||
{
|
||||
/**
|
||||
* Normalizes a node: Converts builder objects to nodes.
|
||||
*
|
||||
@ -55,13 +56,74 @@ final class BuilderHelpers {
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a name: Converts plain string names to PhpParser\Node\Name.
|
||||
* Normalizes strings to Identifier.
|
||||
*
|
||||
* @param string|Identifier $name The identifier to normalize
|
||||
*
|
||||
* @return Identifier The normalized identifier
|
||||
*/
|
||||
public static function normalizeIdentifier($name) : Identifier {
|
||||
if ($name instanceof Identifier) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
if (\is_string($name)) {
|
||||
return new Identifier($name);
|
||||
}
|
||||
|
||||
throw new \LogicException('Expected string or instance of Node\Identifier');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes strings to Identifier, also allowing expressions.
|
||||
*
|
||||
* @param string|Identifier|Expr $name The identifier to normalize
|
||||
*
|
||||
* @return Identifier|Expr The normalized identifier or expression
|
||||
*/
|
||||
public static function normalizeIdentifierOrExpr($name) {
|
||||
if ($name instanceof Identifier || $name instanceof Expr) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
if (\is_string($name)) {
|
||||
return new Identifier($name);
|
||||
}
|
||||
|
||||
throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a name: Converts string names to Name nodes.
|
||||
*
|
||||
* @param Name|string $name The name to normalize
|
||||
*
|
||||
* @return Name The normalized name
|
||||
*/
|
||||
public static function normalizeName($name) : Name {
|
||||
return self::normalizeNameCommon($name, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a name: Converts string names to Name nodes, while also allowing expressions.
|
||||
*
|
||||
* @param Expr|Name|string $name The name to normalize
|
||||
*
|
||||
* @return Name|Expr The normalized name or expression
|
||||
*/
|
||||
public static function normalizeNameOrExpr($name) {
|
||||
return self::normalizeNameCommon($name, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a name: Converts string names to Name nodes, optionally allowing expressions.
|
||||
*
|
||||
* @param Expr|Name|string $name The name to normalize
|
||||
* @param bool $allowExpr Whether to also allow expressions
|
||||
*
|
||||
* @return Name|Expr The normalized name, or expression (if allowed)
|
||||
*/
|
||||
private static function normalizeNameCommon($name, bool $allowExpr) {
|
||||
if ($name instanceof Name) {
|
||||
return $name;
|
||||
} elseif (is_string($name)) {
|
||||
@ -78,7 +140,16 @@ final class BuilderHelpers {
|
||||
}
|
||||
}
|
||||
|
||||
throw new \LogicException('Name must be a string or an instance of PhpParser\Node\Name');
|
||||
if ($allowExpr) {
|
||||
if ($name instanceof Expr) {
|
||||
return $name;
|
||||
}
|
||||
throw new \LogicException(
|
||||
'Name must be a string or an instance of Node\Name or Node\Expr'
|
||||
);
|
||||
} else {
|
||||
throw new \LogicException('Name must be a string or an instance of Node\Name');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -184,7 +255,7 @@ final class BuilderHelpers {
|
||||
public static function normalizeDocComment($docComment) : Comment\Doc {
|
||||
if ($docComment instanceof Comment\Doc) {
|
||||
return $docComment;
|
||||
} else if (is_string($docComment)) {
|
||||
} elseif (is_string($docComment)) {
|
||||
return new Comment\Doc($docComment);
|
||||
} else {
|
||||
throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
|
||||
|
@ -164,4 +164,4 @@ class Comment implements \JsonSerializable
|
||||
'tokenPos' => $this->tokenPos,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ namespace PhpParser\Comment;
|
||||
|
||||
class Doc extends \PhpParser\Comment
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -2,4 +2,5 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
class ConstExprEvaluationException extends \Exception {}
|
||||
class ConstExprEvaluationException extends \Exception
|
||||
{}
|
||||
|
@ -20,15 +20,12 @@ use PhpParser\Node\Scalar;
|
||||
*
|
||||
* The fallback evaluator should throw ConstExprEvaluationException for nodes it cannot evaluate.
|
||||
*
|
||||
* The evaluation is performed as PHP would perform it, and as such may generate notices, warnings
|
||||
* or Errors. For example, if the expression `1%0` is evaluated, an ArithmeticError is thrown. It is
|
||||
* left to the consumer to handle these as appropriate.
|
||||
*
|
||||
* The evaluation is also dependent on runtime configuration in two respects: Firstly, floating
|
||||
* The evaluation is dependent on runtime configuration in two respects: Firstly, floating
|
||||
* point to string conversions are affected by the precision ini setting. Secondly, they are also
|
||||
* affected by the LC_NUMERIC locale.
|
||||
*/
|
||||
class ConstExprEvaluator {
|
||||
class ConstExprEvaluator
|
||||
{
|
||||
private $fallbackEvaluator;
|
||||
|
||||
/**
|
||||
@ -48,7 +45,10 @@ class ConstExprEvaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a constant expression into a PHP value.
|
||||
* Silently evaluates a constant expression into a PHP value.
|
||||
*
|
||||
* Thrown Errors, warnings or notices will be converted into a ConstExprEvaluationException.
|
||||
* The original source of the exception is available through getPrevious().
|
||||
*
|
||||
* If some part of the expression cannot be evaluated, the fallback evaluator passed to the
|
||||
* constructor will be invoked. By default, if no fallback is provided, an exception of type
|
||||
@ -58,9 +58,49 @@ class ConstExprEvaluator {
|
||||
*
|
||||
* @param Expr $expr Constant expression to evaluate
|
||||
* @return mixed Result of evaluation
|
||||
*
|
||||
* @throws ConstExprEvaluationException if the expression cannot be evaluated or an error occurred
|
||||
*/
|
||||
public function evaluateSilently(Expr $expr) {
|
||||
set_error_handler(function($num, $str, $file, $line) {
|
||||
throw new \ErrorException($str, 0, $num, $file, $line);
|
||||
});
|
||||
|
||||
try {
|
||||
return $this->evaluate($expr);
|
||||
} catch (\Throwable $e) {
|
||||
if (!$e instanceof ConstExprEvaluationException) {
|
||||
$e = new ConstExprEvaluationException(
|
||||
"An error occurred during constant expression evaluation", 0, $e);
|
||||
}
|
||||
throw $e;
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly evaluates a constant expression into a PHP value.
|
||||
*
|
||||
* May generate Error exceptions, warnings or notices. Use evaluateSilently() to convert these
|
||||
* into a ConstExprEvaluationException.
|
||||
*
|
||||
* If some part of the expression cannot be evaluated, the fallback evaluator passed to the
|
||||
* constructor will be invoked. By default, if no fallback is provided, an exception of type
|
||||
* ConstExprEvaluationException is thrown.
|
||||
*
|
||||
* See class doc comment for caveats and limitations.
|
||||
*
|
||||
* @param Expr $expr Constant expression to evaluate
|
||||
* @return mixed Result of evaluation
|
||||
*
|
||||
* @throws ConstExprEvaluationException if the expression cannot be evaluated
|
||||
*/
|
||||
public function evaluate(Expr $expr) {
|
||||
public function evaluateDirectly(Expr $expr) {
|
||||
return $this->evaluate($expr);
|
||||
}
|
||||
|
||||
private function evaluate(Expr $expr) {
|
||||
if ($expr instanceof Scalar\LNumber
|
||||
|| $expr instanceof Scalar\DNumber
|
||||
|| $expr instanceof Scalar\String_
|
||||
@ -183,4 +223,4 @@ class ConstExprEvaluator {
|
||||
|
||||
return ($this->fallbackEvaluator)($expr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ class Error extends \RuntimeException
|
||||
* (or start line of error -- deprecated)
|
||||
*/
|
||||
public function __construct(string $message, $attributes = []) {
|
||||
$this->rawMessage = (string) $message;
|
||||
$this->rawMessage = $message;
|
||||
if (is_array($attributes)) {
|
||||
$this->attributes = $attributes;
|
||||
} else {
|
||||
@ -51,7 +51,6 @@ class Error extends \RuntimeException
|
||||
return $this->attributes['endLine'] ?? -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the attributes of the node/token the error occurred at.
|
||||
*
|
||||
@ -62,7 +61,7 @@ class Error extends \RuntimeException
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the attributes of the node/token the error occured at.
|
||||
* Sets the attributes of the node/token the error occurred at.
|
||||
*
|
||||
* @param array $attributes
|
||||
*/
|
||||
@ -77,7 +76,7 @@ class Error extends \RuntimeException
|
||||
* @param string $message Error message
|
||||
*/
|
||||
public function setRawMessage(string $message) {
|
||||
$this->rawMessage = (string) $message;
|
||||
$this->rawMessage = $message;
|
||||
$this->updateMessage();
|
||||
}
|
||||
|
||||
@ -87,7 +86,7 @@ class Error extends \RuntimeException
|
||||
* @param int $line Error start line
|
||||
*/
|
||||
public function setStartLine(int $line) {
|
||||
$this->attributes['startLine'] = (int) $line;
|
||||
$this->attributes['startLine'] = $line;
|
||||
$this->updateMessage();
|
||||
}
|
||||
|
||||
@ -99,7 +98,7 @@ class Error extends \RuntimeException
|
||||
* @return bool
|
||||
*/
|
||||
public function hasColumnInfo() : bool {
|
||||
return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
|
||||
return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,4 +10,4 @@ interface ErrorHandler
|
||||
* @param Error $error The error that needs to be handled
|
||||
*/
|
||||
public function handleError(Error $error);
|
||||
}
|
||||
}
|
||||
|
@ -43,4 +43,4 @@ class Collecting implements ErrorHandler
|
||||
public function clearErrors() {
|
||||
$this->errors = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,4 +15,4 @@ class Throwing implements ErrorHandler
|
||||
public function handleError(Error $error) {
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ namespace PhpParser\Internal;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class DiffElem {
|
||||
class DiffElem
|
||||
{
|
||||
const TYPE_KEEP = 0;
|
||||
const TYPE_REMOVE = 1;
|
||||
const TYPE_ADD = 2;
|
||||
@ -23,4 +24,4 @@ class DiffElem {
|
||||
$this->old = $old;
|
||||
$this->new = $new;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,13 +10,14 @@ namespace PhpParser\Internal;
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Differ {
|
||||
class Differ
|
||||
{
|
||||
private $isEqual;
|
||||
|
||||
/**
|
||||
* Create differ over the given equality relation.
|
||||
*
|
||||
* @param callable $isEqual Equality relation with signature function($a, $b): bool
|
||||
* @param callable $isEqual Equality relation with signature function($a, $b) : bool
|
||||
*/
|
||||
public function __construct(callable $isEqual) {
|
||||
$this->isEqual = $isEqual;
|
||||
@ -160,4 +161,4 @@ class Differ {
|
||||
}
|
||||
return $newDiff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,8 @@ use PhpParser\Node\Expr;
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class PrintableNewAnonClassNode extends Expr {
|
||||
class PrintableNewAnonClassNode extends Expr
|
||||
{
|
||||
/** @var Node\Arg[] Arguments */
|
||||
public $args;
|
||||
/** @var null|Node\Name Name of extended class */
|
||||
@ -38,18 +39,19 @@ class PrintableNewAnonClassNode extends Expr {
|
||||
public static function fromNewNode(Expr\New_ $newNode) {
|
||||
$class = $newNode->class;
|
||||
assert($class instanceof Node\Stmt\Class_);
|
||||
assert($class->name === null);
|
||||
// We don't assert that $class->name is null here, to allow consumers to assign unique names
|
||||
// to anonymous classes for their own purposes. We simplify ignore the name here.
|
||||
return new self(
|
||||
$newNode->args, $class->extends, $class->implements,
|
||||
$class->stmts, $newNode->getAttributes()
|
||||
);
|
||||
}
|
||||
|
||||
public function getType(): string {
|
||||
public function getType() : string {
|
||||
return 'Expr_PrintableNewAnonClass';
|
||||
}
|
||||
|
||||
public function getSubNodeNames() : array {
|
||||
return ['args', 'extends', 'implements', 'stmts'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser;
|
||||
namespace PhpParser\Internal;
|
||||
|
||||
/**
|
||||
* Provides operations on token streams, for use by pretty printer.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class TokenStream {
|
||||
class TokenStream
|
||||
{
|
||||
/** @var array Tokens (in token_get_all format) */
|
||||
private $tokens;
|
||||
/** @var int[] Map from position to indentation */
|
||||
@ -214,7 +215,7 @@ class TokenStream {
|
||||
// TODO Handle non-space indentation
|
||||
if ($indent < 0) {
|
||||
$result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $content);
|
||||
} else if ($indent > 0) {
|
||||
} elseif ($indent > 0) {
|
||||
$result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $content);
|
||||
} else {
|
||||
$result .= $content;
|
||||
@ -252,4 +253,4 @@ class TokenStream {
|
||||
|
||||
return $indentMap;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,8 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
class JsonDecoder {
|
||||
class JsonDecoder
|
||||
{
|
||||
/** @var \ReflectionClass[] Node type to reflection class map */
|
||||
private $reflectionClassCache;
|
||||
|
||||
@ -97,4 +98,4 @@ class JsonDecoder {
|
||||
|
||||
throw new \RuntimeException("Unknown node type \"$nodeType\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,13 @@ class Lexer
|
||||
protected $tokenMap;
|
||||
protected $dropTokens;
|
||||
|
||||
protected $usedAttributes;
|
||||
private $attributeStartLineUsed;
|
||||
private $attributeEndLineUsed;
|
||||
private $attributeStartTokenPosUsed;
|
||||
private $attributeEndTokenPosUsed;
|
||||
private $attributeStartFilePosUsed;
|
||||
private $attributeEndFilePosUsed;
|
||||
private $attributeCommentsUsed;
|
||||
|
||||
/**
|
||||
* Creates a Lexer.
|
||||
@ -37,12 +43,17 @@ class Lexer
|
||||
[\T_WHITESPACE, \T_OPEN_TAG, \T_COMMENT, \T_DOC_COMMENT], 1
|
||||
);
|
||||
|
||||
// the usedAttributes member is a map of the used attribute names to a dummy
|
||||
// value (here "true")
|
||||
$options += [
|
||||
'usedAttributes' => ['comments', 'startLine', 'endLine'],
|
||||
];
|
||||
$this->usedAttributes = array_fill_keys($options['usedAttributes'], true);
|
||||
$defaultAttributes = ['comments', 'startLine', 'endLine'];
|
||||
$usedAttributes = array_fill_keys($options['usedAttributes'] ?? $defaultAttributes, true);
|
||||
|
||||
// Create individual boolean properties to make these checks faster.
|
||||
$this->attributeStartLineUsed = isset($usedAttributes['startLine']);
|
||||
$this->attributeEndLineUsed = isset($usedAttributes['endLine']);
|
||||
$this->attributeStartTokenPosUsed = isset($usedAttributes['startTokenPos']);
|
||||
$this->attributeEndTokenPosUsed = isset($usedAttributes['endTokenPos']);
|
||||
$this->attributeStartFilePosUsed = isset($usedAttributes['startFilePos']);
|
||||
$this->attributeEndFilePosUsed = isset($usedAttributes['endFilePos']);
|
||||
$this->attributeCommentsUsed = isset($usedAttributes['comments']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -153,7 +164,7 @@ class Lexer
|
||||
$nextFilePos = strpos($this->code, $tokenValue, $filePos);
|
||||
$this->handleInvalidCharacterRange(
|
||||
$filePos, $nextFilePos, $line, $errorHandler);
|
||||
$filePos = (int)$nextFilePos;
|
||||
$filePos = (int) $nextFilePos;
|
||||
}
|
||||
|
||||
$filePos += $tokenLen;
|
||||
@ -230,13 +241,13 @@ class Lexer
|
||||
$token = "\0";
|
||||
}
|
||||
|
||||
if (isset($this->usedAttributes['startLine'])) {
|
||||
if ($this->attributeStartLineUsed) {
|
||||
$startAttributes['startLine'] = $this->line;
|
||||
}
|
||||
if (isset($this->usedAttributes['startTokenPos'])) {
|
||||
if ($this->attributeStartTokenPosUsed) {
|
||||
$startAttributes['startTokenPos'] = $this->pos;
|
||||
}
|
||||
if (isset($this->usedAttributes['startFilePos'])) {
|
||||
if ($this->attributeStartFilePosUsed) {
|
||||
$startAttributes['startFilePos'] = $this->filePos;
|
||||
}
|
||||
|
||||
@ -255,7 +266,7 @@ class Lexer
|
||||
$id = $this->tokenMap[$token[0]];
|
||||
if (\T_CLOSE_TAG === $token[0]) {
|
||||
$this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
|
||||
} else if (\T_INLINE_HTML === $token[0]) {
|
||||
} elseif (\T_INLINE_HTML === $token[0]) {
|
||||
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
|
||||
}
|
||||
|
||||
@ -263,7 +274,7 @@ class Lexer
|
||||
$this->filePos += \strlen($value);
|
||||
} else {
|
||||
if (\T_COMMENT === $token[0] || \T_DOC_COMMENT === $token[0]) {
|
||||
if (isset($this->usedAttributes['comments'])) {
|
||||
if ($this->attributeCommentsUsed) {
|
||||
$comment = \T_DOC_COMMENT === $token[0]
|
||||
? new Comment\Doc($token[1], $this->line, $this->filePos, $this->pos)
|
||||
: new Comment($token[1], $this->line, $this->filePos, $this->pos);
|
||||
@ -276,13 +287,13 @@ class Lexer
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($this->usedAttributes['endLine'])) {
|
||||
if ($this->attributeEndLineUsed) {
|
||||
$endAttributes['endLine'] = $this->line;
|
||||
}
|
||||
if (isset($this->usedAttributes['endTokenPos'])) {
|
||||
if ($this->attributeEndTokenPosUsed) {
|
||||
$endAttributes['endTokenPos'] = $this->pos;
|
||||
}
|
||||
if (isset($this->usedAttributes['endFilePos'])) {
|
||||
if ($this->attributeEndFilePosUsed) {
|
||||
$endAttributes['endFilePos'] = $this->filePos - 1;
|
||||
}
|
||||
|
||||
@ -357,7 +368,7 @@ class Lexer
|
||||
if ('T_HASHBANG' === $name) {
|
||||
// HHVM uses a special token for #! hashbang lines
|
||||
$tokenMap[$i] = Tokens::T_INLINE_HTML;
|
||||
} else if (defined($name = 'PhpParser\Parser\Tokens::' . $name)) {
|
||||
} elseif (defined($name = Tokens::class . '::' . $name)) {
|
||||
// Other tokens can be mapped directly
|
||||
$tokenMap[$i] = constant($name);
|
||||
}
|
||||
|
@ -2,8 +2,247 @@
|
||||
|
||||
namespace PhpParser\Lexer;
|
||||
|
||||
use PhpParser\Error;
|
||||
use PhpParser\ErrorHandler;
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
|
||||
use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
|
||||
use PhpParser\Lexer\TokenEmulator\TokenEmulatorInterface;
|
||||
|
||||
class Emulative extends \PhpParser\Lexer
|
||||
class Emulative extends Lexer
|
||||
{
|
||||
/* No features requiring emulation have been added in PHP > 7.0 */
|
||||
}
|
||||
const PHP_7_3 = '7.3.0dev';
|
||||
const PHP_7_4 = '7.4.0dev';
|
||||
|
||||
const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX'
|
||||
/<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
|
||||
(?:.*\r?\n)*?
|
||||
(?<indentation>\h*)\2(?![a-zA-Z_\x80-\xff])(?<separator>(?:;?[\r\n])?)/x
|
||||
REGEX;
|
||||
|
||||
/** @var mixed[] Patches used to reverse changes introduced in the code */
|
||||
private $patches = [];
|
||||
|
||||
/** @var TokenEmulatorInterface[] */
|
||||
private $tokenEmulators = [];
|
||||
|
||||
/**
|
||||
* @param mixed[] $options
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
parent::__construct($options);
|
||||
|
||||
// prepare token emulators
|
||||
$this->tokenEmulators[] = new FnTokenEmulator();
|
||||
$this->tokenEmulators[] = new CoaleseEqualTokenEmulator();
|
||||
|
||||
// add emulated tokens here
|
||||
foreach ($this->tokenEmulators as $emulativeToken) {
|
||||
$this->tokenMap[$emulativeToken->getTokenId()] = $emulativeToken->getParserTokenId();
|
||||
}
|
||||
}
|
||||
|
||||
public function startLexing(string $code, ErrorHandler $errorHandler = null) {
|
||||
$this->patches = [];
|
||||
|
||||
if ($this->isEmulationNeeded($code) === false) {
|
||||
// Nothing to emulate, yay
|
||||
parent::startLexing($code, $errorHandler);
|
||||
return;
|
||||
}
|
||||
|
||||
$collector = new ErrorHandler\Collecting();
|
||||
|
||||
// 1. emulation of heredoc and nowdoc new syntax
|
||||
$preparedCode = $this->processHeredocNowdoc($code);
|
||||
parent::startLexing($preparedCode, $collector);
|
||||
|
||||
// add token emulation
|
||||
foreach ($this->tokenEmulators as $emulativeToken) {
|
||||
if ($emulativeToken->isEmulationNeeded($code)) {
|
||||
$this->tokens = $emulativeToken->emulate($code, $this->tokens);
|
||||
}
|
||||
}
|
||||
|
||||
$this->fixupTokens();
|
||||
|
||||
$errors = $collector->getErrors();
|
||||
if (!empty($errors)) {
|
||||
$this->fixupErrors($errors);
|
||||
foreach ($errors as $error) {
|
||||
$errorHandler->handleError($error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function isHeredocNowdocEmulationNeeded(string $code): bool
|
||||
{
|
||||
// skip version where this works without emulation
|
||||
if (version_compare(\PHP_VERSION, self::PHP_7_3, '>=')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strpos($code, '<<<') !== false;
|
||||
}
|
||||
|
||||
private function processHeredocNowdoc(string $code): string
|
||||
{
|
||||
if ($this->isHeredocNowdocEmulationNeeded($code) === false) {
|
||||
return $code;
|
||||
}
|
||||
|
||||
if (!preg_match_all(self::FLEXIBLE_DOC_STRING_REGEX, $code, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
|
||||
// No heredoc/nowdoc found
|
||||
return $code;
|
||||
}
|
||||
|
||||
// Keep track of how much we need to adjust string offsets due to the modifications we
|
||||
// already made
|
||||
$posDelta = 0;
|
||||
foreach ($matches as $match) {
|
||||
$indentation = $match['indentation'][0];
|
||||
$indentationStart = $match['indentation'][1];
|
||||
|
||||
$separator = $match['separator'][0];
|
||||
$separatorStart = $match['separator'][1];
|
||||
|
||||
if ($indentation === '' && $separator !== '') {
|
||||
// Ordinary heredoc/nowdoc
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($indentation !== '') {
|
||||
// Remove indentation
|
||||
$indentationLen = strlen($indentation);
|
||||
$code = substr_replace($code, '', $indentationStart + $posDelta, $indentationLen);
|
||||
$this->patches[] = [$indentationStart + $posDelta, 'add', $indentation];
|
||||
$posDelta -= $indentationLen;
|
||||
}
|
||||
|
||||
if ($separator === '') {
|
||||
// Insert newline as separator
|
||||
$code = substr_replace($code, "\n", $separatorStart + $posDelta, 0);
|
||||
$this->patches[] = [$separatorStart + $posDelta, 'remove', "\n"];
|
||||
$posDelta += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
private function isEmulationNeeded(string $code): bool
|
||||
{
|
||||
foreach ($this->tokenEmulators as $emulativeToken) {
|
||||
if ($emulativeToken->isEmulationNeeded($code)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->isHeredocNowdocEmulationNeeded($code);
|
||||
}
|
||||
|
||||
private function fixupTokens()
|
||||
{
|
||||
if (\count($this->patches) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Load first patch
|
||||
$patchIdx = 0;
|
||||
|
||||
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
||||
|
||||
// We use a manual loop over the tokens, because we modify the array on the fly
|
||||
$pos = 0;
|
||||
for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
|
||||
$token = $this->tokens[$i];
|
||||
if (\is_string($token)) {
|
||||
// We assume that patches don't apply to string tokens
|
||||
$pos += \strlen($token);
|
||||
continue;
|
||||
}
|
||||
|
||||
$len = \strlen($token[1]);
|
||||
$posDelta = 0;
|
||||
while ($patchPos >= $pos && $patchPos < $pos + $len) {
|
||||
$patchTextLen = \strlen($patchText);
|
||||
if ($patchType === 'remove') {
|
||||
if ($patchPos === $pos && $patchTextLen === $len) {
|
||||
// Remove token entirely
|
||||
array_splice($this->tokens, $i, 1, []);
|
||||
$i--;
|
||||
$c--;
|
||||
} else {
|
||||
// Remove from token string
|
||||
$this->tokens[$i][1] = substr_replace(
|
||||
$token[1], '', $patchPos - $pos + $posDelta, $patchTextLen
|
||||
);
|
||||
$posDelta -= $patchTextLen;
|
||||
}
|
||||
} elseif ($patchType === 'add') {
|
||||
// Insert into the token string
|
||||
$this->tokens[$i][1] = substr_replace(
|
||||
$token[1], $patchText, $patchPos - $pos + $posDelta, 0
|
||||
);
|
||||
$posDelta += $patchTextLen;
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// Fetch the next patch
|
||||
$patchIdx++;
|
||||
if ($patchIdx >= \count($this->patches)) {
|
||||
// No more patches, we're done
|
||||
return;
|
||||
}
|
||||
|
||||
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
||||
|
||||
// Multiple patches may apply to the same token. Reload the current one to check
|
||||
// If the new patch applies
|
||||
$token = $this->tokens[$i];
|
||||
}
|
||||
|
||||
$pos += $len;
|
||||
}
|
||||
|
||||
// A patch did not apply
|
||||
assert(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixup line and position information in errors.
|
||||
*
|
||||
* @param Error[] $errors
|
||||
*/
|
||||
private function fixupErrors(array $errors) {
|
||||
foreach ($errors as $error) {
|
||||
$attrs = $error->getAttributes();
|
||||
|
||||
$posDelta = 0;
|
||||
$lineDelta = 0;
|
||||
foreach ($this->patches as $patch) {
|
||||
list($patchPos, $patchType, $patchText) = $patch;
|
||||
if ($patchPos >= $attrs['startFilePos']) {
|
||||
// No longer relevant
|
||||
break;
|
||||
}
|
||||
|
||||
if ($patchType === 'add') {
|
||||
$posDelta += strlen($patchText);
|
||||
$lineDelta += substr_count($patchText, "\n");
|
||||
} else {
|
||||
$posDelta -= strlen($patchText);
|
||||
$lineDelta -= substr_count($patchText, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
$attrs['startFilePos'] += $posDelta;
|
||||
$attrs['endFilePos'] += $posDelta;
|
||||
$attrs['startLine'] += $lineDelta;
|
||||
$attrs['endLine'] += $lineDelta;
|
||||
$error->setAttributes($attrs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,54 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Lexer\TokenEmulator;
|
||||
|
||||
use PhpParser\Lexer\Emulative;
|
||||
use PhpParser\Parser\Tokens;
|
||||
|
||||
final class CoaleseEqualTokenEmulator implements TokenEmulatorInterface
|
||||
{
|
||||
const T_COALESCE_EQUAL = 1007;
|
||||
|
||||
public function getTokenId(): int
|
||||
{
|
||||
return self::T_COALESCE_EQUAL;
|
||||
}
|
||||
|
||||
public function getParserTokenId(): int
|
||||
{
|
||||
return Tokens::T_COALESCE_EQUAL;
|
||||
}
|
||||
|
||||
public function isEmulationNeeded(string $code) : bool
|
||||
{
|
||||
// skip version where this is supported
|
||||
if (version_compare(\PHP_VERSION, Emulative::PHP_7_4, '>=')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strpos($code, '??=') !== false;
|
||||
}
|
||||
|
||||
public function emulate(string $code, array $tokens): array
|
||||
{
|
||||
// We need to manually iterate and manage a count because we'll change
|
||||
// the tokens array on the way
|
||||
$line = 1;
|
||||
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
||||
if (isset($tokens[$i + 1])) {
|
||||
if ($tokens[$i][0] === T_COALESCE && $tokens[$i + 1] === '=') {
|
||||
array_splice($tokens, $i, 2, [
|
||||
[self::T_COALESCE_EQUAL, '??=', $line]
|
||||
]);
|
||||
$c--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (\is_array($tokens[$i])) {
|
||||
$line += substr_count($tokens[$i][1], "\n");
|
||||
}
|
||||
}
|
||||
|
||||
return $tokens;
|
||||
}
|
||||
}
|
66
lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php
Normal file
66
lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Lexer\TokenEmulator;
|
||||
|
||||
use PhpParser\Lexer\Emulative;
|
||||
use PhpParser\Parser\Tokens;
|
||||
|
||||
final class FnTokenEmulator implements TokenEmulatorInterface
|
||||
{
|
||||
const T_FN = 1008;
|
||||
|
||||
public function getTokenId(): int
|
||||
{
|
||||
return self::T_FN;
|
||||
}
|
||||
|
||||
public function getParserTokenId(): int
|
||||
{
|
||||
return Tokens::T_FN;
|
||||
}
|
||||
|
||||
public function isEmulationNeeded(string $code) : bool
|
||||
{
|
||||
// skip version where this is supported
|
||||
if (version_compare(\PHP_VERSION, Emulative::PHP_7_4, '>=')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strpos($code, 'fn') !== false;
|
||||
}
|
||||
|
||||
public function emulate(string $code, array $tokens): array
|
||||
{
|
||||
// We need to manually iterate and manage a count because we'll change
|
||||
// the tokens array on the way
|
||||
foreach ($tokens as $i => $token) {
|
||||
if ($token[0] === T_STRING && $token[1] === 'fn') {
|
||||
$previousNonSpaceToken = $this->getPreviousNonSpaceToken($tokens, $i);
|
||||
if ($previousNonSpaceToken !== null && $previousNonSpaceToken[0] === T_OBJECT_OPERATOR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tokens[$i][0] = self::T_FN;
|
||||
}
|
||||
}
|
||||
|
||||
return $tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $tokens
|
||||
* @return mixed[]|null
|
||||
*/
|
||||
private function getPreviousNonSpaceToken(array $tokens, int $start)
|
||||
{
|
||||
for ($i = $start - 1; $i >= 0; --$i) {
|
||||
if ($tokens[$i][0] === T_WHITESPACE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $tokens[$i];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
17
lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php
Normal file
17
lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Lexer\TokenEmulator;
|
||||
|
||||
interface TokenEmulatorInterface
|
||||
{
|
||||
public function getTokenId(): int;
|
||||
|
||||
public function getParserTokenId(): int;
|
||||
|
||||
public function isEmulationNeeded(string $code): bool;
|
||||
|
||||
/**
|
||||
* @return array Modified Tokens
|
||||
*/
|
||||
public function emulate(string $code, array $tokens): array;
|
||||
}
|
@ -6,7 +6,8 @@ use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class NameContext {
|
||||
class NameContext
|
||||
{
|
||||
/** @var null|Name Current namespace */
|
||||
protected $namespace;
|
||||
|
||||
@ -281,4 +282,4 @@ class NameContext {
|
||||
$shortName = substr($name, $nsSep + 1);
|
||||
return strtolower($ns) . '\\' . $shortName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ interface Node
|
||||
*
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function getComments(): array;
|
||||
public function getComments() : array;
|
||||
|
||||
/**
|
||||
* Gets the doc comment of the node.
|
||||
|
@ -22,7 +22,7 @@ class Arg extends NodeAbstract
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->value = $value;
|
||||
$this->byRef = $byRef;
|
||||
$this->unpack = $unpack;
|
||||
@ -31,4 +31,8 @@ class Arg extends NodeAbstract
|
||||
public function getSubNodeNames() : array {
|
||||
return ['value', 'byRef', 'unpack'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Arg';
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ class Const_ extends NodeAbstract
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct($name, Expr $value, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->name = \is_string($name) ? new Identifier($name) : $name;
|
||||
$this->value = $value;
|
||||
}
|
||||
@ -30,4 +30,8 @@ class Const_ extends NodeAbstract
|
||||
public function getSubNodeNames() : array {
|
||||
return ['name', 'value'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Const';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,4 @@ use PhpParser\NodeAbstract;
|
||||
|
||||
abstract class Expr extends NodeAbstract
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ class ArrayDimFetch extends Expr
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $var, Expr $dim = null, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->var = $var;
|
||||
$this->dim = $dim;
|
||||
}
|
||||
@ -27,4 +27,8 @@ class ArrayDimFetch extends Expr
|
||||
public function getSubNodeNames() : array {
|
||||
return ['var', 'dim'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_ArrayDimFetch';
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ class ArrayItem extends Expr
|
||||
public $value;
|
||||
/** @var bool Whether to assign by reference */
|
||||
public $byRef;
|
||||
/** @var bool Whether to unpack the argument */
|
||||
public $unpack;
|
||||
|
||||
/**
|
||||
* Constructs an array item node.
|
||||
@ -21,14 +23,19 @@ class ArrayItem extends Expr
|
||||
* @param bool $byRef Whether to assign by reference
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $value, Expr $key = null, bool $byRef = false, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
public function __construct(Expr $value, Expr $key = null, bool $byRef = false, array $attributes = [], bool $unpack = false) {
|
||||
$this->attributes = $attributes;
|
||||
$this->key = $key;
|
||||
$this->value = $value;
|
||||
$this->byRef = $byRef;
|
||||
$this->unpack = $unpack;
|
||||
}
|
||||
|
||||
public function getSubNodeNames() : array {
|
||||
return ['key', 'value', 'byRef'];
|
||||
return ['key', 'value', 'byRef', 'unpack'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_ArrayItem';
|
||||
}
|
||||
}
|
||||
|
@ -20,11 +20,15 @@ class Array_ extends Expr
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(array $items = [], array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->items = $items;
|
||||
}
|
||||
|
||||
public function getSubNodeNames() : array {
|
||||
return ['items'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_Array';
|
||||
}
|
||||
}
|
||||
|
71
lib/PhpParser/Node/Expr/ArrowFunction.php
Normal file
71
lib/PhpParser/Node/Expr/ArrowFunction.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Node\Expr;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
|
||||
class ArrowFunction extends Expr implements FunctionLike
|
||||
{
|
||||
/** @var bool */
|
||||
public $static;
|
||||
|
||||
/** @var bool */
|
||||
public $byRef;
|
||||
|
||||
/** @var Node\Param[] */
|
||||
public $params = [];
|
||||
|
||||
/** @var null|Node\Identifier|Node\Name|Node\NullableType */
|
||||
public $returnType;
|
||||
|
||||
/** @var Expr */
|
||||
public $expr;
|
||||
|
||||
/**
|
||||
* @param array $subNodes Array of the following optional subnodes:
|
||||
* 'static' => false : Whether the closure is static
|
||||
* 'byRef' => false : Whether to return by reference
|
||||
* 'params' => array() : Parameters
|
||||
* 'returnType' => null : Return type
|
||||
* 'expr' => Expr : Expression body
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(array $subNodes = [], array $attributes = []) {
|
||||
$this->attributes = $attributes;
|
||||
$this->static = $subNodes['static'] ?? false;
|
||||
$this->byRef = $subNodes['byRef'] ?? false;
|
||||
$this->params = $subNodes['params'] ?? [];
|
||||
$returnType = $subNodes['returnType'] ?? null;
|
||||
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
|
||||
$this->expr = $subNodes['expr'] ?? null;
|
||||
}
|
||||
|
||||
public function getSubNodeNames() : array {
|
||||
return ['static', 'byRef', 'params', 'returnType', 'expr'];
|
||||
}
|
||||
|
||||
public function returnsByRef() : bool {
|
||||
return $this->byRef;
|
||||
}
|
||||
|
||||
public function getParams() : array {
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
public function getReturnType() {
|
||||
return $this->returnType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Node\Stmt\Return_[]
|
||||
*/
|
||||
public function getStmts() : array {
|
||||
return [new Node\Stmt\Return_($this->expr)];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_ArrowFunction';
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ class Assign extends Expr
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->var = $var;
|
||||
$this->expr = $expr;
|
||||
}
|
||||
@ -27,4 +27,8 @@ class Assign extends Expr
|
||||
public function getSubNodeNames() : array {
|
||||
return ['var', 'expr'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_Assign';
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ abstract class AssignOp extends Expr
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->var = $var;
|
||||
$this->expr = $expr;
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class BitwiseAnd extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_BitwiseAnd';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class BitwiseOr extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_BitwiseOr';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class BitwiseXor extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_BitwiseXor';
|
||||
}
|
||||
}
|
||||
|
12
lib/PhpParser/Node/Expr/AssignOp/Coalesce.php
Normal file
12
lib/PhpParser/Node/Expr/AssignOp/Coalesce.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Coalesce extends AssignOp
|
||||
{
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Coalesce';
|
||||
}
|
||||
}
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Concat extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Concat';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Div extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Div';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Minus extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Minus';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Mod extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Mod';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Mul extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Mul';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Plus extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Plus';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class Pow extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_Pow';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class ShiftLeft extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_ShiftLeft';
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
|
||||
|
||||
class ShiftRight extends AssignOp
|
||||
{
|
||||
}
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignOp_ShiftRight';
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ class AssignRef extends Expr
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->var = $var;
|
||||
$this->expr = $expr;
|
||||
}
|
||||
@ -27,4 +27,8 @@ class AssignRef extends Expr
|
||||
public function getSubNodeNames() : array {
|
||||
return ['var', 'expr'];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_AssignRef';
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,14 @@ abstract class BinaryOp extends Expr
|
||||
public $right;
|
||||
|
||||
/**
|
||||
* Constructs a bitwise and node.
|
||||
* Constructs a binary operator node.
|
||||
*
|
||||
* @param Expr $left The left hand side expression
|
||||
* @param Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(Expr $left, Expr $right, array $attributes = []) {
|
||||
parent::__construct($attributes);
|
||||
$this->attributes = $attributes;
|
||||
$this->left = $left;
|
||||
$this->right = $right;
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class BitwiseAnd extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '&';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_BitwiseAnd';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class BitwiseOr extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '|';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_BitwiseOr';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class BitwiseXor extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '^';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_BitwiseXor';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class BooleanAnd extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '&&';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_BooleanAnd';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class BooleanOr extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '||';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_BooleanOr';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Coalesce extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '??';
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Coalesce';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Concat extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '.';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Concat';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Div extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '/';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Div';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Equal extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '==';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Equal';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Greater extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '>';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Greater';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class GreaterOrEqual extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '>=';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_GreaterOrEqual';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Identical extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '===';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Identical';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class LogicalAnd extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return 'and';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_LogicalAnd';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class LogicalOr extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return 'or';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_LogicalOr';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class LogicalXor extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return 'xor';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_LogicalXor';
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ class Minus extends BinaryOp
|
||||
public function getOperatorSigil() : string {
|
||||
return '-';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_BinaryOp_Minus';
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user