Skip to content

Releases

Released version 1.0.1

Choose a tag to compare

@dg dg released this 15 Nov 21:45
  • New Indentation::opensMatchArm() – tells whether a token begins an arm of match, the companion of opensStatement() for a tool deciding how deep a continuation line goes.
  • Indentation::infer() places a line continuing an expression that begins an arm of match one level deeper than the arm, so an operator such as || $b stands apart from the next arm.

Released version 1.0.0 🎉

Choose a tag to compare

@dg dg released this 22 Oct 00:07

The first release of PhpSyntax, a lossless concrete syntax tree for PHP. Every token, space and comment of the source is in the tree, and printing it gives the file back byte for byte, so a tool that changes one thing produces a diff with one thing in it. It is built for coding-standard fixers, codemods and upgrade tools, and DressCode already runs on it.

  • Lossless round trip – Printer::print($file) === $code for any input PHP accepts, BOM, CRLF, ?> with HTML after it, heredocs and __halt_compiler() data included; whitespace and comments live as trivia on the token they belong to
  • A typed tree with named slots – $if->condition, $if->body, $call->arguments; every token, ( and ; included, is a child, and lists are nodes of their own
  • Edits that leave the rest of the file alone – write a slot by assignment, replaceWith(), remove() or insert() into a list; a list clones its separator style and its neighbor's indentation, a removed line takes its comment and separator with it, and replaceWithExpression() adds the parentheses only where precedence needs them
  • Builder – make nodes from text (expression(), statement(), type(), fragment()), from templates with named placeholders, or from nodes and values you already hold (methodCall(), binary(), value())
  • Questions a rewrite needs answered – matches(), isRepeatableRead(), hasInnerComment(), isRedundant() on parentheses, alwaysLeaves(), toValue() and more, so the safety checks of a fixer are method calls, not heuristics
  • Positions that follow your edits – lines, columns and offsets kept up to date incrementally, so the read–change–read loop of a fixer stays linear; findNode() maps an editor position back to a node
  • Name resolution and imports – NameResolver resolves names against namespace and imports, shortenName() writes a name the way this file would, addImport() adds a use in the style the statement already has; Scope tells where $this is available
  • Modern syntax on every supported runtime – reads PHP 8.5 syntax plus the partial function application of PHP 8.6 on PHP 8.4 to 8.6, through token emulation on older runtimes
  • bin/phpsyntax – check the round trip over your own project, dump the tree of a file or an expression, list its tokens, resolve its names, find a node class
  • No package dependencies – just PHP 8.4+ and ext-tokenizer, so it never brings version constraints into the projects your tool runs in

Coming from nikic/php-parser? The migration guide maps one onto the other, and the examples show each idea as a runnable script.