From dc5582dc5a93a235557af73e523c389aac9a8e88 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Fri, 31 Mar 2023 12:46:32 -0400 Subject: [PATCH] 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 --- .../ArrowFunctionTest.php | 2 + .../fixtures/ArrowFunctionFixture.php | 39 +++++++++++++++++++ VariableAnalysis/Lib/Helpers.php | 5 --- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Tests/VariableAnalysisSniff/ArrowFunctionTest.php b/Tests/VariableAnalysisSniff/ArrowFunctionTest.php index 60b25ab..471d315 100644 --- a/Tests/VariableAnalysisSniff/ArrowFunctionTest.php +++ b/Tests/VariableAnalysisSniff/ArrowFunctionTest.php @@ -32,6 +32,7 @@ public function testArrowFunctions() 87, 102, 112, + 150, ]; $this->assertSame($expectedWarnings, $lines); } @@ -64,6 +65,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed() 87, 102, 112, + 150, ]; $this->assertSame($expectedWarnings, $lines); } diff --git a/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php b/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php index 89f466e..9a16dc4 100644 --- a/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php @@ -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 + ); +} diff --git a/VariableAnalysis/Lib/Helpers.php b/VariableAnalysis/Lib/Helpers.php index 720b00b..f0cc6d5 100644 --- a/VariableAnalysis/Lib/Helpers.php +++ b/VariableAnalysis/Lib/Helpers.php @@ -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.