Skip to content

Commit accf5e3

Browse files
committed
LongTypeHintsSniff is fixable now
1 parent 7b5fa57 commit accf5e3

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ use LogableTrait;
177177
use LoggerInterface;
178178
```
179179

180-
#### SlevomatCodingStandard.TypeHints.LongTypeHints
180+
#### SlevomatCodingStandard.TypeHints.LongTypeHints 🔧
181181

182182
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.
183183

SlevomatCodingStandard/Sniffs/TypeHints/LongTypeHintsSniff.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace SlevomatCodingStandard\Sniffs\TypeHints;
44

55
use SlevomatCodingStandard\Helpers\AnnotationHelper;
6+
use SlevomatCodingStandard\Helpers\DocCommentHelper;
67
use SlevomatCodingStandard\Helpers\FunctionHelper;
78
use SlevomatCodingStandard\Helpers\PropertyHelper;
9+
use SlevomatCodingStandard\Helpers\TokenHelper;
810

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

6567
if ($suggestType !== null) {
66-
$phpcsFile->addError(sprintf(
68+
$fix = $phpcsFile->addFixableError(sprintf(
6769
'Expected "%s" but found "%s" in %s annotation.',
6870
$suggestType,
6971
$type,
7072
$annotationName
7173
), $pointer, self::CODE_USED_LONG_TYPE_HINT);
74+
75+
if ($fix) {
76+
$docCommentOpenPointer = DocCommentHelper::findDocCommentOpenToken($phpcsFile, $pointer);
77+
$docCommentClosePointer = $tokens[$docCommentOpenPointer]['comment_closer'];
78+
79+
$phpcsFile->fixer->beginChangeset();
80+
for ($i = $docCommentOpenPointer; $i <= $docCommentClosePointer; $i++) {
81+
$phpcsFile->fixer->replaceToken($i, '');
82+
}
83+
84+
$docComment = TokenHelper::getContent($phpcsFile, $docCommentOpenPointer, $docCommentClosePointer);
85+
86+
$fixedDocComment = preg_replace_callback('~((?:@(?:var|param|return)\\s+)|\|)' . preg_quote($type, '~') . '(\\s|\||\[)~', function (array $matches) use ($suggestType): string {
87+
return $matches[1] . $suggestType . $matches[2];
88+
}, $docComment);
89+
90+
$phpcsFile->fixer->addContent($docCommentOpenPointer, $fixedDocComment);
91+
92+
$phpcsFile->fixer->endChangeset();
93+
}
7294
}
7395
}
7496
}

tests/Sniffs/TypeHints/LongTypeHintsSniffTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ public function testErrors(): void
1414
{
1515
$report = $this->checkFile(__DIR__ . '/data/longTypeHintsErrors.php');
1616

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

1919
$this->assertSniffError($report, 7, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @param annotation.');
2020
$this->assertSniffError($report, 7, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @return annotation.');
2121
$this->assertSniffError($report, 16, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @var annotation.');
22-
$this->assertSniffError($report, 19, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @var annotation.');
23-
$this->assertSniffError($report, 25, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @param annotation.');
24-
$this->assertSniffError($report, 25, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @return annotation.');
22+
$this->assertSniffError($report, 26, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @var annotation.');
23+
$this->assertSniffError($report, 32, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @var annotation.');
24+
$this->assertSniffError($report, 32, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @var annotation.');
25+
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @param annotation.');
26+
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "bool" but found "boolean" in @param annotation.');
27+
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @param annotation.');
28+
$this->assertSniffError($report, 40, LongTypeHintsSniff::CODE_USED_LONG_TYPE_HINT, 'Expected "int" but found "integer" in @return annotation.');
29+
30+
$this->assertAllFixedInFile($report);
2531
}
2632

2733
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* @param int $a
5+
* @return bool
6+
*/
7+
function doSomething($a)
8+
{
9+
return true;
10+
}
11+
12+
class Foo
13+
{
14+
15+
/** @var int|null */
16+
private $integer = 0;
17+
18+
/**
19+
* Boolean
20+
* With
21+
* Long
22+
* Destription
23+
*
24+
* @var bool
25+
*/
26+
private $boolean = true;
27+
28+
/**
29+
* Array
30+
* @var int[]|bool[]|null
31+
*/
32+
public $array;
33+
34+
/**
35+
* @param bool|null $a
36+
* @param null|bool
37+
* @param int|bool $c
38+
* @return int|null
39+
*/
40+
public function doSomething($a, $b, $c)
41+
{
42+
return 0;
43+
}
44+
45+
}

tests/Sniffs/TypeHints/data/longTypeHintsErrors.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ class Foo
1515
/** @var integer|null */
1616
private $integer = 0;
1717

18-
/** @var boolean */
18+
/**
19+
* Boolean
20+
* With
21+
* Long
22+
* Destription
23+
*
24+
* @var boolean
25+
*/
1926
private $boolean = true;
2027

28+
/**
29+
* Array
30+
* @var integer[]|boolean[]|null
31+
*/
32+
public $array;
33+
2134
/**
2235
* @param boolean|null $a
36+
* @param null|boolean
37+
* @param integer|bool $c
2338
* @return integer|null
2439
*/
25-
public function doSomething($a)
40+
public function doSomething($a, $b, $c)
2641
{
2742
return 0;
2843
}

0 commit comments

Comments
 (0)