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
23 changes: 23 additions & 0 deletions Universal/Docs/WhiteSpace/FnKeywordSpacingStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Fn Keyword Spacing"
>
<standard>
<![CDATA[
The `fn` keyword of an arrow function should not be succeeded by a space.
]]>
</standard>
<code_comparison>
<code title="Valid: No space after the `fn` keyword.">
<![CDATA[
$fn = fn<em></em>($x) => $x + 1;
]]>
</code>
<code title="Invalid: Space after the `fn` keyword.">
<![CDATA[
$fn = fn<em> </em>($x) => $x + 1;
]]>
</code>
</code_comparison>
</documentation>
73 changes: 73 additions & 0 deletions Universal/Sniffs/WhiteSpace/FnKeywordSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
*
* @package PHPCSExtra
* @copyright 2020 PHPCSExtra Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSExtra
*/

namespace PHPCSExtra\Universal\Sniffs\WhiteSpace;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Fixers\SpacesFixer;

/**
* Enforce the spacing after the "fn" keyword of arrow functions.
*
* @since 1.6.0
*/
final class FnKeywordSpacingSniff implements Sniff
{

/**
* The number of spaces to demand after the "fn" keyword.
*
* @since 1.6.0
*
* @var int
*/
public $spacingAfterKeyword = 0;

/**
* Registers the tokens that this sniff wants to listen for.
*
* @since 1.6.0
*
* @return array<int|string>
*/
public function register()
{
return [\T_FN];
}

/**
* Processes this sniff, when one of its tokens is encountered.
*
* @since 1.6.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);

SpacesFixer::checkAndFix(
$phpcsFile,
$stackPtr,
$nextNonEmpty,
(int) $this->spacingAfterKeyword,
'Expected %s after the "fn" keyword. Found: %s.',
'Incorrect',
'error',
0,
'Spacing after the arrow function "fn" keyword'
);
}
}
31 changes: 31 additions & 0 deletions Universal/Tests/WhiteSpace/FnKeywordSpacingUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* Valid spacing.
*/

$fn = fn($x) => $x + 1;
$fn = fn&() => doSomething();
$fn = static fn($x) => fn($y) => $x + $y;

/*
* Invalid spacing.
*/

$fn = fn ($x) => $x + 1;
$fn = fn &($x) => $x + 1;
$fn = static fn
($x) => $x + 1;
$fn = fn () => doSomething();

$fn = fn ($x) => fn ($y) => $x + $y;

$fn = fn
/* comment */
($x) => $x + 1;

// phpcs:set Universal.WhiteSpace.FnKeywordSpacing spacingAfterKeyword 1
$fn = fn($x) => $x + 1; // Error.
$fn = fn ($x) => $x + 1; // OK.
// Resetting to the default value.
// phpcs:set Universal.WhiteSpace.FnKeywordSpacing spacingAfterKeyword 0
30 changes: 30 additions & 0 deletions Universal/Tests/WhiteSpace/FnKeywordSpacingUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* Valid spacing.
*/

$fn = fn($x) => $x + 1;
$fn = fn&() => doSomething();
$fn = static fn($x) => fn($y) => $x + $y;

/*
* Invalid spacing.
*/

$fn = fn($x) => $x + 1;
$fn = fn&($x) => $x + 1;
$fn = static fn($x) => $x + 1;
$fn = fn() => doSomething();

$fn = fn($x) => fn($y) => $x + $y;

$fn = fn
/* comment */
($x) => $x + 1;

// phpcs:set Universal.WhiteSpace.FnKeywordSpacing spacingAfterKeyword 1
$fn = fn ($x) => $x + 1; // Error.
$fn = fn ($x) => $x + 1; // OK.
// Resetting to the default value.
// phpcs:set Universal.WhiteSpace.FnKeywordSpacing spacingAfterKeyword 0
52 changes: 52 additions & 0 deletions Universal/Tests/WhiteSpace/FnKeywordSpacingUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
*
* @package PHPCSExtra
* @copyright 2020 PHPCSExtra Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSExtra
*/

namespace PHPCSExtra\Universal\Tests\WhiteSpace;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;

/**
* Unit test class for the FnKeywordSpacing sniff.
*
* @covers PHPCSExtra\Universal\Sniffs\WhiteSpace\FnKeywordSpacingSniff
*
* @since 1.6.0
*/
final class FnKeywordSpacingUnitTest extends AbstractSniffTestCase
{

/**
* Returns the lines where errors should occur.
*
* @return array<int, int> Key is the line number, value is the number of expected errors.
*/
public function getErrorList()
{
return [
15 => 1,
16 => 1,
17 => 1,
19 => 1,
21 => 2,
23 => 1,
28 => 1,
];
}

/**
* Returns the lines where warnings should occur.
*
* @return array<int, int> Key is the line number, value is the number of expected warnings.
*/
public function getWarningList()
{
return [];
}
}
Loading