Skip to content

Commit

Permalink
LongTypeHintsSniff is fixable now
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jan 4, 2018
1 parent 7b5fa57 commit accf5e3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ use LogableTrait;
use LoggerInterface;
```

#### SlevomatCodingStandard.TypeHints.LongTypeHints
#### SlevomatCodingStandard.TypeHints.LongTypeHints 🔧

Enforces using shorthand scalar typehint variants in phpDocs: `int` instead of `integer` and `bool` instead of `boolean`. This is for consistency with native scalar typehints which also allow shorthand variants only.

Expand Down
24 changes: 23 additions & 1 deletion SlevomatCodingStandard/Sniffs/TypeHints/LongTypeHintsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace SlevomatCodingStandard\Sniffs\TypeHints;

use SlevomatCodingStandard\Helpers\AnnotationHelper;
use SlevomatCodingStandard\Helpers\DocCommentHelper;
use SlevomatCodingStandard\Helpers\FunctionHelper;
use SlevomatCodingStandard\Helpers\PropertyHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;

class LongTypeHintsSniff implements \PHP_CodeSniffer\Sniffs\Sniff
{
Expand Down Expand Up @@ -63,12 +65,32 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $pointer): void
}

if ($suggestType !== null) {
$phpcsFile->addError(sprintf(
$fix = $phpcsFile->addFixableError(sprintf(
'Expected "%s" but found "%s" in %s annotation.',
$suggestType,
$type,
$annotationName
), $pointer, self::CODE_USED_LONG_TYPE_HINT);

if ($fix) {
$docCommentOpenPointer = DocCommentHelper::findDocCommentOpenToken($phpcsFile, $pointer);
$docCommentClosePointer = $tokens[$docCommentOpenPointer]['comment_closer'];

$phpcsFile->fixer->beginChangeset();
for ($i = $docCommentOpenPointer; $i <= $docCommentClosePointer; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$docComment = TokenHelper::getContent($phpcsFile, $docCommentOpenPointer, $docCommentClosePointer);

$fixedDocComment = preg_replace_callback('~((?:@(?:var|param|return)\\s+)|\|)' . preg_quote($type, '~') . '(\\s|\||\[)~', function (array $matches) use ($suggestType): string {
return $matches[1] . $suggestType . $matches[2];
}, $docComment);

$phpcsFile->fixer->addContent($docCommentOpenPointer, $fixedDocComment);

$phpcsFile->fixer->endChangeset();
}
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions tests/Sniffs/TypeHints/LongTypeHintsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ public function testErrors(): void
{
$report = $this->checkFile(__DIR__ . '/data/longTypeHintsErrors.php');

$this->assertSame(6, $report->getErrorCount());
$this->assertSame(10, $report->getErrorCount());

$this->assertSniffError($report, 7, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @param annotation.');
$this->assertSniffError($report, 7, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @return annotation.');
$this->assertSniffError($report, 16, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @var annotation.');
$this->assertSniffError($report, 19, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @var annotation.');
$this->assertSniffError($report, 25, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @param annotation.');
$this->assertSniffError($report, 25, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @return annotation.');
$this->assertSniffError($report, 26, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @var annotation.');
$this->assertSniffError($report, 32, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @var annotation.');
$this->assertSniffError($report, 32, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @var annotation.');
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @param annotation.');
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @param annotation.');
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @param annotation.');
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @return annotation.');

$this->assertAllFixedInFile($report);
}

}
45 changes: 45 additions & 0 deletions tests/Sniffs/TypeHints/data/longTypeHintsErrors.fixed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @param int $a
* @return bool
*/
function doSomething($a)
{
return true;
}

class Foo
{

/** @var int|null */
private $integer = 0;

/**
* Boolean
* With
* Long
* Destription
*
* @var bool
*/
private $boolean = true;

/**
* Array
* @var int[]|bool[]|null
*/
public $array;

/**
* @param bool|null $a
* @param null|bool
* @param int|bool $c
* @return int|null
*/
public function doSomething($a, $b, $c)
{
return 0;
}

}
19 changes: 17 additions & 2 deletions tests/Sniffs/TypeHints/data/longTypeHintsErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ class Foo
/** @var integer|null */
private $integer = 0;

/** @var boolean */
/**
* Boolean
* With
* Long
* Destription
*
* @var boolean
*/
private $boolean = true;

/**
* Array
* @var integer[]|boolean[]|null
*/
public $array;

/**
* @param boolean|null $a
* @param null|boolean
* @param integer|bool $c
* @return integer|null
*/
public function doSomething($a)
public function doSomething($a, $b, $c)
{
return 0;
}
Expand Down

0 comments on commit accf5e3

Please sign in to comment.