Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Parser Laravel Package

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

View on GitHub
Deep Wiki
Context7
v5.7.0

Fixed

  • Fixed changing modifier on anonymous class with formatting preserving pretty printer.
  • Emit an error for unparenthesized arrow functions in pipe operator, and print necessary parentheses in the pretty printer.
  • Fix PHP 8.5 deprecation warning in php-parse binary.

Changed

  • When targeting PHP 8.4 or newer, omit parentheses around immediately dereferenced new expressions.

Added

  • Added 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.
v4.19.5

Fixed

  • Fixed deprecation warnings on PHP 8.5.
v5.6.2

Fixed

  • Fixed formatting-preserving pretty-printing when changing the visibility modifier on a node that has attributes.
  • Fixed chr() deprecation warning on PHP 8.5.

Added

  • Added Param::isFinal() method.
v5.6.1

Fixed

  • Fixed Param::isPublic() for parameters with asymmetric visibility keyword.
  • Fixed PHP 8.5 deprecation warnings for SplObjectStorage methods.

Added

  • Added cast kind attributes to Cast\Int_, Cast\Bool_ and Cast\String_. These allow distinguishing the deprecated versions of these casts.
v5.6.0

Added

  • [8.5] Added support for clone with arbitrary function arguments. This will be parsed as an Expr\FuncCall node, instead of the usual Expr\Clone_ node.
  • [8.5] Permit declaration of function clone for use in stubs.
  • [8.5] Added support for the pipe operator, represented by Expr\BinaryOp\Pipe.
  • [8.5] Added support for the (void) cast, represented by Expr\Cast\Void_.
  • [8.5] Added support for the final modifier on promoted properties.
  • Added CallLike::getArg() to fetch an argument by position and name.
v5.5.0

Added

  • [8.5] Added support for attributes on constants. Stmt\Const_ now has an attrGroups subnode.
  • Added 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.

Changed

  • Attributes on parameters are now printed on separate lines if the pretty printer target version is PHP 7.4 or older (which is the default). This allows them to be interpreted as comments, instead of causing a parse error. Specify a target version of PHP 8.0 or newer to restore the previous behavior.
v5.4.0

Added

  • Added Property::isAbstract() and Property::isFinal() methods.
  • Added PropertyHook::isFinal() method.
  • Emit an error if property hook is used on declaration with multiple properties.

Fixed

  • Make legacy class aliases compatible with classmap-authoritative autoloader.
  • 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.
v5.3.1

Added

  • Added support for declaring functions with name exit or die, to allow their use in stubs.
v4.19.4

Fixed

  • Remove the Parser\Php8 class from PHP-Parser 5.x that was mistakenly added. It could cause errors when using something like composer preload.
v4.19.3

Fixed

  • Include the trailing semicolon inside Stmt\GroupUse nodes, making them consistent with Stmt\Use_ nodes.
  • Fixed indentation sometimes becoming negative in formatting-preserving pretty printer, resulting in ValueErrors.
v5.3.0

Added

  • Added 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.

Fixed

  • Resolve names in PropertyHooks in the NameResolver.
  • Include the trailing semicolon inside Stmt\GroupUse nodes, making them consistent with Stmt\Use_ nodes.
  • Fixed indentation sometimes becoming negative in formatting-preserving pretty printer, resulting in ValueErrors.
v4.19.2

Added

  • Added support for passing enum values to various builder methods, like BuilderFactory::val().
v5.2.0

Added

  • [8.4] Added support for __PROPERTY__ magic constant, represented using a Node\Scalar\MagicConst\Property node.
  • [8.4] Added support for property hooks, which are represented using a new hooks subnode on Node\Stmt\Property and Node\Param, which contains an array of Node\PropertyHook.
  • [8.4] Added support for asymmetric visibility modifiers. Property flags can now hold the additional bits Modifiers::PUBLIC_SET, Modifiers::PROTECTED_SET and Modifiers::PRIVATE_SET.
  • [8.4] Added support for generalized exit function. For backwards compatibility, exit without argument or a single plain argument continues to use a Node\Expr\Exit_ node. Otherwise (e.g. if a named argument is used) it will be represented as a plain Node\Expr\FuncCall.
  • Added support for passing enum values to various builder methods, like BuilderFactory::val().

Removed

  • Removed support for alternative array syntax $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.
v5.1.0

Added

  • [8.4] Added support for dereferencing new expressions without parentheses.

Fixed

  • Fixed redundant parentheses being added when pretty printing ternary expressions.

Changed

  • Made some phpdoc types more precise.
v4.19.1

Fixed

  • Fixed "Optional parameter before required parameter" deprecation warning introduced in previous version.
v4.19.0

Changed

  • Do not use implicitly nullable parameters, which are deprecated in PHP 8.4.
  • Remove support for running on PHP 7.0, which does not support explicitly nullable parameters.
v5.0.2

Fixed

  • Fix handling of indentation on next line after opening PHP tag in formatting-preserving pretty printer.

Changed

  • Avoid cyclic references in Parser objects. This means that no longer used parser objects are immediately destroyed now, instead of requiring cycle GC.
  • Update PhpVersion::getNewestSupported() to report PHP 8.3 instead of PHP 8.2.
v5.0.1

Changed

  • Added check to detect use of PHP-Parser with libraries that define 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.
v5.0.0

See the upgrading guide for detailed migration instructions. The changelog is relative to PHP-Parser 4.18.0.

Added

  • Added 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.
  • Added PHP 8 parser though it only differs from the PHP 7 parser in concatenation precedence.
  • Added Parser::getTokens() method.
  • Added a Modifiers class, as a replacement for Stmt\Class_::MODIFIER_*.
  • Added support for returning an array or REMOVE_NODE from NodeVisitor::enterNode().
  • Added many additional type annotations. PhpStan is now used.
  • Added a fuzzing target for PHP-Fuzzer, which was how a lot of pretty printer bugs were found.
  • Added isPromoted(), isPublic(), isProtected(), isPrivate() and isReadonly() methods on Param.
  • Added support for class constants in trait builder.
  • Added PrettyPrinter interface.
  • Added support for formatting preservation when toggling static modifiers.
  • The php-parse binary now accepts - as the file name, in which case it will read from stdin.
  • Added support for NodeVisitor::REPLACE_WITH_NULL.
  • Added support for CRLF newlines in the pretty printer, using the new newline option.
  • Visitors can now be passed directly to the NodeTraverser constructor. A separate call to addVisitor() is no longer required.
  • Added support for printing additional attributes (like kind) in NodeDumper.
  • Added 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.
  • Added 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.

Changed

  • PHP 7.4 is now required to run PHP-Parser.
  • Property types have been added where possible.
  • Formatting of the standard pretty printer has been adjusted to match PSR-12 more closely.
  • The internal token representation now uses a PhpParser\Token class, which is compatible with PHP 8 token representation (PhpToken).
  • Array destructuring is now always represented using Expr\List_ nodes, even if it uses [] syntax.
  • Renamed a number of node classes, and moved things that were not real expressions/statements outside the Expr/Stmt hierarchy. Compatibility shims for the old names have been retained.
  • The pretty printer no longer unconditionally wraps yield in parentheses, unless the target version is set to older than PHP 7.0.
  • The pretty printer now defaults to PHP 7.4 as the target version.
  • Print else if { } instead of else { if { } }.
  • The leaveNode() method on visitors is now invoked in reverse order of enterNode().
  • Moved NodeTraverser::REMOVE_NODE etc. to NodeVisitor::REMOVE_NODE. The old constants are still available for compatibility.
  • The 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.
  • No longer accept strings for types in Node constructors. Instead, either an Identifier, Name or ComplexType must be passed.
  • Comment::getReformattedText() now normalizes CRLF newlines to LF newlines.
  • The 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.
  • Attribute handling has been moved from the lexer to the parser, and is no longer configurable. The comments, startLine, endLine, startTokenPos, endTokenPos, startFilePos, and endFilePos attributes will always be added.
  • The pretty printer now indents heredoc/nowdoc strings if the target version is >= 7.3 (flexible heredoc/nowdoc).
  • Use visitor to assign comments. This fixes the long-standing issue where comments were assigned to all nodes sharing a starting position. Now only the outer-most node will hold the comments.
  • Improve NodeDumper performance for large dumps.

Removed

  • The PHP 5 parser has been removed. The PHP 7 parser has been adjusted to deal with PHP 5 code more gracefully.
  • Removed deprecated Error constructor taking a line number instead of an attributes array.
  • The deprecated Comment::getLine(), Comment::getTokenPos() and Comment::getFilePos() methods have been removed. Use Comment::getStartLine(), Comment::getStartTokenPos() and Comment::getStartFilePos() instead.
  • Removed Stmt\Throw_ node, use Expr\Throw_ inside Stmt\Expression instead.
  • Removed ParserFactory::create().

Fixed

  • The pretty printer now uses a more accurate treatment of unary operator precedence, and will only wrap them in parentheses if required. This allowed fixing a number of other precedence related bugs.
  • The pretty printer now respects the precedence of clone, throw and arrow functions.
  • Fixed formatting preservation for alternative elseif/else syntax.
  • Fixed checks for when it is safe to print strings as heredoc/nowdoc to accommodate flexible doc string semantics.
  • Fixed various cases where \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.
  • Fix handling of very large overflowing \u escape sequences.
  • Don't trim leading whitespace in formatting preserving printer.
  • Treat DEL as a label character in the formatting preserving printer depending on the targeted PHP version.
  • Fix error reporting in emulative lexer without explicitly specified error handler.
  • Gracefully handle non-contiguous array indices in the Differ.

Deprecated

  • The Node::getLine() method has been deprecated. Use Node::getStartLine() instead.
v5.0.0rc1

See UPGRADE-5.0 for detailed migration instructions. The changelog is relative to beta 1.

Fixed

  • Fixed parsing of empty files.

Added

  • Added support for printing additional attributes (like kind) in NodeDumper.
  • Added 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.
  • Added 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.

Changed

  • Use visitor to assign comments. This fixes the long-standing issue where comments were assigned to all nodes sharing a starting position. Now only the outer-most node will hold the comments.
  • Don't parse unicode escape sequences when targeting PHP < 7.0.
  • Improve NodeDumper performance for large dumps.

Removed

  • Removed Stmt\Throw_ node, use Expr\Throw_ inside Stmt\Expression instead.
  • Removed ParserFactory::create().
v4.18.0

Added

  • Added methods ParserFactory::createForNewestSupportedVersion() and ParserFactory::createForHostVersion() for forward-compatibility with PHP-Parser 5.0.

Fixed

  • Fixed missing name resolution of class constant types.
  • Fixed class members being dropped if an error is encountered while parsing a later class member (when error recovery is enabled).

Changed

  • The grammar/ directory has been excluded from exported git archives.
v5.0.0beta1

See UPGRADE-5.0 for detailed migration instructions. The changelog is relative to alpha 3.

Added

  • Visitors can now be passed directly to the NodeTraverser constructor. A separate call to addVisitor() is no longer required.

Changed

  • The minimum host PHP version is now PHP 7.4. It is still possible to parse code from older versions. Property types have been added where possible.
  • The 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.
  • The Parser::getLexer() method has been replaced by Parser::getTokens().
  • Attribute handling has been moved from the lexer to the parser, and is no longer configurable. The comments, startLine, endLine, startTokenPos, endTokenPos, startFilePos, and endFilePos attributes will always be added.
  • The pretty printer now defaults to PHP 7.4 as the target version.
  • The pretty printer now indents heredoc/nowdoc strings if the target version is >= 7.3 (flexible heredoc/nowdoc).

Removed

  • The deprecated Comment::getLine(), Comment::getTokenPos() and Comment::getFilePos() methods have been removed. Use Comment::getStartLine(), Comment::getStartTokenPos() and Comment::getStartFilePos() instead.

Deprecated

  • The Node::getLine() method has been deprecated. Use Node::getStartLine() instead.
v4.17.1

Fixed

  • Fixed phpdoc mismatches for ClassConst::$type introduced in previous release.
v4.17.0

Added

  • [PHP 8.3] Added support for typed class constants.
  • [PHP 8.3] Added supprot for dynamic class constant fetch.
  • [PHP 8.3] Added support for readonly anonymous classes.

Fixed

  • Fixed missing required parentheses when pretty printing new with an expression class name.
  • Fixed missing required parentheses when pretty printing (CONST)::$x and similar.
v4.16.0

Added

  • Added Name::getParts() method for forward-compatibility with PHP-Parser 5.

Deprecated

  • Deprecated direct access to Name::$parts, which will be removed in PHP-Parser 5.
v5.0.0alpha3

See UPGRADE-5.0 for detailed migration instructions.

Added

  • [PHP 8.3] Added support for typed constants.
  • [PHP 8.3] Added support for readonly anonymous classes.
  • Added support for NodeVisitor::REPLACE_WITH_NULL.
  • Added support for CRLF newlines in the pretty printer, using the new newline option.

Changed

  • Use PHP 7.1 as the default target version for the pretty printer.
  • Print else if { } instead of else { if { } }.
  • The leaveNode() method on visitors is now invoked in reverse order of enterNode().
  • Moved NodeTraverser::REMOVE_NODE etc. to NodeVisitor::REMOVE_NODE. The old constants are still available for compatibility.
  • The 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.
  • No longer accept strings for types in Node constructors. Instead, either an Identifier, Name or ComplexType must be passed.
  • Comment::getReformattedText() now normalizes CRLF newlines to LF newlines.

Fixed

  • Don't trim leading whitespace in formatting preserving printer.
  • Treat DEL as a label character in the formatting preserving printer depending on the targeted PHP version.
  • Fix error reporting in emulative lexer without explicitly specified error handler.
  • Gracefully handle non-contiguous array indices in the Differ.
v4.15.5

Added

  • Added makePrivate(), makeProtected(), makePublic() and makeReadonly() methods to Builder\Param to allow the creation of promoted parameters.
v4.15.4

Fixed

  • Fixed formatting-preservation for alternative if syntax with trailing comments.
v5.0.0alpha2

See UPGRADE-5.0 for detailed migration instructions.

Added

  • [PHP 8.3] Added support for dynamic class constant fetch.
  • Added many additional type annotations. PhpStan is now used.
  • Added a fuzzing target for PHP-Fuzzer, which was how a lot of pretty printer bugs were found.
  • Added isPromoted(), isPublic(), isProtected(), isPrivate() and isReadonly() methods on Param.
  • Added support for class constants in trait builder.
  • Added PrettyPrinter interface.
  • Added support for formatting preservation when toggling static modifiers.
  • The php-parse binary now accepts - as the file name, in which case it will read from stdin.

Fixed

  • The pretty printer now uses a more accurate treatment of unary operator precedence, and will only wrap them in parentheses if required. This allowed fixing a number of other precedence related bugs.
  • The pretty printer now respects the precedence of clone, throw and arrow functions.
  • The pretty printer no longer unconditionally wraps yield in parentheses, unless the target version is set to older than PHP 7.0.
  • Fixed formatting preservation for alternative elseif/else syntax.
  • Fixed checks for when it is safe to print strings as heredoc/nowdoc to accommodate flexible doc string semantics.
  • The pretty printer now prints parentheses around new/instanceof operands in all required situations.
  • Similar, differences in allowed expressions on the LHS of -> and :: are now taken into account.
  • Fixed various cases where \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.
  • Fix handling of very large overflowing \u escape sequences.

Removed

  • Removed deprecated Error constructor taking a line number instead of an attributes array.
v4.15.3

Fixed

  • Support readonly property with PHP 8.2 DNF type.
  • Fixed PHP attribute group and PHP-Parser attribute mixup in EnumCase builder.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport