Compare commits

..

35 Commits

Author SHA1 Message Date
c18bb27723 Release PHP-Parser 4.0 Beta 1 2018-01-27 19:06:36 +01:00
dd0adcc96c Move code gen docs to components and improve
Mention non-fluent helper methods.
2018-01-27 18:56:21 +01:00
e4505de346 Move FAQ into component documentation 2018-01-27 18:40:22 +01:00
a513ccabb7 Improve constant evaluation and add docs
Split into evaluateDirectly() and evaluateSilently(), to be able
to treat errors more gracefully. Add documentation for constant
evaluation.
2018-01-27 17:47:45 +01:00
d817818b5d Move TokenStream into Internal namespace 2018-01-27 13:40:20 +01:00
6a273c9fbd Remove Autoloader class 2018-01-25 23:13:53 +01:00
c2d3ecad35 Merge branch '3.x'
Conflicts:
	CHANGELOG.md
2018-01-25 22:32:25 +01:00
e57b3a0978 Release PHP-Parser 3.1.4 2018-01-25 22:31:33 +01:00
1cdb280a30 Merge branch '3.x' 2018-01-25 22:28:08 +01:00
d01fafcb40 Handle +(++$x) and -(--$x) as well 2018-01-25 22:27:37 +01:00
67df02c844 Update license year 2018-01-25 22:23:06 +01:00
b85b6b3519 Merge branch '3.x'
Conflicts:
	lib/PhpParser/PrettyPrinter/Standard.php
2018-01-25 22:18:32 +01:00
94c715d97e Fix pretty printing of -(-$x) and +(+$x)
Fixes #459.
2018-01-25 22:17:35 +01:00
4dacbb8d39 FPPP: Fix indentation on list insertion
Use indentation of last list element, instead of indentation
before the insertion point.

Fixes #466.
2018-01-25 22:08:40 +01:00
aa685e711a Bump to PHP 7.2 in documentation 2018-01-14 22:10:37 +01:00
edafeb85c4 [CS] Order uses A -> Z 2018-01-13 16:08:27 +01:00
68d07c4662 [CS] New line in the end of file 2018-01-13 16:08:26 +01:00
8fae99aafe [CS] Remove spaces 2018-01-13 16:08:25 +01:00
c7ada124d0 [CS] Use ::class notation instead of string
Conflicts:
	test/PhpParser/ParserFactoryTest.php
	test/PhpParser/ParserTest.php
2018-01-13 16:08:17 +01:00
f6617e6d25 [CS] Space after ternary operator 2018-01-13 16:04:00 +01:00
2499534729 [CS] Whitespace before return type 2018-01-13 16:03:59 +01:00
e0a2043089 [CS] Space before casting 2018-01-13 16:03:57 +01:00
e6e52abae7 [CS] Trim whitespaces 2018-01-13 16:03:56 +01:00
7f72c84122 [CS] Open class brackets in new line 2018-01-13 16:03:55 +01:00
fc8ac71e76 [CS] Trim whitespaces inside arrays 2018-01-13 16:03:54 +01:00
a8968caa5b [CS] Remove extra lines 2018-01-13 16:03:53 +01:00
5285df8f22 [CS] Use elseif instead of else if
Conflicts:
	lib/PhpParser/TokenStream.php
2018-01-13 16:03:36 +01:00
4366aa2fb0 [CS] Use __DIR__ instead of dirname(__FILE__) 2018-01-13 16:02:14 +01:00
bf7d811cda Add methods visibility (#464)
* [PSR-2] Visibility before static

* [PSR-2] Declare method Visibility
2018-01-10 19:07:41 +01:00
248b29ecf6 Add public visibility to getType method (#463) 2018-01-10 18:57:48 +01:00
bcb45d31eb Trailing whitespaces (#461)
Signed-off-by: Gabriel Caruso <carusogabriel34@gmail.com>
2018-01-08 11:10:03 +01:00
3bc31488ce Combine issets (#460) 2018-01-04 13:36:01 +01:00
92b39e3d1f Fix nNextFreeElement for arrays with trailing comma
Ugh.
2017-12-26 21:17:36 +01:00
1c7fd314d1 FPPP: Add heuristic for multi-line lists 2017-12-26 21:14:36 +01:00
fb8175567e Simplify delayed add code
$insertStr always stays the same, so no reason to store it
separately.
2017-12-26 17:53:36 +01:00
236 changed files with 806 additions and 479 deletions

View File

@ -3,15 +3,31 @@ Version 4.0.0-dev
Nothing yet.
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.
* 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
@ -92,11 +108,18 @@ 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.4-dev
Version 3.1.5-dev
-----------------
Nothing yet.
Version 3.1.4 (2018-01-25)
--------------------------
### Fixed
* Fixed pretty printing of `-(-$x)` and `+(+$x)`. (#459)
Version 3.1.3 (2017-12-26)
--------------------------
@ -134,7 +157,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
@ -254,7 +277,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.

View File

@ -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.

View File

@ -8,7 +8,7 @@ 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] (development; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.2).
[Documentation for version 4.x][doc_master] (beta; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.2).
Features
--------
@ -184,8 +184,6 @@ 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:
@ -196,6 +194,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](doc/component/Lexer.markdown)
* Lexer options
* Token and file positions for nodes
@ -203,10 +203,15 @@ Component documentation:
* [Error handling](doc/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
* [Performance](doc/component/Performance.markdown)
* Disabling XDebug
* Reusing objects
* Garbage collection impact
* [Frequently asked questions](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

View File

@ -52,7 +52,7 @@ Because HHVM does not support PHP 7, HHVM is no longer supported.
* 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
@ -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.

View File

@ -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.2 parser **written in PHP itself**.
What is this for?
-----------------
@ -26,11 +26,11 @@ 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.2.
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.2 source code running on PHP 5.5, for example. This emulation is somewhat
hacky and not perfect, but it should work well on any sane code.
What output does it produce?

View File

@ -7,12 +7,10 @@ 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
-----------------------
* [Name resolution](component/Name_resolution.markdown)
* Name resolver options
* Name resolution context
@ -20,6 +18,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 +27,12 @@ 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
* [Performance](component/Performance.markdown)
* Disabling XDebug
* Reusing objects
* Garbage collection impact
* [Frequently asked questions](component/FAQ.markdown)
* Parent and sibling references

View File

@ -1,9 +1,17 @@
Code generation
===============
AST builders
============
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
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
@ -82,3 +90,17 @@ abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
}
}
```
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]`.
* `args(array $args)`: Creates an array of function/method arguments, including the required `Arg`
wrappers. Also converts literals to AST nodes.
* `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.

View 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;
}
```

View File

@ -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
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
`replaceNames` 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

View File

@ -937,7 +937,7 @@ list_expr_element:
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($$); }
;
inner_array_pair_list:

View File

@ -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;
}
}
}
}

View File

@ -10,4 +10,4 @@ interface Builder
* @return Node The built node
*/
public function getNode() : Node;
}
}

View File

@ -119,4 +119,4 @@ class Class_ extends Declaration
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
], $this->attributes);
}
}
}

View File

@ -40,4 +40,4 @@ abstract class Declaration implements PhpParser\Builder
return $this;
}
}
}

View File

@ -50,7 +50,7 @@ class Interface_ extends Declaration
if ($stmt instanceof Stmt\ClassConst) {
$this->constants[] = $stmt;
} else if ($stmt instanceof Stmt\ClassMethod) {
} elseif ($stmt instanceof Stmt\ClassMethod) {
// we erase all statements in the body of an interface method
$stmt->stmts = null;
$this->methods[] = $stmt;
@ -72,4 +72,4 @@ class Interface_ extends Declaration
'stmts' => array_merge($this->constants, $this->methods),
], $this->attributes);
}
}
}

View File

@ -109,4 +109,4 @@ class Property implements PhpParser\Builder
$this->attributes
);
}
}
}

View File

@ -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()));

View File

@ -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;

View File

@ -14,7 +14,8 @@ use PhpParser\Node\Stmt;
*
* @internal
*/
final class BuilderHelpers {
final class BuilderHelpers
{
/**
* Normalizes a node: Converts builder objects to nodes.
*
@ -184,7 +185,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');

View File

@ -164,4 +164,4 @@ class Comment implements \JsonSerializable
'tokenPos' => $this->tokenPos,
];
}
}
}

View File

@ -4,4 +4,4 @@ namespace PhpParser\Comment;
class Doc extends \PhpParser\Comment
{
}
}

View File

@ -2,4 +2,5 @@
namespace PhpParser;
class ConstExprEvaluationException extends \Exception {}
class ConstExprEvaluationException extends \Exception
{}

View File

@ -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);
}
}
}

View File

@ -51,7 +51,6 @@ class Error extends \RuntimeException
return $this->attributes['endLine'] ?? -1;
}
/**
* Gets the attributes of the node/token the error occurred at.
*
@ -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']);
}
/**

View File

@ -10,4 +10,4 @@ interface ErrorHandler
* @param Error $error The error that needs to be handled
*/
public function handleError(Error $error);
}
}

View File

@ -43,4 +43,4 @@ class Collecting implements ErrorHandler
public function clearErrors() {
$this->errors = [];
}
}
}

View File

@ -15,4 +15,4 @@ class Throwing implements ErrorHandler
public function handleError(Error $error) {
throw $error;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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 */
@ -45,11 +46,11 @@ class PrintableNewAnonClassNode extends Expr {
);
}
public function getType(): string {
public function getType() : string {
return 'Expr_PrintableNewAnonClass';
}
public function getSubNodeNames() : array {
return ['args', 'extends', 'implements', 'stmts'];
}
}
}

View File

@ -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;
}
}
}

View File

@ -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\"");
}
}
}

View File

@ -153,7 +153,7 @@ class Lexer
$nextFilePos = strpos($this->code, $tokenValue, $filePos);
$this->handleInvalidCharacterRange(
$filePos, $nextFilePos, $line, $errorHandler);
$filePos = (int)$nextFilePos;
$filePos = (int) $nextFilePos;
}
$filePos += $tokenLen;
@ -255,7 +255,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;
}
@ -357,7 +357,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 = Tokens::class . '::' . $name)) {
} elseif (defined($name = Tokens::class . '::' . $name)) {
// Other tokens can be mapped directly
$tokenMap[$i] = constant($name);
}

View File

@ -2,8 +2,7 @@
namespace PhpParser\Lexer;
class Emulative extends \PhpParser\Lexer
{
/* No features requiring emulation have been added in PHP > 7.0 */
}
}

View File

@ -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;
}
}
}

View File

@ -90,7 +90,7 @@ interface Node
*
* @return Comment[]
*/
public function getComments(): array;
public function getComments() : array;
/**
* Gets the doc comment of the node.

View File

@ -32,7 +32,7 @@ class Arg extends NodeAbstract
return ['value', 'byRef', 'unpack'];
}
function getType() : string {
public function getType() : string {
return 'Arg';
}
}

View File

@ -31,7 +31,7 @@ class Const_ extends NodeAbstract
return ['name', 'value'];
}
function getType() : string {
public function getType() : string {
return 'Const';
}
}

View File

@ -6,4 +6,4 @@ use PhpParser\NodeAbstract;
abstract class Expr extends NodeAbstract
{
}
}

View File

@ -28,7 +28,7 @@ class ArrayDimFetch extends Expr
return ['var', 'dim'];
}
function getType() : string {
public function getType() : string {
return 'Expr_ArrayDimFetch';
}
}

View File

@ -32,7 +32,7 @@ class ArrayItem extends Expr
return ['key', 'value', 'byRef'];
}
function getType() : string {
public function getType() : string {
return 'Expr_ArrayItem';
}
}

View File

@ -28,7 +28,7 @@ class Array_ extends Expr
return ['items'];
}
function getType() : string {
public function getType() : string {
return 'Expr_Array';
}
}

View File

@ -28,7 +28,7 @@ class Assign extends Expr
return ['var', 'expr'];
}
function getType() : string {
public function getType() : string {
return 'Expr_Assign';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class BitwiseAnd extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_BitwiseAnd';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class BitwiseOr extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_BitwiseOr';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class BitwiseXor extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_BitwiseXor';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Concat extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Concat';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Div extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Div';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Minus extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Minus';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Mod extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Mod';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Mul extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Mul';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Plus extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Plus';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class Pow extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_Pow';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class ShiftLeft extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_ShiftLeft';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\AssignOp;
class ShiftRight extends AssignOp
{
function getType() : string {
public function getType() : string {
return 'Expr_AssignOp_ShiftRight';
}
}
}

View File

@ -28,7 +28,7 @@ class AssignRef extends Expr
return ['var', 'expr'];
}
function getType() : string {
public function getType() : string {
return 'Expr_AssignRef';
}
}

View File

@ -10,7 +10,7 @@ class BitwiseAnd extends BinaryOp
return '&';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_BitwiseAnd';
}
}
}

View File

@ -10,7 +10,7 @@ class BitwiseOr extends BinaryOp
return '|';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_BitwiseOr';
}
}
}

View File

@ -10,7 +10,7 @@ class BitwiseXor extends BinaryOp
return '^';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_BitwiseXor';
}
}
}

View File

@ -10,7 +10,7 @@ class BooleanAnd extends BinaryOp
return '&&';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_BooleanAnd';
}
}
}

View File

@ -10,7 +10,7 @@ class BooleanOr extends BinaryOp
return '||';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_BooleanOr';
}
}
}

View File

@ -10,7 +10,7 @@ class Coalesce extends BinaryOp
return '??';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Coalesce';
}
}

View File

@ -10,7 +10,7 @@ class Concat extends BinaryOp
return '.';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Concat';
}
}
}

View File

@ -10,7 +10,7 @@ class Div extends BinaryOp
return '/';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Div';
}
}
}

View File

@ -10,7 +10,7 @@ class Equal extends BinaryOp
return '==';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Equal';
}
}
}

View File

@ -10,7 +10,7 @@ class Greater extends BinaryOp
return '>';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Greater';
}
}
}

View File

@ -10,7 +10,7 @@ class GreaterOrEqual extends BinaryOp
return '>=';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_GreaterOrEqual';
}
}
}

View File

@ -10,7 +10,7 @@ class Identical extends BinaryOp
return '===';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Identical';
}
}
}

View File

@ -10,7 +10,7 @@ class LogicalAnd extends BinaryOp
return 'and';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_LogicalAnd';
}
}
}

View File

@ -10,7 +10,7 @@ class LogicalOr extends BinaryOp
return 'or';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_LogicalOr';
}
}
}

View File

@ -10,7 +10,7 @@ class LogicalXor extends BinaryOp
return 'xor';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_LogicalXor';
}
}
}

View File

@ -10,7 +10,7 @@ class Minus extends BinaryOp
return '-';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Minus';
}
}
}

View File

@ -10,7 +10,7 @@ class Mod extends BinaryOp
return '%';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Mod';
}
}
}

View File

@ -10,7 +10,7 @@ class Mul extends BinaryOp
return '*';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Mul';
}
}
}

View File

@ -10,7 +10,7 @@ class NotEqual extends BinaryOp
return '!=';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_NotEqual';
}
}
}

View File

@ -10,7 +10,7 @@ class NotIdentical extends BinaryOp
return '!==';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_NotIdentical';
}
}
}

View File

@ -10,7 +10,7 @@ class Plus extends BinaryOp
return '+';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Plus';
}
}
}

View File

@ -10,7 +10,7 @@ class Pow extends BinaryOp
return '**';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Pow';
}
}
}

View File

@ -10,7 +10,7 @@ class ShiftLeft extends BinaryOp
return '<<';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_ShiftLeft';
}
}
}

View File

@ -10,7 +10,7 @@ class ShiftRight extends BinaryOp
return '>>';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_ShiftRight';
}
}
}

View File

@ -10,7 +10,7 @@ class Smaller extends BinaryOp
return '<';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Smaller';
}
}
}

View File

@ -10,7 +10,7 @@ class SmallerOrEqual extends BinaryOp
return '<=';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_SmallerOrEqual';
}
}
}

View File

@ -10,7 +10,7 @@ class Spaceship extends BinaryOp
return '<=>';
}
function getType() : string {
public function getType() : string {
return 'Expr_BinaryOp_Spaceship';
}
}

View File

@ -24,7 +24,7 @@ class BitwiseNot extends Expr
return ['expr'];
}
function getType() : string {
public function getType() : string {
return 'Expr_BitwiseNot';
}
}

View File

@ -24,7 +24,7 @@ class BooleanNot extends Expr
return ['expr'];
}
function getType() : string {
public function getType() : string {
return 'Expr_BooleanNot';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class Array_ extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_Array';
}
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class Bool_ extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_Bool';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class Double extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_Double';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class Int_ extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_Int';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class Object_ extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_Object';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class String_ extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_String';
}
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node\Expr\Cast;
class Unset_ extends Cast
{
function getType() : string {
public function getType() : string {
return 'Expr_Cast_Unset';
}
}
}

View File

@ -30,7 +30,7 @@ class ClassConstFetch extends Expr
return ['class', 'name'];
}
function getType() : string {
public function getType() : string {
return 'Expr_ClassConstFetch';
}
}

View File

@ -24,7 +24,7 @@ class Clone_ extends Expr
return ['expr'];
}
function getType() : string {
public function getType() : string {
return 'Expr_Clone';
}
}

View File

@ -65,7 +65,7 @@ class Closure extends Expr implements FunctionLike
return $this->stmts;
}
function getType() : string {
public function getType() : string {
return 'Expr_Closure';
}
}

View File

@ -28,7 +28,7 @@ class ClosureUse extends Expr
return ['var', 'byRef'];
}
function getType() : string {
public function getType() : string {
return 'Expr_ClosureUse';
}
}

View File

@ -25,7 +25,7 @@ class ConstFetch extends Expr
return ['name'];
}
function getType() : string {
public function getType() : string {
return 'Expr_ConstFetch';
}
}

View File

@ -24,7 +24,7 @@ class Empty_ extends Expr
return ['expr'];
}
function getType() : string {
public function getType() : string {
return 'Expr_Empty';
}
}

View File

@ -25,7 +25,7 @@ class Error extends Expr
return [];
}
function getType() : string {
public function getType() : string {
return 'Expr_Error';
}
}

Some files were not shown because too many files have changed in this diff Show More