Skip to content

Commit

Permalink
Added SlevomatCodingStandard.Functions.NamedArgumentSpacing
Browse files Browse the repository at this point in the history
  • Loading branch information
mzk authored and kukulich committed Oct 7, 2023
1 parent e741174 commit 5cb40fa
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Slevomat Coding Standard for [PHP_CodeSniffer](https://github.com/squizlabs/PHP_
- [SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse](doc/functions.md#slevomatcodingstandardfunctionsdisallowtrailingcommainclosureuse-) 🔧
- [SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration](doc/functions.md#slevomatcodingstandardfunctionsdisallowtrailingcommaindeclaration-) 🔧
- [SlevomatCodingStandard.Functions.FunctionLength](doc/functions.md#slevomatcodingstandardfunctionsfunctionlength)
- [SlevomatCodingStandard.Functions.NamedArgumentSpacing](doc/functions.md#slevomatcodingstandardfunctionsnamedargumentspacing-) 🔧
- [SlevomatCodingStandard.Functions.RequireArrowFunction](doc/functions.md#slevomatcodingstandardfunctionsrequirearrowfunction-) 🔧
- [SlevomatCodingStandard.Functions.RequireMultiLineCall](doc/functions.md#slevomatcodingstandardfunctionsrequiremultilinecall-) 🔧
- [SlevomatCodingStandard.Functions.RequireSingleLineCall](doc/functions.md#slevomatcodingstandardfunctionsrequiresinglelinecall-) 🔧
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function sprintf;
use const T_COLON;
use const T_PARAM_NAME;
use const T_WHITESPACE;

class NamedArgumentSpacingSniff implements Sniff
{

public const CODE_WHITESPACE_BEFORE_COLOR = 'WhitespaceBeforeColon';
public const CODE_NO_WHITESPACE_AFTER_COLON = 'NoWhitespaceAfterColon';

/**
* @return list<string>
*/
public function register(): array
{
return [
T_PARAM_NAME,
];
}

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $pointer
*/
public function process(File $phpcsFile, $pointer): void
{
$tokens = $phpcsFile->getTokens();

/** @var int $colonPointer */
$colonPointer = TokenHelper::findNext($phpcsFile, T_COLON, $pointer + 1);

$parameterName = $tokens[$pointer]['content'];

if ($colonPointer !== $pointer + 1) {
$fix = $phpcsFile->addFixableError(
sprintf('There must be no whitespace between named argument "%s" and colon.', $parameterName),
$colonPointer,
self::CODE_WHITESPACE_BEFORE_COLOR
);
if ($fix) {
$phpcsFile->fixer->replaceToken($colonPointer - 1, '');
}
}

$whitespacePointer = $colonPointer + 1;

if (
$tokens[$whitespacePointer]['code'] === T_WHITESPACE
&& $tokens[$whitespacePointer]['content'] === ' '
) {
return;
}

$fix = $phpcsFile->addFixableError(
sprintf('There must be exactly one space after colon in named argument "%s".', $parameterName),
$colonPointer,
self::CODE_NO_WHITESPACE_AFTER_COLON
);

if (!$fix) {
return;
}

if ($tokens[$whitespacePointer]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($whitespacePointer, ' ');
} else {
$phpcsFile->fixer->addContent($colonPointer, ' ');
}
}

}
1 change: 1 addition & 0 deletions build/phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall"/>
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse"/>
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration"/>
<rule ref="SlevomatCodingStandard.Functions.NamedArgumentSpacing"/>
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
<rule ref="SlevomatCodingStandard.Functions.StrictCall"/>
<rule ref="SlevomatCodingStandard.Functions.RequireMultiLineCall">
Expand Down
4 changes: 4 additions & 0 deletions doc/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Sniff provides the following settings:

This sniff disallows usage of named arguments.

#### SlevomatCodingStandard.Functions.NamedArgumentSpacing 🔧

Checks spacing in named argument.

#### SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall 🔧

This sniff disallows trailing commas in multi-line calls.
Expand Down
44 changes: 44 additions & 0 deletions tests/Sniffs/Functions/NamedArgumentSpacingSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Sniffs\Functions;

use SlevomatCodingStandard\Sniffs\TestCase;

class NamedArgumentSpacingSniffTest extends TestCase
{

public function testNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/namedArgumentSpacingNoErrors.php');
self::assertNoSniffErrorInFile($report);
}

public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/namedArgumentSpacingErrors.php');

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

self::assertSniffError(
$report,
3,
NamedArgumentSpacingSniff::CODE_WHITESPACE_BEFORE_COLOR,
'There must be no whitespace between named argument "search" and colon.'
);
self::assertSniffError(
$report,
4,
NamedArgumentSpacingSniff::CODE_NO_WHITESPACE_AFTER_COLON,
'There must be exactly one space after colon in named argument "search".'
);
self::assertSniffError(
$report,
4,
NamedArgumentSpacingSniff::CODE_NO_WHITESPACE_AFTER_COLON,
'There must be exactly one space after colon in named argument "subject".'
);

self::assertAllFixedInFile($report);
}

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

str_replace(search: 'foo', replace: 'bar', subject: 'foo lol');
str_replace(search: 'foo', replace: 'bar', subject: 'foo lol');
4 changes: 4 additions & 0 deletions tests/Sniffs/Functions/data/namedArgumentSpacingErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php // lint >= 8.0

str_replace(search : 'foo', replace: 'bar', subject: 'foo lol');
str_replace(search:'foo', replace: 'bar', subject: 'foo lol');
5 changes: 5 additions & 0 deletions tests/Sniffs/Functions/data/namedArgumentSpacingNoErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php // lint >= 8.0

str_replace(search: 'foo', replace: 'bar', subject: 'foo lol'); // space before + missing space

str_replace('foo', 'bar', 'foo');

0 comments on commit 5cb40fa

Please sign in to comment.