Skip to content

Commit dc5582d

Browse files
authored
Allow newlines in arrow functions (#301)
* Add test for arrow func with newlines * Allow newlines in arrow functions * Add test for new class in arrow function * Add some more arrow function tests
1 parent bdf1e50 commit dc5582d

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

Tests/VariableAnalysisSniff/ArrowFunctionTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testArrowFunctions()
3232
87,
3333
102,
3434
112,
35+
150,
3536
];
3637
$this->assertSame($expectedWarnings, $lines);
3738
}
@@ -64,6 +65,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed()
6465
87,
6566
102,
6667
112,
68+
150,
6769
];
6870
$this->assertSame($expectedWarnings, $lines);
6971
}

Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php

+39
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,42 @@ function arrowFunctionWithReturnType() {
127127
$type = do_something(fn(string $func): string => $func ? $func : '');
128128
echo $type;
129129
}
130+
131+
function arrowFunctionWithNewlines( $items ): array {
132+
return $items
133+
->map(
134+
fn ( array $item ) => apply_overrides(
135+
[
136+
'a' => ! empty( $item['b'] ),
137+
],
138+
$item,
139+
)
140+
)
141+
->filter( fn ( array $item ) => ! empty( $item['post'] ) )
142+
->values()
143+
->all();
144+
}
145+
146+
function arrowFunctionWithNewClass(): array {
147+
$arrow = fn($a) => new class($a) {
148+
public function __construct($key) {
149+
$this->key = $key;
150+
echo $bar; // undefined variable $bar
151+
}
152+
};
153+
echo $arrow;
154+
}
155+
156+
function arrowFunctionWithQuotes($allowedReferrers) {
157+
array_map(
158+
static fn (string $allowedReferrer) => str_replace(
159+
['\*\*', '\*'],
160+
['[a-z\d.-]{0,63}', '[a-z\d-]{0,63}'],
161+
preg_quote($allowedReferrer, '~'),
162+
),
163+
$allowedReferrers;
164+
do_something(
165+
static fn (string $permissionName) => Str::startsWith($permissionName, CONFIG_START)
166+
&& $permissionName !== CustomPermission::ALL_CONFIG
167+
);
168+
}

VariableAnalysis/Lib/Helpers.php

-5
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,6 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
665665
break;
666666
}
667667

668-
// A line break is always a closer.
669-
if ($token['line'] !== $tokens[$stackPtr]['line']) {
670-
$scopeCloserIndex = $index;
671-
break;
672-
}
673668
$code = $token['code'];
674669

675670
// A semicolon is always a closer.

0 commit comments

Comments
 (0)