-3

I wonder if it exists pretty printer for php-parser which is capable to enforce desired maximum line length?

(It seems rather simple to implement for some basic cases (array elements list, function arguments list), but it starts to be puzzling with variable expressions etc.)

ts.
  • 10,510
  • 7
  • 47
  • 73

2 Answers2

4

As far as I know there's no existing pretty printer for PHP-Parser that takes a right margin into account.

There's the standard pretty printer of PHP-Parser itself.

There's also a PSR-2 pretty printer made for PHP-Parser.

DIY

If these don't suffice, you'll have to write a pretty printer yourself.

IMHO this shouldn't be to hard. You can simply wrap when a node exceeds the right margin and indent 4 spaces (or whatever you use). Then you can start optimizing things like array definitions and such.

Jasper N. Brouwer
  • 21,517
  • 4
  • 52
  • 76
1

Sorry for the late reply. You could use PHP Front too. Indentation is done for all nestings of statements, 2 spaces per nesting.

Some customized Indentation is possible. And it is available in PHP Front.

The parser and the pretty printer are also tested together using the test-files of the source-distribution of PHP.

Each test-file is parsed, pretty-printed, parsed and pretty-printed again.

The correctness of this round-trip is tested by performing a diff between the two parsed and the two pretty-printed files.

However I got recommendation to use Standard one as it has many features. It has variable expressions and array expressions feautures. Where as in PHP front, it is still some bugs available to use arrays.

Standard Pretty Printer: (Variable Expressions & Array)

 public function pExpr_Variable(Expr\Variable $node) {
        if ($node->name instanceof Expr) {
            return '${' . $this->p($node->name) . '}';
        } else {
            return '$' . $node->name;
        }
    }

    public function pExpr_Array(Expr\Array_ $node) {
        return 'array(' . $this->pCommaSeparated($node->items) . ')';
    }
alagu
  • 779
  • 1
  • 5
  • 19