Skip to content

Commit 8c92e99

Browse files
committed
Add support for static short closures
1 parent 2ec02f5 commit 8c92e99

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

Inpsyde/PhpcsHelpers.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public static function functionDocBlockTags(
342342

343343
if (
344344
!array_key_exists($position, $tokens)
345-
|| !in_array($tokens[$position]['code'], [T_FUNCTION, T_CLOSURE], true)
345+
|| !in_array($tokens[$position]['code'], [T_FUNCTION, T_CLOSURE, T_FN], true)
346346
) {
347347
return [];
348348
}
@@ -480,7 +480,7 @@ public static function functionBody(File $file, int $position): string
480480
/**
481481
* @param File $file
482482
* @param int $position
483-
* @return array{int, int}
483+
* @return list{int, int}
484484
*/
485485
public static function functionBoundaries(File $file, int $position): array
486486
{
@@ -491,19 +491,13 @@ public static function functionBoundaries(File $file, int $position): array
491491
return [-1, -1];
492492
}
493493

494-
$functionStart = (int)($tokens[$position]['scope_opener'] ?? 0);
495-
$functionEnd = (int)($tokens[$position]['scope_closer'] ?? 0);
496-
if ($functionStart <= 0 || $functionEnd <= 0 || $functionStart >= ($functionEnd - 1)) {
497-
return [-1, -1];
498-
}
499-
500-
return [$functionStart, $functionEnd];
494+
return static::boundaries($tokens, $position);
501495
}
502496

503497
/**
504498
* @param File $file
505499
* @param int $position
506-
* @return array{int, int}
500+
* @return list{int, int}
507501
*/
508502
public static function classBoundaries(File $file, int $position): array
509503
{
@@ -514,13 +508,7 @@ public static function classBoundaries(File $file, int $position): array
514508
return [-1, -1];
515509
}
516510

517-
$start = (int)($tokens[$position]['scope_opener'] ?? 0);
518-
$end = (int)($tokens[$position]['scope_closer'] ?? 0);
519-
if ($start <= 0 || $end <= 0 || $start >= ($end - 1)) {
520-
return [-1, -1];
521-
}
522-
523-
return [$start, $end];
511+
return static::boundaries($tokens, $position);
524512
}
525513

526514
/**
@@ -728,4 +716,20 @@ public static function isUntypedPsrMethod(File $file, int $position): bool
728716

729717
return false;
730718
}
719+
720+
/**
721+
* @param array<int, array<string, mixed>> $tokens
722+
* @param int $position
723+
* @return list{int, int}
724+
*/
725+
private static function boundaries(array $tokens, int $position): array
726+
{
727+
$start = (int)($tokens[$position]['scope_opener'] ?? 0);
728+
$end = (int)($tokens[$position]['scope_closer'] ?? 0);
729+
if ($start <= 0 || $end <= 0 || $start >= ($end - 1)) {
730+
return [-1, -1];
731+
}
732+
733+
return [$start, $end];
734+
}
731735
}

Inpsyde/Sniffs/CodeQuality/StaticClosureSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
class StaticClosureSniff implements Sniff
1212
{
1313
/**
14-
* @return list<string>
14+
* @return list<int|string>
1515
*/
1616
public function register(): array
1717
{
18-
return [T_CLOSURE];
18+
return [T_CLOSURE, T_FN];
1919
}
2020

2121
/**

tests/fixtures/static-closure.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ function () {
3232
return $foo;
3333
};
3434

35+
fn() => $this;
36+
37+
static fn() => 'Foo';
38+
3539
static function () {
3640
return 'Foo';
3741
};
@@ -60,6 +64,8 @@ public function b()
6064
return 'Foo';
6165
};
6266

67+
$a = fn () => 'x'; // @phpcsWarningOnThisLine
68+
6369
return $a;
6470
}
6571

@@ -80,5 +86,13 @@ function () {
8086
function () {
8187
return 'Foo';
8288
};
89+
90+
/** @bound */
91+
fn() => 'Foo';
92+
93+
fn() => $this;
8394
}
8495
}
96+
97+
add_filter('x', fn() => 'y'); // @phpcsWarningOnThisLine
98+
add_filter('x', static fn() => 'y');

0 commit comments

Comments
 (0)