Skip to content

Commit

Permalink
Add support for static short closures
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Aug 25, 2023
1 parent 2ec02f5 commit 8c92e99
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
38 changes: 21 additions & 17 deletions Inpsyde/PhpcsHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public static function functionDocBlockTags(

if (
!array_key_exists($position, $tokens)
|| !in_array($tokens[$position]['code'], [T_FUNCTION, T_CLOSURE], true)
|| !in_array($tokens[$position]['code'], [T_FUNCTION, T_CLOSURE, T_FN], true)
) {
return [];
}
Expand Down Expand Up @@ -480,7 +480,7 @@ public static function functionBody(File $file, int $position): string
/**
* @param File $file
* @param int $position
* @return array{int, int}
* @return list{int, int}
*/
public static function functionBoundaries(File $file, int $position): array
{
Expand All @@ -491,19 +491,13 @@ public static function functionBoundaries(File $file, int $position): array
return [-1, -1];
}

$functionStart = (int)($tokens[$position]['scope_opener'] ?? 0);
$functionEnd = (int)($tokens[$position]['scope_closer'] ?? 0);
if ($functionStart <= 0 || $functionEnd <= 0 || $functionStart >= ($functionEnd - 1)) {
return [-1, -1];
}

return [$functionStart, $functionEnd];
return static::boundaries($tokens, $position);
}

/**
* @param File $file
* @param int $position
* @return array{int, int}
* @return list{int, int}
*/
public static function classBoundaries(File $file, int $position): array
{
Expand All @@ -514,13 +508,7 @@ public static function classBoundaries(File $file, int $position): array
return [-1, -1];
}

$start = (int)($tokens[$position]['scope_opener'] ?? 0);
$end = (int)($tokens[$position]['scope_closer'] ?? 0);
if ($start <= 0 || $end <= 0 || $start >= ($end - 1)) {
return [-1, -1];
}

return [$start, $end];
return static::boundaries($tokens, $position);
}

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

return false;
}

/**
* @param array<int, array<string, mixed>> $tokens
* @param int $position
* @return list{int, int}
*/
private static function boundaries(array $tokens, int $position): array
{
$start = (int)($tokens[$position]['scope_opener'] ?? 0);
$end = (int)($tokens[$position]['scope_closer'] ?? 0);
if ($start <= 0 || $end <= 0 || $start >= ($end - 1)) {
return [-1, -1];
}

return [$start, $end];
}
}
4 changes: 2 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/StaticClosureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
class StaticClosureSniff implements Sniff
{
/**
* @return list<string>
* @return list<int|string>
*/
public function register(): array
{
return [T_CLOSURE];
return [T_CLOSURE, T_FN];
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/static-closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function () {
return $foo;
};

fn() => $this;

static fn() => 'Foo';

static function () {
return 'Foo';
};
Expand Down Expand Up @@ -60,6 +64,8 @@ public function b()
return 'Foo';
};

$a = fn () => 'x'; // @phpcsWarningOnThisLine

return $a;
}

Expand All @@ -80,5 +86,13 @@ function () {
function () {
return 'Foo';
};

/** @bound */
fn() => 'Foo';

fn() => $this;
}
}

add_filter('x', fn() => 'y'); // @phpcsWarningOnThisLine
add_filter('x', static fn() => 'y');

0 comments on commit 8c92e99

Please sign in to comment.