Skip to content

Commit

Permalink
Allow newlines in arrow functions (#301)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sirbrillig authored Mar 31, 2023
1 parent bdf1e50 commit dc5582d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
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

0 comments on commit dc5582d

Please sign in to comment.