8000 Added SlevomatCodingStandard.Functions.NamedArgumentSpacing · slevomat/coding-standard@5cb40fa · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cb40fa

Browse files
mzkkukulich
authored andcommitted
Added SlevomatCodingStandard.Functions.NamedArgumentSpacing
1 parent e741174 commit 5cb40fa

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Slevomat Coding Standard for [PHP_CodeSniffer](https://github.com/squizlabs/PHP_
126126
- [SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse](doc/functions.md#slevomatcodingstandardfunctionsdisallowtrailingcommainclosureuse-) 🔧
127127
- [SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration](doc/functions.md#slevomatcodingstandardfunctionsdisallowtrailingcommaindeclaration-) 🔧
128128
- [SlevomatCodingStandard.Functions.FunctionLength](doc/functions.md#slevomatcodingstandardfunctionsfunctionlength)
129+
- [SlevomatCodingStandard.Functions.NamedArgumentSpacing](doc/functions.md#slevomatcodingstandardfunctionsnamedargumentspacing-) 🔧
129130
- [SlevomatCodingStandard.Functions.RequireArrowFunction](doc/functions.md#slevomatcodingstandardfunctionsrequirearrowfunction-) 🔧
130131
- [SlevomatCodingStandard.Functions.RequireMultiLineCall](doc/functions.md#slevomatcodingstandardfunctionsrequiremultilinecall-) 🔧
131132
- [SlevomatCodingStandard.Functions.RequireSingleLineCall](doc/functions.md#slevomatcodingstandardfunctionsrequiresinglelinecall-) 🔧
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Functions;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\TokenHelper;
8+
use function sprintf;
9+
use const T_COLON;
10+
use const T_PARAM_NAME;
11+
use const T_WHITESPACE;
12+
13+
class NamedArgumentSpacingSniff implements Sniff
14+
{
15+
16+
public const CODE_WHITESPACE_BEFORE_COLOR = 'WhitespaceBeforeColon';
17+
public const CODE_NO_WHITESPACE_AFTER_COLON = 'NoWhitespaceAfterColon';
18+
19+
/**
20+
* @return list<string>
21+
*/
22+
public function register(): array
23+
{
24+
return [
25+
T_PARAM_NAME,
26+
];
27+
}
28+
29+
/**
30+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
31+
* @param int $pointer
32+
*/
33+
public function process(File $phpcsFile, $pointer): void
34+
{
35+
$tokens = $phpcsFile->getTokens();
36+
37+
/** @var int $colonPointer */
38+
$colonPointer = TokenHelper::findNext($phpcsFile, T_COLON, $pointer + 1);
39+
40+
$parameterName = $tokens[$pointer]['content'];
41+
42+
if ($colonPointer !== $pointer + 1) {
43+
$fix = $phpcsFile->addFixableError(
44+
sprintf('There must be no whitespace between named argument "%s" and colon.', $parameterName),
45+
$colonPointer,
46+
self::CODE_WHITESPACE_BEFORE_COLOR
47+
);
48+
if ($fix) {
49+
$phpcsFile->fixer->replaceToken($colonPointer - 1, '');
50+
}
51+
}
52+
53+
$whitespacePointer = $colonPointer + 1;
54+
55+
if (
56+
$tokens[$whitespacePointer]['code'] === T_WHITESPACE
57+
&& $tokens[$whitespacePointer]['content'] === ' '
58+
) {
59+
return;
60+
}
61+
62+
$fix = $phpcsFile->addFixableError(
63+
sprintf('There must be exactly one space after colon in named argument "%s".', $parameterName),
64+
$colonPointer,
65+
self::CODE_NO_WHITESPACE_AFTER_COLON
66+
);
67+
68+
if (!$fix) {
69+
return;
70+
}
71+
72+
if ($tokens[$whitespacePointer]['code'] === T_WHITESPACE) {
73+
$phpcsFile->fixer->replaceToken($whitespacePointer, ' ');
74+
} else {
75+
$phpcsFile->fixer->addContent($colonPointer, ' ');
76+
}
77+
}
78+
79+
}

build/phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@
370370
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall"/>
371371
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse"/>
372372
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration"/>
373+
<rule ref="SlevomatCodingStandard.Functions.NamedArgumentSpacing"/>
373374
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
374375
<rule ref="SlevomatCodingStandard.Functions.StrictCall"/>
375376
<rule ref="SlevomatCodingStandard.Functions.RequireMultiLineCall">

doc/functions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Sniff provides the following settings:
5757

5858
This sniff disallows usage of named arguments.
5959

60+
#### SlevomatCodingStandard.Functions.NamedArgumentSpacing 🔧
61+
62+
Checks spacing in named argument.
63+
6064
#### SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall 🔧
6165

6266
This sniff disallows trailing commas in multi-line calls.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Functions;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class NamedArgumentSpacingSniffTest extends TestCase
8+
{
9+
10+
public function testNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/namedArgumentSpacingNoErrors.php');
13+
self::assertNoSniffErrorInFile($report);
14+
}
15+
16+
public function testErrors(): void
17+
{
18+
$report = self::checkFile(__DIR__ . '/data/namedArgumentSpacingErrors.php');
19+
20+
self::assertSame(3, $report->getErrorCount());
21+
22+
self::assertSniffError(
23+
$report,
24+
3,
25+
NamedArgumentSpacingSniff::CODE_WHITESPACE_BEFORE_COLOR,
26+
'There must be no whitespace between named argument "search" and colon.'
27+
);
28+
self::assertSniffError(
29+
$report,
30+
4,
31+
NamedArgumentSpacingSniff::CODE_NO_WHITESPACE_AFTER_COLON,
32+
'There must be exactly one space after colon in named argument "search".'
33+
);
34+
self::assertSniffError(
35+
$report,
36+
4,
37+
NamedArgumentSpacingSniff::CODE_NO_WHITESPACE_AFTER_COLON,
38+
'There must be exactly one space after colon in named argument "subject".'
39+
);
40+
41+
self::assertAllFixedInFile($report);
42+
}
43+
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php // lint >= 8.0
2+
3+
str_replace(search: 'foo', replace: 'bar', subject: 'foo lol');
4+
str_replace(search: 'foo', replace: 'bar', subject: 'foo lol');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php // lint >= 8.0
2+
3+
str_replace(search : 'foo', replace: 'bar', subject: 'foo lol');
4+
str_replace(search:'foo', replace: 'bar', subject: 'foo lol');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php // lint >= 8.0
2+
3+
str_replace(search: 'foo', replace: 'bar', subject: 'foo lol'); // space before + missing space
4+
5+
str_replace('foo', 'bar', 'foo');

0 commit comments

Comments
 (0)
0