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

2 Answers 2

4
+100

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.

Sign up to request clarification or add additional context in comments.

Comments

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) . ')';
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.