-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SlevomatCodingStandard.Functions.NamedArgumentSpacing
- Loading branch information
Showing
8 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
SlevomatCodingStandard/Sniffs/Functions/NamedArgumentSpacingSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ' '); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
tests/Sniffs/Functions/data/namedArgumentSpacingErrors.fixed.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |