Skip to content

Commit 2c13a90

Browse files
authored
Merge pull request #1308 from Soh1121/issue-968/fix-wrong-indentation-with-arrow-function
Fix wrong indentation with arrow function
2 parents 6890455 + ff60d8a commit 2c13a90

File tree

6 files changed

+119
-15
lines changed

6 files changed

+119
-15
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,15 +1134,6 @@ public function process(File $phpcsFile, int $stackPtr)
11341134
}
11351135

11361136
$condition = $tokens[$tokens[$i]['scope_condition']]['code'];
1137-
if ($condition === T_FN) {
1138-
if ($this->debug === true) {
1139-
$line = $tokens[$tokens[$i]['scope_condition']]['line'];
1140-
StatusWriter::write("* ignoring arrow function on line $line *");
1141-
}
1142-
1143-
$i = $closer;
1144-
continue;
1145-
}
11461137

11471138
if (isset(Tokens::SCOPE_OPENERS[$condition]) === true
11481139
&& in_array($condition, $this->nonIndentingScopes, true) === false
@@ -1175,14 +1166,15 @@ public function process(File $phpcsFile, int $stackPtr)
11751166
}
11761167
}
11771168

1178-
// Closing an anon class, closure, or match.
1169+
// Closing an anon class, closure, match, or arrow function.
11791170
// Each may be returned, which can confuse control structures that
11801171
// use return as a closer, like CASE statements.
11811172
if (isset($tokens[$i]['scope_condition']) === true
11821173
&& $tokens[$i]['scope_closer'] === $i
11831174
&& ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
11841175
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS
1185-
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_MATCH)
1176+
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_MATCH
1177+
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_FN)
11861178
) {
11871179
if ($this->debug === true) {
11881180
$type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2)));

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,34 @@ function test() {
16201620
from [ 3, 4 ];
16211621
}
16221622

1623+
// Issue #968: Arrow function in function call argument
1624+
return json_encode(
1625+
array_filter(
1626+
[
1627+
'foo' => 42,
1628+
'bar' => null,
1629+
],
1630+
static fn(mixed $value) => $value !== null
1631+
),
1632+
JSON_THROW_ON_ERROR
1633+
);
1634+
1635+
// Issue #968: Arrow function with multiple arguments
1636+
$result = array_map(
1637+
static fn($a, $b) => $a + $b,
1638+
$array1,
1639+
$array2
1640+
);
1641+
1642+
// Issue #968: Nested arrow function
1643+
$result = array_map(
1644+
static fn($x) => array_filter(
1645+
$x,
1646+
static fn($y) => $y > 0
1647+
),
1648+
$data
1649+
);
1650+
16231651
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
16241652
?>
16251653

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,34 @@ function test() {
16201620
from [ 3, 4 ];
16211621
}
16221622

1623+
// Issue #968: Arrow function in function call argument
1624+
return json_encode(
1625+
array_filter(
1626+
[
1627+
'foo' => 42,
1628+
'bar' => null,
1629+
],
1630+
static fn(mixed $value) => $value !== null
1631+
),
1632+
JSON_THROW_ON_ERROR
1633+
);
1634+
1635+
// Issue #968: Arrow function with multiple arguments
1636+
$result = array_map(
1637+
static fn($a, $b) => $a + $b,
1638+
$array1,
1639+
$array2
1640+
);
1641+
1642+
// Issue #968: Nested arrow function
1643+
$result = array_map(
1644+
static fn($x) => array_filter(
1645+
$x,
1646+
static fn($y) => $y > 0
1647+
),
1648+
$data
1649+
);
1650+
16231651
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
16241652
?>
16251653

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,34 @@ function test() {
16201620
from [ 3, 4 ];
16211621
}
16221622

1623+
// Issue #968: Arrow function in function call argument
1624+
return json_encode(
1625+
array_filter(
1626+
[
1627+
'foo' => 42,
1628+
'bar' => null,
1629+
],
1630+
static fn(mixed $value) => $value !== null
1631+
),
1632+
JSON_THROW_ON_ERROR
1633+
);
1634+
1635+
// Issue #968: Arrow function with multiple arguments
1636+
$result = array_map(
1637+
static fn($a, $b) => $a + $b,
1638+
$array1,
1639+
$array2
1640+
);
1641+
1642+
// Issue #968: Nested arrow function
1643+
$result = array_map(
1644+
static fn($x) => array_filter(
1645+
$x,
1646+
static fn($y) => $y > 0
1647+
),
1648+
$data
1649+
);
1650+
16231651
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
16241652
?>
16251653

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,34 @@ function test() {
16201620
from [ 3, 4 ];
16211621
}
16221622

1623+
// Issue #968: Arrow function in function call argument
1624+
return json_encode(
1625+
array_filter(
1626+
[
1627+
'foo' => 42,
1628+
'bar' => null,
1629+
],
1630+
static fn(mixed $value) => $value !== null
1631+
),
1632+
JSON_THROW_ON_ERROR
1633+
);
1634+
1635+
// Issue #968: Arrow function with multiple arguments
1636+
$result = array_map(
1637+
static fn($a, $b) => $a + $b,
1638+
$array1,
1639+
$array2
1640+
);
1641+
1642+
// Issue #968: Nested arrow function
1643+
$result = array_map(
1644+
static fn($x) => array_filter(
1645+
$x,
1646+
static fn($y) => $y > 0
1647+
),
1648+
$data
1649+
);
1650+
16231651
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
16241652
?>
16251653

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ public function getErrorList($testFile = '')
169169
1527 => 1,
170170
1529 => 1,
171171
1530 => 1,
172-
1631 => 1,
173-
1632 => 1,
174-
1633 => 1,
175-
1634 => 1,
172+
1659 => 1,
173+
1660 => 1,
174+
1661 => 1,
175+
1662 => 1,
176176
];
177177
}
178178

0 commit comments

Comments
 (0)