Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import org.netbeans.modules.php.editor.parser.astnodes.StaticStatement;
import org.netbeans.modules.php.editor.parser.astnodes.SwitchCase;
import org.netbeans.modules.php.editor.parser.astnodes.SwitchStatement;
import org.netbeans.modules.php.editor.parser.astnodes.ThrowExpression;
import org.netbeans.modules.php.editor.parser.astnodes.TraitConflictResolutionDeclaration;
import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration;
import org.netbeans.modules.php.editor.parser.astnodes.TraitMethodAliasDeclaration;
Expand Down Expand Up @@ -1295,6 +1296,20 @@ public void visit(DoStatement node) {
addAllUntilOffset(node.getEndOffset());
}

/**
* Whether the expression is a method call that adds the indent of its own
* chain, so the statement must not add one more. {@code throw} is an
* expression since PHP 8.0, so look at what it throws.
*/
private static boolean isMethodCall(Expression expression) {
Expression target = expression;
if (target instanceof ThrowExpression) {
target = ((ThrowExpression) target).getExpression();
}
return target instanceof MethodInvocation
|| target instanceof StaticMethodInvocation;
}

@Override
public void visit(ExpressionStatement node) {
if (node.getExpression() instanceof FunctionInvocation) {
Expand All @@ -1304,8 +1319,7 @@ public void visit(ExpressionStatement node) {
if (moveNext() && lastIndex < ts.index()) {
addFormatToken(formatTokens); // add the first token of the expression and then add the indentation
Expression expression = node.getExpression();
boolean addIndent = !(expression instanceof MethodInvocation
|| expression instanceof StaticMethodInvocation
boolean addIndent = !(isMethodCall(expression)
|| isAnonymousClass(expression));
if (expression instanceof Assignment) {
// anonymous classes
Expand Down Expand Up @@ -2310,7 +2324,9 @@ public void visit(ReturnStatement node) {
addFormatToken(formatTokens);
}

boolean addIndent = !isAnonymousClass(node.getExpression())
boolean addIndent =
!isAnonymousClass(node.getExpression())
&& !isMethodCall(node.getExpression())
&& !(path.size() > 3 && path.get(2) instanceof LambdaFunctionDeclaration && !(path.get(3) instanceof FunctionInvocation)) // #259111 GH-7140
&& !(node.getExpression() instanceof LambdaFunctionDeclaration) // NETBEANS-4970
&& !(node.getExpression() instanceof MatchExpression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function returnMethodChainRef1($factory): Exception {
$exception = Exception::create()
->message('a')
->code(1);
return $exception;
}

function returnMethodChainRef2($factory): Exception {
$exception = $factory->create()
->message('a')
->code(1);
return $exception;
}

function returnMethodChain1($factory): Exception {
return Exception::create()
->message('a')
->code(1);
}

function returnMethodChain2($factory): Exception {
return Exception::create()->message('a')
->code(1);
}

function returnMethodChain3($factory): Exception {
return $factory->create()
->message('a')
->code(1);
}

function returnMethodChain4($factory): Exception {
return $factory
->create()
->message('a')
->code(1);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function returnMethodChainRef1($factory): Exception {
$exception = Exception::create()
->message('a')
->code(1);
return $exception;
}

function returnMethodChainRef2($factory): Exception {
$exception = $factory->create()
->message('a')
->code(1);
return $exception;
}

function returnMethodChain1($factory): Exception {
return Exception::create()
->message('a')
->code(1);
}

function returnMethodChain2($factory): Exception {
return Exception::create()->message('a')
->code(1);
}

function returnMethodChain3($factory): Exception {
return $factory->create()
->message('a')
->code(1);
}

function returnMethodChain4($factory): Exception {
return $factory
->create()
->message('a')
->code(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function returnMethodChainRef1($factory): Exception {
$exception = Exception::create()
->message('a')
->code(1);
return $exception;
}

function returnMethodChainRef2($factory): Exception {
$exception = $factory->create()
->message('a')
->code(1);
return $exception;
}

function returnMethodChain1($factory): Exception {
return Exception::create()
->message('a')
->code(1);
}

function returnMethodChain2($factory): Exception {
return Exception::create()->message('a')
->code(1);
}

function returnMethodChain3($factory): Exception {
return $factory->create()
->message('a')
->code(1);
}

function returnMethodChain4($factory): Exception {
return $factory
->create()
->message('a')
->code(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function throwMethodChain($factory): void {
throw Exception::create()
->message('a')
->code(1);

throw $factory->create()
->message('a')
->code(1);

$exception = Exception::create()
->message('a')
->code(1);

$factory->create()
->message('a')
->code(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function throwMethodChain($factory): void {
throw Exception::create()
->message('a')
->code(1);

throw $factory->create()
->message('a')
->code(1);

$exception = Exception::create()
->message('a')
->code(1);

$factory->create()
->message('a')
->code(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function throwMethodChain($factory): void {
throw Exception::create()
->message('a')
->code(1);

throw $factory->create()
->message('a')
->code(1);

$exception = Exception::create()
->message('a')
->code(1);

$factory->create()
->message('a')
->code(1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,18 @@ public void testThrowExpression_01() throws Exception {
reformatFileContents("testfiles/formatting/throwExpression01.php", options);
}

public void testThrowExpressionMethodChain_01() throws Exception {
HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 4);
reformatFileContents("testfiles/formatting/issueThrowMethodChain.php", options, false, true);
}

public void testThrowExpressionMethodChain_02() throws Exception {
HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 8);
reformatFileContents("testfiles/formatting/issueThrowMethodChain.php", options, false, true);
}

public void testMatchExpressionNewLineBracePlacement_01() throws Exception {
HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
options.put(FmtOptions.MATCH_BRACE_PLACEMENT, CodeStyle.BracePlacement.NEW_LINE);
Expand Down Expand Up @@ -1392,4 +1404,16 @@ public void testPipeOperatorMultilineExpression_01() throws Exception {
HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
reformatFileContents("testfiles/formatting/php85/pipeOperatorMultilineExpression_01.php", options);
}

public void testReturnStatementMethodChain_01() throws Exception {
HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 4);
reformatFileContents("testfiles/formatting/issueReturnMethodChain.php", options, false, true);
}

public void testReturnStatementMethodChain_02() throws Exception {
HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 8);
reformatFileContents("testfiles/formatting/issueReturnMethodChain.php", options, false, true);
}
}
Loading