Skip to content

Commit

Permalink
Added nullability symbol spacing sniff
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 3, 2017
1 parent ad05702 commit 946a6d1
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
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 tests/Sniffs/TypeHints/NullabilitySymbolSpacingSniffTest.php
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);
}

}
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
{

}

}
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 tests/Sniffs/TypeHints/data/nullabilitySymbolSpacingErrors.php
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 tests/Sniffs/TypeHints/data/nullabilitySymbolSpacingNoErrors.php
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
{

}

}

0 comments on commit 946a6d1

Please sign in to comment.