Skip to content

Commit 063d49a

Browse files
Add rules for ReturnTypeDeclaration
1 parent d11762b commit 063d49a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Inpsyde/PhpcsHelpers.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static function isHookClosure(
288288
/** @var array<int, array<string, mixed>> $tokens */
289289
$tokens = $file->getTokens();
290290

291-
if (($tokens[$position]['code'] ?? '') !== T_CLOSURE) {
291+
if (!in_array(($tokens[$position]['code'] ?? ''), [T_CLOSURE, T_FN], true)) {
292292
return false;
293293
}
294294

@@ -486,7 +486,7 @@ public static function functionBoundaries(File $file, int $position): array
486486
/** @var array<int, array<string, mixed>> $tokens */
487487
$tokens = $file->getTokens();
488488

489-
if (!in_array(($tokens[$position]['code'] ?? null), [T_FUNCTION, T_CLOSURE], true)) {
489+
if (!in_array(($tokens[$position]['code'] ?? null), [T_FUNCTION, T_CLOSURE, T_FN], true)) {
490490
return [-1, -1];
491491
}
492492

@@ -539,6 +539,15 @@ public static function returnsCountInfo(File $file, int $position): array
539539
/** @var array<int, array<string, mixed>> $tokens */
540540
$tokens = $file->getTokens();
541541

542+
if (T_FN === $tokens[$position]['code'] ?? null) {
543+
return [
544+
'nonEmpty' => 1,
545+
'void' => 0,
546+
'null' => 0,
547+
'total' => 1,
548+
];
549+
}
550+
542551
$pos = $start + 1;
543552
while ($pos < $end) {
544553
list(, $innerFunctionEnd) = self::functionBoundaries($file, $pos);

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function register()
3838
{
3939
// phpcs:enable Inpsyde.CodeQuality
4040

41-
return [T_FUNCTION, T_CLOSURE];
41+
return [T_FUNCTION, T_CLOSURE, T_FN];
4242
}
4343

4444
/**
@@ -170,7 +170,9 @@ private function maybeErrors(
170170
return;
171171
}
172172

173-
$name = (string)$file->getDeclarationName($position);
173+
$tokenCode = $file->getTokens()[$position]['code'] ?? '';
174+
// TODO Remove check on T_FN if getDeclarationName() will support T_FN out of the box
175+
$name = $tokenCode !== T_FN ? (string)$file->getDeclarationName($position) : '';
174176
if (
175177
PhpcsHelpers::functionIsMethod($file, $position)
176178
&& (in_array($name, self::METHODS_WHITELIST, true) || strpos($name, '__') === 0)

tests/fixtures/return-type-declaration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ function filter_wrapper(): bool
218218
return true;
219219
}
220220

221+
// @phpcsWarningCodeOnNextLine NoReturnType
222+
fn() => true;
223+
224+
fn(): bool => true;
225+
221226
/**
222227
* @return string
223228
* @wp-hook Meh
@@ -266,6 +271,8 @@ public function filterWrapper(string $x, int $y): bool
266271
return "$x, $y";
267272
});
268273

274+
add_filter('x', fn($x, $y) => "$x, $y");
275+
269276
return true;
270277
}
271278

@@ -283,6 +290,8 @@ function ($x, $y) {
283290
10,
284291
2
285292
);
293+
294+
add_action('x', fn($x, $y) => "$x, $y");
286295
}
287296

288297
// @phpcsWarningCodeOnNextLine NoReturnType

0 commit comments

Comments
 (0)