Skip to content

Fix the indent of a method chain after throw [GH-9841] - #9842

Open
zeleznypa wants to merge 1 commit into
apache:masterfrom
zeleznypa:GH-9841-throw-method-chain-indent
Open

zeleznypa wants to merge 1 commit into
apache:masterfrom
zeleznypa:GH-9841-throw-method-chain-indent

Conversation

@zeleznypa

@zeleznypa zeleznypa commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #9841.

What

The PHP formatter indents the lines of a method chain after throw twice: 8 spaces with Continuation Indentation 4. After an assignment, and in a plain call statement, the same chain gets 4 spaces.

Why

Since the PHP 8.0 support (NETBEANS-4443, 2020) throw is an expression, so throw ...; is an ExpressionStatement that holds a ThrowExpression. visit(ExpressionStatement) adds one continuation indent to every expression statement except a method call, and visit(MethodInvocation) adds a second one for the chain. throw is not excluded from the first one, so it gets both (4 + 4). The links to the code lines and the history are in #9841.

Change

visit(ExpressionStatement) looks through the ThrowExpression when it decides whether the statement adds an indent (new private method isMethodCall). A throw of a method call is treated like a plain method call statement.

Tests

Two new tests in PHPFormatterTest: testThrowExpressionMethodChain_01 (Continuation Indentation 4) and testThrowExpressionMethodChain_02 (8), with one input file and two expected files in testfiles/formatting.

I did not run the tests and did not build NetBeans. The only thing checked is that the changed FormatVisitor.java compiles against the jars of Apache NetBeans 31. The expected files are written by hand and may need to be regenerated after a real run. I searched the formatter test data of master for a multi-line throw statement and found none, so I do not expect an existing expected file to change.

AI assistance and my background

I am not a Java developer. The analysis, the code, the tests and this description were produced with the help of an AI model (Claude Sonnet 5.5), and I cannot judge the Java code on my own. Please review it as code from someone who does not know the codebase, and feel free to change it or to close this PR in favour of a better fix. Maintainer edits are allowed.

Assisted-by: Claude Sonnet 5.5

- apache#9841
- A throw statement is an ExpressionStatement with a ThrowExpression since
  PHP 8.0 support (NETBEANS-4443). visit(ExpressionStatement) added the indent
  of a statement to it, and visit(MethodInvocation) added the indent of the
  chain, so the lines of a chain after throw got the continuation indent twice
- Look through the ThrowExpression when deciding whether the statement adds
  an indent, as is done for a method call statement
- Add unit tests

The change and the tests were written with the help of an AI model and were
not run: the formatter tests were not built or executed, only the changed class
was compiled against the jars of NetBeans 31. The expected files are written
by hand. The author is not a Java developer.

Assisted-by: Claude Sonnet 5.5
@mbien mbien added PHP [ci] enable extra PHP tests (php/php.editor) ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Nov 30, 2026
@apache apache locked and limited conversation to collaborators Nov 30, 2026
@apache apache unlocked this conversation Nov 30, 2026
@matthiasblaesing

Copy link
Copy Markdown
Contributor

@zeleznypa thanks for taking care. As I struggle to remember the correct way to force push into a foreign branch, I decided to create a new PR instead, that picks this up.

Changes there:

  • I fixed the author information (email was missing) of your commit
  • Looking at the new tests I checked what would happen when I switch from throw to return and it turns out, it also looks also not correct. The second commit I added aligns behavior for the return case with the throw case fixed by you.

Could you please have a look and indicate if that is fine with you? If so I'll ping the primary PHP reviewers to get their opinion. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) PHP [ci] enable extra PHP tests (php/php.editor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PHP] Formatter indents a method chain after throw twice (8 spaces with Continuation Indentation 4), since throw became an expression in 2020

3 participants