nikic/php-parser
Parse PHP code into an Abstract Syntax Tree (AST) for static analysis, manipulation, and code generation. Supports PHP 5.x to 8.4, handles errors gracefully, and preserves formatting during AST-to-code conversion. Easily traverse, modify, and convert ASTs back to PHP, with JSON serialization support...
shouldPrintRawValue attribute to Scalar\Int_, which makes the pretty printer use the rawValue of the node. This can be used to print integers with separators.chr() deprecation warning on PHP 8.5.Param::isFinal() method.Param::isPublic() for parameters with asymmetric visibility keyword.SplObjectStorage methods.kind attributes to Cast\Int_, Cast\Bool_ and Cast\String_. These allow distinguishing the deprecated versions of these casts.clone with arbitrary function arguments. This will be parsed as an Expr\FuncCall node, instead of the usual Expr\Clone_ node.function clone for use in stubs.Expr\BinaryOp\Pipe.(void) cast, represented by Expr\Cast\Void_.final modifier on promoted properties.CallLike::getArg() to fetch an argument by position and name.Stmt\Const_ now has an attrGroups subnode.weakReferences option to NodeConnectingVisitor and ParentConnectingVisitor. This will create the parent/next/prev references as WeakReferences, to avoid making the AST cyclic and thus increasing GC pressure.Property::isAbstract() and Property::isFinal() methods.PropertyHook::isFinal() method.Param::isPromoted() and Param::isPublic() now returns true for parameters that have property hooks but no explicit visibility modifier.PropertyHook::getStmts() now correctly desugars short set hooks. set => $value will be expanded to set { $this->propertyName = $value; }. This requires the propertyName attribute on the hook to be set, which is now also set by the parser. If the attribute is not set, getStmts() will throw an error for short set hooks, as it is not possible to produce a correct desugaring.exit or die, to allow their use in stubs.Parser\Php8 class from PHP-Parser 5.x that was mistakenly added. It could cause errors when using something like composer preload.Stmt\GroupUse nodes, making them consistent with Stmt\Use_ nodes.ValueErrors.indent option to pretty printer, which can be used to specify the indentation to use (defaulting to four spaces). This also allows using tab indentation.PropertyHooks in the NameResolver.Stmt\GroupUse nodes, making them consistent with Stmt\Use_ nodes.ValueErrors.BuilderFactory::val().__PROPERTY__ magic constant, represented using a Node\Scalar\MagicConst\Property node.hooks subnode on Node\Stmt\Property and Node\Param, which contains an array of Node\PropertyHook.flags can now hold the additional bits Modifiers::PUBLIC_SET, Modifiers::PROTECTED_SET and Modifiers::PRIVATE_SET.Node\Expr\Exit_ node. Otherwise (e.g. if a named argument is used) it will be represented as a plain Node\Expr\FuncCall.BuilderFactory::val().$array{0} from the PHP 8 parser. It is still supported by the PHP 7 parser. This is necessary in order to support property hooks.new expressions without parentheses.Parser objects. This means that no longer used parser objects are immediately destroyed now, instead of requiring cycle GC.PhpVersion::getNewestSupported() to report PHP 8.3 instead of PHP 8.2.T_* compatibility tokens with incorrect type (such as string instead of int). This would lead to TypeErrors down the line. Now an Error will be thrown early to indicate the problem.See the upgrading guide for detailed migration instructions. The changelog is relative to PHP-Parser 4.18.0.
PhpVersion class, which is accepted in a number of places (e.g. ParserFactory, Parser, Lexer, PrettyPrinter) and gives more precise control over the PHP version being targeted.Parser::getTokens() method.Modifiers class, as a replacement for Stmt\Class_::MODIFIER_*.REMOVE_NODE from NodeVisitor::enterNode().isPromoted(), isPublic(), isProtected(), isPrivate() and isReadonly() methods on Param.PrettyPrinter interface.php-parse binary now accepts - as the file name, in which case it will read from stdin.NodeVisitor::REPLACE_WITH_NULL.newline option.NodeTraverser constructor. A separate call to addVisitor() is no longer required.kind) in NodeDumper.rawValue attribute to InterpolatedStringPart and heredoc/nowdoc String_s, which provides the original, unparsed value. It was previously only available for non-interpolated single/double quoted strings.Stmt\Block to represent {} code blocks. Previously, such code blocks were flattened into the parent statements array. Stmt\Block will not be created for structures that are typically used with code blocks, for example if ($x) { $y; } will be represented as previously, while if ($x) { { $x; } } will have an extra Stmt\Block wrapper.PhpParser\Token class, which is compatible with PHP 8 token representation (PhpToken).Expr\List_ nodes, even if it uses [] syntax.Expr/Stmt hierarchy. Compatibility shims for the old names have been retained.yield in parentheses, unless the target version is set to older than PHP 7.0.else if { } instead of else { if { } }.leaveNode() method on visitors is now invoked in reverse order of enterNode().NodeTraverser::REMOVE_NODE etc. to NodeVisitor::REMOVE_NODE. The old constants are still available for compatibility.Name subnode parts has been replaced by name, which stores the name as a string rather than an array of parts separated by namespace separators. The getParts() method returns the old representation.Identifier, Name or ComplexType must be passed.Comment::getReformattedText() now normalizes CRLF newlines to LF newlines.Lexer no longer accepts options. Lexer\Emulative only accepts a PhpVersion. The startLexing(), getTokens() and handleHaltCompiler() methods have been removed. Instead, there is a single method tokenize() returning the tokens.Error constructor taking a line number instead of an attributes array.Comment::getLine(), Comment::getTokenPos() and Comment::getFilePos() methods have been removed. Use Comment::getStartLine(), Comment::getStartTokenPos() and Comment::getStartFilePos() instead.Stmt\Throw_ node, use Expr\Throw_ inside Stmt\Expression instead.ParserFactory::create().clone, throw and arrow functions.\r at the end of a doc string could be incorrectly merged into a CRLF sequence with a following \n.__halt_compiler is no longer recognized as a semi-reserved keyword, in line with PHP behavior.<?= is no longer recognized as a semi-reserved keyword.\u escape sequences.Differ.Node::getLine() method has been deprecated. Use Node::getStartLine() instead.See UPGRADE-5.0 for detailed migration instructions. The changelog is relative to beta 1.
kind) in NodeDumper.rawValue attribute to InterpolatedStringPart and heredoc/nowdoc String_s, which provides the original, unparsed value. It was previously only available for non-interpolated single/double quoted strings.Stmt\Block to represent {} code blocks. Previously, such code blocks were flattened into the parent statements array. Stmt\Block will not be created for structures that are typically used with code blocks, for example if ($x) { $y; } will be represented as previously, while if ($x) { { $y; } } will have an extra Stmt\Block wrapper.Stmt\Throw_ node, use Expr\Throw_ inside Stmt\Expression instead.ParserFactory::create().ParserFactory::createForNewestSupportedVersion() and ParserFactory::createForHostVersion() for forward-compatibility with PHP-Parser 5.0.grammar/ directory has been excluded from exported git archives.See UPGRADE-5.0 for detailed migration instructions. The changelog is relative to alpha 3.
NodeTraverser constructor. A separate call to addVisitor() is no longer required.Lexer no longer accepts options. Lexer\Emulative only accepts a PhpVersion. The startLexing(), getTokens() and handleHaltCompiler() methods have been removed. Instead, there is a single method tokenize() returning the tokens.Parser::getLexer() method has been replaced by Parser::getTokens().Comment::getLine(), Comment::getTokenPos() and Comment::getFilePos() methods have been removed. Use Comment::getStartLine(), Comment::getStartTokenPos() and Comment::getStartFilePos() instead.Node::getLine() method has been deprecated. Use Node::getStartLine() instead.ClassConst::$type introduced in previous release.(CONST)::$x and similar.Name::getParts() method for forward-compatibility with PHP-Parser 5.Name::$parts, which will be removed in PHP-Parser 5.See UPGRADE-5.0 for detailed migration instructions.
NodeVisitor::REPLACE_WITH_NULL.newline option.else if { } instead of else { if { } }.leaveNode() method on visitors is now invoked in reverse order of enterNode().NodeTraverser::REMOVE_NODE etc. to NodeVisitor::REMOVE_NODE. The old constants are still available for compatibility.Name subnode parts has been replaced by name, which stores the name as a string rather than an array of parts separated by namespace separators. The getParts() method returns the old representation.Identifier, Name or ComplexType must be passed.Comment::getReformattedText() now normalizes CRLF newlines to LF newlines.Differ.makePrivate(), makeProtected(), makePublic() and makeReadonly() methods to Builder\Param to allow the creation of promoted parameters.See UPGRADE-5.0 for detailed migration instructions.
isPromoted(), isPublic(), isProtected(), isPrivate() and isReadonly() methods on Param.PrettyPrinter interface.php-parse binary now accepts - as the file name, in which case it will read from stdin.clone, throw and arrow functions.yield in parentheses, unless the target version is set to older than PHP 7.0.-> and :: are now taken into account.\r at the end of a doc string could be incorrectly merged into a CRLF sequence with a following \n.__halt_compiler is no longer recognized as a semi-reserved keyword, in line with PHP behavior.<?= is no longer recognized as a semi-reserved keyword.\u escape sequences.Error constructor taking a line number instead of an attributes array.How can I help you explore Laravel packages today?