Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\FunctionHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function preg_match;
Expand All @@ -21,6 +22,7 @@ class ArrowFunctionDeclarationSniff implements Sniff
public const CODE_INCORRECT_SPACES_AFTER_KEYWORD = 'IncorrectSpacesAfterKeyword';
public const CODE_INCORRECT_SPACES_BEFORE_ARROW = 'IncorrectSpacesBeforeArrow';
public const CODE_INCORRECT_SPACES_AFTER_ARROW = 'IncorrectSpacesAfterArrow';
public const CODE_DISALLOWED_RETURN_TYPE_HINT = 'DisallowedReturnTypeHint';

public int $spacesCountAfterKeyword = 1;

Expand All @@ -30,6 +32,8 @@ class ArrowFunctionDeclarationSniff implements Sniff

public bool $allowMultiLine = false;

public bool $disallowReturnTypeHint = false;

/**
* @return array<int, (int|string)>
*/
Expand All @@ -50,10 +54,46 @@ public function process(File $phpcsFile, int $arrowFunctionPointer): void

$arrowPointer = TokenHelper::findNext($phpcsFile, T_FN_ARROW, $arrowFunctionPointer);

$this->checkReturnTypeHint($phpcsFile, $arrowFunctionPointer, $arrowPointer);

$this->checkSpacesBeforeArrow($phpcsFile, $arrowPointer);
$this->checkSpacesAfterArrow($phpcsFile, $arrowPointer);
}

private function checkReturnTypeHint(File $phpcsFile, int $arrowFunctionPointer, int $arrowPointer): void
{
if (!$this->disallowReturnTypeHint) {
return;
}

$returnTypeHint = FunctionHelper::findReturnTypeHint($phpcsFile, $arrowFunctionPointer);
if ($returnTypeHint === null) {
return;
}

$fix = $phpcsFile->addFixableError(
'Arrow function must not have return type hint.',
$returnTypeHint->getStartPointer(),
self::CODE_DISALLOWED_RETURN_TYPE_HINT,
);
if (!$fix) {
return;
}

$colonPointer = TokenHelper::findPreviousEffective($phpcsFile, $returnTypeHint->getStartPointer() - 1);

$pointerBeforeColon = TokenHelper::findPreviousEffective($phpcsFile, $colonPointer - 1);

$phpcsFile->fixer->beginChangeset();
FixerHelper::removeBetween($phpcsFile, $pointerBeforeColon, $arrowPointer);

if ($this->spacesCountBeforeArrow > 0) {
FixerHelper::addBefore($phpcsFile, $arrowPointer, str_repeat(' ', $this->spacesCountBeforeArrow));
}

$phpcsFile->fixer->endChangeset();
}

private function checkSpacesAfterKeyword(File $phpcsFile, int $arrowFunctionPointer): void
{
$pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $arrowFunctionPointer + 1);
Expand Down
1 change: 1 addition & 0 deletions doc/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Sniff provides the following settings:
* `spacesCountBeforeArrow`: the number of spaces before `=>`.
* `spacesCountAfterArrow`: the number of spaces after `=>`.
* `allowMultiLine`: allows multi-line declaration.
* `disallowReturnTypeHint` (default: `false`): disallows return type hints in arrow functions.

#### SlevomatCodingStandard.Functions.DisallowArrowFunction

Expand Down
36 changes: 36 additions & 0 deletions tests/Sniffs/Functions/ArrowFunctionDeclarationSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,40 @@ public function testModifiedSettingsErrors(): void
self::assertAllFixedInFile($report);
}

public function testDisallowReturnTypeHintErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/arrowFunctionDeclarationDisallowReturnTypeHintErrors.php', [
'disallowReturnTypeHint' => true,
]);

self::assertSame(4, $report->getErrorCount());

self::assertSniffError(
$report,
3,
ArrowFunctionDeclarationSniff::CODE_DISALLOWED_RETURN_TYPE_HINT,
'Arrow function must not have return type hint.',
);
self::assertSniffError(
$report,
5,
ArrowFunctionDeclarationSniff::CODE_DISALLOWED_RETURN_TYPE_HINT,
'Arrow function must not have return type hint.',
);
self::assertSniffError(
$report,
7,
ArrowFunctionDeclarationSniff::CODE_DISALLOWED_RETURN_TYPE_HINT,
'Arrow function must not have return type hint.',
);
self::assertSniffError(
$report,
9,
ArrowFunctionDeclarationSniff::CODE_DISALLOWED_RETURN_TYPE_HINT,
'Arrow function must not have return type hint.',
);

self::assertAllFixedInFile($report);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php // lint >= 8.1

fn ($a) => $a;

fn ($a) => $a;

fn ($a) => $a;

fn ($a) => $a;

fn ($a) => $a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php // lint >= 8.1

fn ($a): int => $a;

fn ($a): ?string => $a;

fn ($a): int|string => $a;

fn ($a): A&B => $a;

fn ($a) => $a;
Loading