Skip to content

Commit

Permalink
Fix formatting for an indent of a return statement of a closure [apac…
Browse files Browse the repository at this point in the history
…heGH-7140]

- apache#7140
- Add an indent token to a return statement of a closure if a function invocation has the closure
- Add unit tests

Example:
```php
class GH7140 {
    public function test1(): void {
        $this->test->test(Test::class,
            function (): Test {
                return new Test(
                test: 0,
                );
            });
    }
}
```

Before:
```php
class GH7140 {
    public function test1(): void {
        $this->test->test(Test::class,
            function (): Test {
                return new Test(
                test: 0,
                );
            });
    }
}
```

After:
```php
class GH7140 {
    public function test1(): void {
        $this->test->test(Test::class,
            function (): Test {
                return new Test(
                    test: 0,
                );
            });
    }
}
```
  • Loading branch information
junichi11 committed Jun 9, 2024
1 parent bf9d119 commit 1d10cbc
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ public void visit(ReturnStatement node) {
}

boolean addIndent = !isAnonymousClass(node.getExpression())
&& !(path.size() > 2 && path.get(2) instanceof LambdaFunctionDeclaration) // #259111
&& !(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
112 changes: 112 additions & 0 deletions php/php.editor/test/unit/data/testfiles/formatting/issueGH7140.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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.
*/

class GH7140 {

public function test1(): void {
$this->test->test(Test::class,
function (): Test {
return new Test(
test: 0,
);
});
}

public function test2(): void {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

public function test3(): Test {
return new Test(
test: 0,
);
}
}

function test(): Test {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);

// formatted
class GH7140_2 {

public function test1(): void {
$this->test->test(Test::class,
function (): Test {
return new Test(
test: 0,
);
});
}

public function test2(): void {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

public function test3(): Test {
return new Test(
test: 0,
);
}
}

function test(): Test {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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.
*/

class GH7140 {

public function test1(): void {
$this->test->test(Test::class,
function (): Test {
return new Test(
test: 0,
);
});
}

public function test2(): void {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

public function test3(): Test {
return new Test(
test: 0,
);
}
}

function test(): Test {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);

// formatted
class GH7140_2 {

public function test1(): void {
$this->test->test(Test::class,
function (): Test {
return new Test(
test: 0,
);
});
}

public function test2(): void {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

public function test3(): Test {
return new Test(
test: 0,
);
}
}

function test(): Test {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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.
*/

class GH7140 {

public function test1(): void {
$this->test->test(Test::class,
function (): Test {
return new Test(
test: 0,
);
});
}

public function test2(): void {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

public function test3(): Test {
return new Test(
test: 0,
);
}
}

function test(): Test {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);

// formatted
class GH7140_2 {

public function test1(): void {
$this->test->test(Test::class,
function (): Test {
return new Test(
test: 0,
);
});
}

public function test2(): void {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

public function test3(): Test {
return new Test(
test: 0,
);
}
}

function test(): Test {
test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
}

test(Test::class,
function (): Test {
return new Test(
test: 0,
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -1164,4 +1164,16 @@ public void testGH7185_05() throws Exception {
reformatFileContents("testfiles/formatting/issueGH7185_05.php", options, false, false);
}

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

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

}

0 comments on commit 1d10cbc

Please sign in to comment.