Skip to content

Allow newlines in arrow functions #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2023
Merged
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
2 changes: 2 additions & 0 deletions Tests/VariableAnalysisSniff/ArrowFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testArrowFunctions()
87,
102,
112,
150,
];
$this->assertSame($expectedWarnings, $lines);
}
Expand Down Expand Up @@ -64,6 +65,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed()
87,
102,
112,
150,
];
$this->assertSame($expectedWarnings, $lines);
}
Expand Down
39 changes: 39 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,42 @@ function arrowFunctionWithReturnType() {
$type = do_something(fn(string $func): string => $func ? $func : '');
echo $type;
}

function arrowFunctionWithNewlines( $items ): array {
return $items
->map(
fn ( array $item ) => apply_overrides(
[
'a' => ! empty( $item['b'] ),
],
$item,
)
)
->filter( fn ( array $item ) => ! empty( $item['post'] ) )
->values()
->all();
}

function arrowFunctionWithNewClass(): array {
$arrow = fn($a) => new class($a) {
public function __construct($key) {
$this->key = $key;
echo $bar; // undefined variable $bar
}
};
echo $arrow;
}

function arrowFunctionWithQuotes($allowedReferrers) {
array_map(
static fn (string $allowedReferrer) => str_replace(
['\*\*', '\*'],
['[a-z\d.-]{0,63}', '[a-z\d-]{0,63}'],
preg_quote($allowedReferrer, '~'),
),
$allowedReferrers;
do_something(
static fn (string $permissionName) => Str::startsWith($permissionName, CONFIG_START)
&& $permissionName !== CustomPermission::ALL_CONFIG
);
}
5 changes: 0 additions & 5 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,6 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
break;
}

// A line break is always a closer.
if ($token['line'] !== $tokens[$stackPtr]['line']) {
$scopeCloserIndex = $index;
break;
}
$code = $token['code'];

// A semicolon is always a closer.
Expand Down