879 Commits

Author SHA1 Message Date
Nikita Popov
2d3dd4e23e Don't align phpdoc tags
I did this to start with, but then alignment kept being broken
during refactorings, and at some point I switched to not aligning,
and now we have a big mess.

Add a php-cs-fixer rule to consistently not align phpdoc tags.
2023-09-17 16:00:10 +02:00
Nikita Popov
e395f042d2 Add php-cs-fixer CI job 2023-09-17 11:08:23 +02:00
Nikita Popov
06c7ab51b7 Drop Lexer::getTokens() method
This doesn't make a lot of sense now that Lexer::tokenize() returns
the tokens.

The tokens for the last parse should be fetched via
Parser::getTokens() instead.
2023-09-16 09:50:50 +02:00
Markus Staab
263fa80b81
Use more precise Use_::TYPE_* types (#945)
For better static analysis support in consuming projects.
2023-09-14 10:03:42 +02:00
Nikita Popov
5da5231fde Indent heredoc/nowdoc when targeting PHP >= 7.3 2023-08-27 22:02:31 +02:00
Nikita Popov
8d58380108 Default pretty printer to PHP 7.4 2023-08-27 21:26:47 +02:00
Nikita Popov
ea77807592 Add more property types
Some of these are not maximally accurate due to lack of union
types.
2023-08-17 21:36:07 +02:00
Nikita Popov
502b090900 Add property types
Types omitted in two places where we violate them currently:
Namespace_::$stmts can be null during parsing, and Enum_::$scalarType
can be a complex type for invalid programs.
2023-08-16 21:37:02 +02:00
Nikita Popov
3c0432b09d Remove emulation for unsupported PHP versions 2023-08-16 21:09:51 +02:00
Nikita Popov
ba851243f4 Replace startLexing() with tokenize()
For now Lexer::getTokens() still exists, but should probably be
removed.
2023-08-13 16:06:10 +02:00
Nikita Popov
d1d784a5c6 Fixup line numbers when applying emulator patches
This fixes the test failures on PHP 7.2.
2023-08-13 12:59:26 +02:00
Nikita Popov
62853b179c Fix PhpStan errors 2023-08-13 12:45:21 +02:00
Nikita Popov
4b497045e0 Move attribute handling into parser
The Lexer now only provides the tokens to the parser, while the
parser is responsible for determining which attributes are placed
on notes. This only needs to be done when the attributes are
actually needed, rather than for all tokens.

This removes the usedAttributes lexer option (and lexer options
entirely). The attributes are now enabled unconditionally. They
have less overhead now, and the need to explicitly enable them for
some use cases (e.g. formatting-preserving printing) doesn't seem
like a good tradeoff anymore.

There are some additional changes to the Lexer interface that
should be done after this, and the docs / upgrading guide haven't
been adjusted yet.
2023-08-13 10:40:21 +02:00
Nikita Popov
b20267c5ad Make use of default actions
For the default action $$ = $1, save the closure invocation.
2023-07-09 21:18:21 +02:00
Nikita Popov
748aab3365 Don't set start attributes for whitespace
These will get overwritten later anyway.
2023-07-09 15:47:37 +02:00
Nikita Popov
c48ee36f54 Allow passing visitors to NodeTraverser constructor 2023-07-09 15:34:31 +02:00
Abdul Malik Ikhsan
eaa1d91b4e
Early return false after VariadicPlaceholder check on CallLike::isFirstClassCallable() (#924)
VariadicPlaceholder can only occur as the first (and only) argument, so avoid a loop to check for it.
2023-06-25 18:38:11 +02:00
Nikita Popov
16c766eae1 Don't take subnodes by reference when traversing
Explicitly assign the property where necessary to avoid the
creation of unnecessary reference-wrappers everywhere.
2023-05-28 21:54:07 +02:00
Nikita Popov
53f6717329 Remove unnecessary Node return value from traverseNode()
The node always stays the same: We may only change subnodes.
2023-05-28 21:51:45 +02:00
Nikita Popov
23647573e8 Represent names using string rather than array of parts
In most circumstances we are interested in the whole string, not
the parts split by namespace separator. As names are common, this
representation measurably improves memory usage and performance.
2023-05-21 21:25:49 +02:00
Nikita Popov
df3a7057ab Add Name::getParts(), deprecate Name::$parts
In preparation for switching this to a plain string in
PHP-Parser 5, deprecate direct access to the property and
provide an API that will work on both versions.

(cherry picked from commit c9e5a13d68486e9fd75f9be1b4639644e54e7f4f)
2023-05-21 21:24:13 +02:00
Nikita Popov
d43edfbb31 Support CRLF newlines in pretty printer
Can be enabled using the "newlines" option.
2023-05-21 20:56:56 +02:00
Nikita Popov
ad8daa12b2 Normalize newlines in Comment::getReformattedText()
Normalize CRLF to LF in getReformattedText(). That was, if the
comment is pretty-printed, we will use proper LF newlines, rather
than inserting indentation between the CR and LF.

At the same time, this also makes it easier to emit actual CRLF
newlines with a custom pretty printer.

Fixes #599.
2023-05-21 17:38:56 +02:00
Nikita Popov
5883189d61 Fix return type of Comment::getReformattedText() 2023-05-21 15:51:27 +02:00
Nikita Popov
afe1628a72 Add support for NodeVisitor::REPLACE_WITH_NULL
Fixes #716.
2023-05-21 15:41:41 +02:00
Nikita Popov
289756d056 Gracefully handle non-contiguous arrays in Differ
Fixes #909.
2023-05-21 15:15:36 +02:00
Nikita Popov
8490c0e82d Call leaveNode() on visitors in reverse order
Node visitation is now properly nested. The call sequence will
now be

    $visitor1->enterNode($n);
    $visitor2->enterNode($n);
    $visitor2->leaveNode($n);
    $visitor1->leaveNode($n);

rather than

    $visitor1->enterNode($n);
    $visitor2->enterNode($n);
    $visitor1->leaveNode($n);
    $visitor2->leaveNode($n);

Fixes #899.
2023-05-21 12:31:15 +02:00
Nikita Popov
8bc698248d Ensure removing visitor does not leave holes 2023-05-21 12:07:21 +02:00
Nikita Popov
fb2c3ac97c Fix emulative lexer with default error handler
If no error handler is provided, explicitly create one, so we don't
end up calling handleError() on null.
2023-05-20 22:26:53 +02:00
Nikita Popov
c23976a299 Stop accepting strings as types
For types the use of a string is ambiguous -- it could be either
an Identifier or a Name. Don't guess.

Retain the implicit promotion to Identifier in places where only
Identifier is legal, e.g. various symbol names.
2023-05-20 22:13:04 +02:00
Nikita Popov
a9dad5c54e Fix type of ClassConst::$type 2023-05-20 22:01:01 +02:00
Nikita Popov
74caed6446 Fix labelCharMap for DEL character
This map is supposed to have string keys, not integer keys.
2023-05-20 21:55:53 +02:00
Nikita Popov
a5d4c1005c Remove some unused symbols 2023-05-20 21:40:55 +02:00
Nikita Popov
93731c5cfa Move constants from NodeTraverser to NodeVisitor
These are really part of the NodeVisitor API. Retain aliases for
compatibility.
2023-05-20 21:37:34 +02:00
Nikita Popov
91da19147b Support readonly anonymous classes 2023-05-20 21:17:44 +02:00
Nikita Popov
5c267f55c9 Add support for typed constants
RFC: https://wiki.php.net/rfc/typed_class_constants
2023-05-20 21:02:03 +02:00
Nikita Popov
779b6950c3 Fix coding style 2023-05-20 18:55:12 +02:00
Nikita Popov
b68fb76f14 Add makeReadonly() to param builder
(cherry picked from commit 11e2dcd96c830ee934fa7b0243f4d67d8a8821ab)
2023-05-19 22:17:50 +02:00
Cees-Jan Kiewiet
36a6dcd04e [5.x] Add constructor property promotion
By making flags on the Param builder configurable by providing make(Public|Protected|Private) methods we can promote parameters to properties from the constructor
2023-03-06 23:12:51 +01:00
Nikita Popov
bb4263ea1a Treat del as label character depending on PHP version
In formatting-preserving pretty printing, treat DEL as a label
character based on the target PHP version (the default of 7.1
implying it isn't one).

This avoids failure to round-trip an unchanged input.
2023-03-05 17:03:00 +01:00
Nikita Popov
4ce0de2d12 Use PHP 7.1 as default pretty printer target 2023-03-05 16:56:50 +01:00
Nikita Popov
a3bc900a41 Don't ltrim when preserving formatting
We shouldn't ltrim when printing a whole file, that way we will
not just fail to preserve formatting, but actually change semantics
by dropping meaningful whitespace.
2023-03-05 16:42:30 +01:00
Nikita Popov
aa721520f9 Don't generate braces for "else if"
Mentioned in #915.
2023-03-05 11:30:39 +01:00
Anthony Ferrara
64484a4979 Add PrettyPrinter interface
Closes #423.
2023-03-04 21:56:58 +01:00
Nikita Popov
a0ed229b31 Revert "Rename PrettyPrinterAbstract to PrettyPrinter"
This reverts commit 2217f14d6e039f1c0572329e6fcc99f6c17178a3.
2023-03-04 21:54:56 +01:00
Nikita Popov
698ff1ca46 Don't always omit parentheses for argument-less yield
We may need parentheses around an argument-less yield to distinguish
(yield) - $a from yield -$a and similar.

Treat argument-less yield like a prefix operator. This will print
parentheses a bit more often than is really required, because the
arity ambiguity only exists for certain operators. This seems like
a reasonably good approximation through, as it only affects
argument-less yield on the LHS of infix operators.
2023-03-04 12:06:35 +01:00
Nikita Popov
b3158e0b53 Respect namespace style for enum scalar type printing
Doesn't matter in any practical sense because such types are always
invalid, but makes sure pretty printing round-trips.
2023-03-04 11:56:59 +01:00
Nikita Popov
bbec9db626 Handle overflowing \u escape sequence 2023-03-01 22:39:15 +01:00
Nikita Popov
6649012e6c Produce error if <?= used as identifier
Due to a peculiarity of how <?= is handled, it can get treated as
an echo identifier. Make sure it isn't, to match PHP behavior.
2023-03-01 21:35:27 +01:00
Nikita Popov
2217f14d6e Rename PrettyPrinterAbstract to PrettyPrinter 2023-03-01 21:25:02 +01:00