1101 Commits

Author SHA1 Message Date
Markus Staab
09691fc86e Prevent off-by-one errors in line-number related methods 2024-03-13 20:24:50 +01:00
Nikita Popov
70c96493b4 Fix indentation detection after opening tag
Fixes #982.
2024-03-02 18:59:44 +01:00
Maarten Buis
ec02613432 Update PhpVersion::getNewestSupported() to PHP 8.3 2024-03-02 08:27:21 +01:00
Nikita Popov
af14fdb282 Avoid cyclic reference in parser
Pass $this as an explicit $self argument to the reduce callbacks,
so we can make them static. This avoids a circular reference in
the parser, so that it can be immediately destroyed when it goes
out of scope.

Fixes #980.
2024-02-21 21:13:45 +01:00
Nikita Popov
ff095c3c65 Check for tokens with non-integer ID
Fixes #974.
2024-01-10 20:34:09 +01:00
Ondrej Mirtes
1eeeb2d525 Fix parent class of PropertyItem and UseItem 2023-12-22 19:57:32 +01:00
Nikita Popov
f7d484aa0e Fix handling of empty input
Fixes #967.
2023-12-20 21:51:46 +01:00
Ondrej Mirtes
f66650073c Fix NameResolver for class constant native type 2023-11-28 20:10:53 +01:00
Nikita Popov
3640d18b87 Remove ParserFactory::create()
Don't try to keep backwards-compatibility with the old factory
style, which doesn't map cleanly onto supported options (we only
have ONLY_PHP7/PREFER_PHP7, which should probably create a Php8
parser in terms of how they are used, but this would no longer
match their names).

Instead, I have backported the new createForNewestSupportedVersion()
and createForHostVersion() methods to PHP-Parser 4.
2023-11-12 16:52:30 +01:00
Nikita Popov
acfccd9d74 Run with updated php-cs-fixer 2023-11-01 18:50:25 +01:00
xjaja
8d50e9d066 Don't drop class statements before error (#952)
When encountering a null statement (indicating that an error occurred),
retain the preceding statements. These were accidentally dropped
previously.

(cherry picked from commit 54103d838734be0499172026525e38cbf6af2711)
2023-10-07 12:22:01 +02:00
Abdul Malik Ikhsan
481fec47f4 Improve performance of find() and findFirst() when passed $nodes is empty array 2023-10-07 12:11:47 +02:00
Nikita Popov
4e27a17cd8 Use visitor to assign comments
This fixes the long-standing issue where a comment would get assigned
to all nodes with the same starting position, instead of only the
outer-most one.

Fixes #253.
2023-09-28 21:45:08 +02:00
Nikita Popov
de84f76766 Avoid by-reference iteration in NodeTraverser
No need to create reference wrappers around every element in the
array.
2023-09-26 20:25:37 +02:00
Nikita Popov
b4183c2b09 Remove Stmt\Throw
This was a backwards-compatibility shim for Expr\Throw.
2023-09-25 18:37:07 +02:00
Nikita Popov
a1ccf57727 Introduce Stmt\Block
Stmt\Block will be created for { $a; } style blocks, unless these
occur directly inside some structure that is usually combined
with a block.

For example if ($a) { $b; } will continue to use the old
representation (plain array in in If_::$stmts), but a free-standing
{ $b; } will become a Stmt\Block.

Fixes #590.
2023-09-24 21:13:01 +02:00
Nikita Popov
f5adbb5e3f Add rawValue to InterpolatedStringPart and doc strings
For doc strings, the rawValue (on either the String_ or
InterpolatedStringPrts) does not include the leading indentation
(which is available as docIndentation) or the trailing newline on
the last part.
2023-09-24 12:55:38 +02:00
Nikita Popov
5edc190bda Add support for dumping additional attributes 2023-09-23 21:36:57 +02:00
Nikita Popov
80851163a6 Assemble NodeDumper result in property
Append to a property instead of returning strings. This is
significantly faster when dumping large ASTs.

This also fixes an inconsistency where the indentation level for
strings and comments was off-by-one.
2023-09-23 20:38:53 +02:00
Nikita Popov
d8e8065313 Don't parse unicode escapes for PHP < 7.0
We still had the option for this but were hardcoding it to true.
Make it conditional on the PHP version instead.
2023-09-23 17:35:51 +02:00
RainX
f4961b89ac
Add missing phpdoc annotations in AST nodes (#946) 2023-09-18 21:16:54 +02:00
Nikita Popov
ab51e9d35a Remove superfluous phpdoc tags
These just specify a type that is already specified as a real PHP
type.
2023-09-17 18:24:05 +02:00
Nikita Popov
1873020bf7 Deprecate Node::getLine() in favor of Node::getStartLine() 2023-09-17 16:32:10 +02:00
Nikita Popov
1b346f7935 Remove deprecated Comment methods 2023-09-17 16:30:28 +02:00
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