-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added nullability symbol spacing sniff
- Loading branch information
Showing
6 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
SlevomatCodingStandard/Sniffs/TypeHints/NullabilitySymbolSpacingSniff.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,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\TypeHints; | ||
|
||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
|
||
class NullabilitySymbolSpacingSniff implements \PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
const CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT = 'WhitespaceBetweenNullabilitySymbolAndTypeHint'; | ||
|
||
/** | ||
* @return int[] | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_NULLABLE, | ||
]; | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
* @param \PHP_CodeSniffer_File $phpcsFile | ||
* @param int $nullabilitySymbolPointer | ||
*/ | ||
public function process(\PHP_CodeSniffer_File $phpcsFile, $nullabilitySymbolPointer) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if ($tokens[$nullabilitySymbolPointer + 1]['code'] === T_WHITESPACE) { | ||
$typeHintStartPointer = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $nullabilitySymbolPointer + 1); | ||
$typeHintEndPointer = TokenHelper::findNextExcluding($phpcsFile, [T_STRING, T_NS_SEPARATOR, T_ARRAY_HINT, T_CALLABLE, T_SELF, T_PARENT, T_RETURN_TYPE], $typeHintStartPointer + 1) - 1; | ||
|
||
$fix = $phpcsFile->addFixableError( | ||
sprintf('There must be no whitespace between nullability symbol and type hint "%s".', TokenHelper::getContent($phpcsFile, $typeHintStartPointer, $typeHintEndPointer)), | ||
$nullabilitySymbolPointer, | ||
self::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT | ||
); | ||
if ($fix) { | ||
$phpcsFile->fixer->beginChangeset(); | ||
$phpcsFile->fixer->replaceToken($nullabilitySymbolPointer + 1, ''); | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
tests/Sniffs/TypeHints/NullabilitySymbolSpacingSniffTest.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,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\TypeHints; | ||
|
||
class NullabilitySymbolSpacingSniffTest extends \SlevomatCodingStandard\Sniffs\TestCase | ||
{ | ||
|
||
public function testNoErrors() | ||
{ | ||
$this->assertNoSniffErrorInFile($this->checkFile(__DIR__ . '/data/nullabilitySymbolSpacingNoErrors.php')); | ||
} | ||
|
||
public function testErrors() | ||
{ | ||
$report = $this->checkFile(__DIR__ . '/data/nullabilitySymbolSpacingErrors.php'); | ||
|
||
$this->assertSame(8, $report->getErrorCount()); | ||
|
||
$this->assertSniffError($report, 3, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"\DateTimeImmutable"'); | ||
$this->assertSniffError($report, 3, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"\Foo\Boo\Doo"'); | ||
$this->assertSniffError($report, 8, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"string"'); | ||
$this->assertSniffError($report, 8, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"self"'); | ||
$this->assertSniffError($report, 16, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"int"'); | ||
$this->assertSniffError($report, 16, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"array"'); | ||
$this->assertSniffError($report, 23, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"bool"'); | ||
$this->assertSniffError($report, 23, NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT, '"callable"'); | ||
} | ||
|
||
public function testFixableWhitespaceBetweenNullabilitySymbolAndTypeHint() | ||
{ | ||
$report = $this->checkFile(__DIR__ . '/data/fixableWhitespaceBetweenNullabilitySymbolAndTypeHint.php', [], [NullabilitySymbolSpacingSniff::CODE_WHITESPACE_BETWEEN_NULLABILITY_SYMBOL_AND_TYPE_HINT]); | ||
$this->assertAllFixedInFile($report); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/Sniffs/TypeHints/data/fixableWhitespaceBetweenNullabilitySymbolAndTypeHint.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,28 @@ | ||
<?php // lint >= 7.1 | ||
|
||
function foo(?\DateTimeImmutable $param): ?Foo | ||
{ | ||
|
||
} | ||
|
||
$callback = function(?string $param): ?Foo | ||
{ | ||
|
||
}; | ||
|
||
interface Foo | ||
{ | ||
|
||
public function doFoo(?int $param): ?Foo; | ||
|
||
} | ||
|
||
class FooBar implements Foo | ||
{ | ||
|
||
public function doBar(?bool $param): ?Bar | ||
{ | ||
|
||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/Sniffs/TypeHints/data/fixableWhitespaceBetweenNullabilitySymbolAndTypeHint.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,28 @@ | ||
<?php // lint >= 7.1 | ||
|
||
function foo(? \DateTimeImmutable $param): ? Foo | ||
{ | ||
|
||
} | ||
|
||
$callback = function(? string $param): ? Foo | ||
{ | ||
|
||
}; | ||
|
||
interface Foo | ||
{ | ||
|
||
public function doFoo(? int $param): ? Foo; | ||
|
||
} | ||
|
||
class FooBar implements Foo | ||
{ | ||
|
||
public function doBar(? bool $param): ? Bar | ||
{ | ||
|
||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/Sniffs/TypeHints/data/nullabilitySymbolSpacingErrors.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,28 @@ | ||
<?php // lint >= 7.1 | ||
|
||
function foo(? \DateTimeImmutable $param): ? \Foo\Boo\Doo | ||
{ | ||
|
||
} | ||
|
||
$callback = function(? string $param): ? self | ||
{ | ||
|
||
}; | ||
|
||
interface Foo | ||
{ | ||
|
||
public function doFoo(? int $param): ? array; | ||
|
||
} | ||
|
||
class FooBar implements Foo | ||
{ | ||
|
||
public function doBar(? bool $param): ? callable | ||
{ | ||
|
||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/Sniffs/TypeHints/data/nullabilitySymbolSpacingNoErrors.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,28 @@ | ||
<?php // lint >= 7.1 | ||
|
||
function foo(?\DateTimeImmutable $param): ?Foo | ||
{ | ||
|
||
} | ||
|
||
$callback = function(?string $param): ?Foo | ||
{ | ||
|
||
}; | ||
|
||
interface Foo | ||
{ | ||
|
||
public function doFoo(?int $param): ?Foo; | ||
|
||
} | ||
|
||
class FooBar implements Foo | ||
{ | ||
|
||
public function doBar(?bool $param): ?Bar | ||
{ | ||
|
||
} | ||
|
||
} |