Skip to content

Commit 130711c

Browse files
committed
Added InlineDocCommentDeclarationSniff
1 parent 771585f commit 130711c

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Commenting;
4+
5+
use SlevomatCodingStandard\Helpers\DocCommentHelper;
6+
use SlevomatCodingStandard\Helpers\PropertyHelper;
7+
use SlevomatCodingStandard\Helpers\TokenHelper;
8+
9+
class InlineDocCommentDeclarationSniff implements \PHP_CodeSniffer_Sniff
10+
{
11+
12+
const CODE_INVALID_FORMAT = 'InvalidFormat';
13+
14+
/**
15+
* @return int[]
16+
*/
17+
public function register(): array
18+
{
19+
return [
20+
T_VARIABLE,
21+
];
22+
}
23+
24+
/**
25+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
26+
* @param \PHP_CodeSniffer_File $phpcsFile
27+
* @param int $variablePointer
28+
*/
29+
public function process(\PHP_CodeSniffer_File $phpcsFile, $variablePointer)
30+
{
31+
if (PropertyHelper::isProperty($phpcsFile, $variablePointer)) {
32+
return;
33+
}
34+
35+
$docCommentOpenPointer = DocCommentHelper::findDocCommentOpenToken($phpcsFile, $variablePointer);
36+
if ($docCommentOpenPointer === null) {
37+
return;
38+
}
39+
40+
$tokens = $phpcsFile->getTokens();
41+
42+
$docCommentContent = trim(TokenHelper::getContent($phpcsFile, $docCommentOpenPointer + 1, $tokens[$docCommentOpenPointer]['comment_closer'] - 1));
43+
44+
if (strpos($docCommentContent, '@var') !== 0) {
45+
return;
46+
}
47+
48+
if (preg_match('~^@var\\s+\\S+\s+\$\\S+(?:\\s+.+)?$~', $docCommentContent)) {
49+
return;
50+
}
51+
52+
if (preg_match('~^@var\\s+(\$\\S+)\\s+(\\S+)(\\s+.+)?$~', $docCommentContent, $matches)) {
53+
$fix = $phpcsFile->addFixableError(
54+
sprintf(
55+
'Invalid inline doc comment format "%s" for variable %s, expected "@var %s %s%s".',
56+
$docCommentContent,
57+
$tokens[$variablePointer]['content'],
58+
$matches[2],
59+
$matches[1],
60+
$matches[3] ?? ''
61+
),
62+
$variablePointer,
63+
self::CODE_INVALID_FORMAT
64+
);
65+
66+
if ($fix) {
67+
$phpcsFile->fixer->beginChangeset();
68+
for ($i = $docCommentOpenPointer; $i <= $tokens[$docCommentOpenPointer]['comment_closer']; $i++) {
69+
$phpcsFile->fixer->replaceToken($i, '');
70+
}
71+
$phpcsFile->fixer->addContent($docCommentOpenPointer, sprintf('/** @var %s %s%s */', $matches[2], $matches[1], $matches[3] ?? ''));
72+
$phpcsFile->fixer->endChangeset();
73+
}
74+
} else {
75+
$phpcsFile->addError(
76+
sprintf('Invalid inline doc comment format "%1$s" for variable %2$s, expected "@var type %2$s".', $docCommentContent, $tokens[$variablePointer]['content']),
77+
$variablePointer,
78+
self::CODE_INVALID_FORMAT
79+
);
80+
}
81+
}
82+
83+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Commenting;
4+
5+
class InlineDocCommentDeclarationSniffTest extends \SlevomatCodingStandard\Sniffs\TestCase
6+
{
7+
8+
public function testNoInvalidInlineDocCommentDeclarations()
9+
{
10+
$report = $this->checkFile(__DIR__ . '/data/noInvalidInlineDocCommentDeclarations.php');
11+
$this->assertNoSniffErrorInFile($report);
12+
}
13+
14+
public function testInvalidInlineDocCommentDeclarations()
15+
{
16+
$report = $this->checkFile(__DIR__ . '/data/invalidInlineDocCommentDeclarations.php');
17+
18+
$this->assertSame(3, $report->getErrorCount());
19+
20+
$this->assertSniffError(
21+
$report,
22+
12,
23+
InlineDocCommentDeclarationSniff::CODE_INVALID_FORMAT,
24+
'Invalid inline doc comment format "@var $a string[]" for variable $a, expected "@var string[] $a".'
25+
);
26+
$this->assertSniffError(
27+
$report,
28+
18,
29+
InlineDocCommentDeclarationSniff::CODE_INVALID_FORMAT,
30+
'Invalid inline doc comment format "@var $c" for variable $c, expected "@var type $c".'
31+
);
32+
$this->assertSniffError(
33+
$report,
34+
21,
35+
InlineDocCommentDeclarationSniff::CODE_INVALID_FORMAT,
36+
'Invalid inline doc comment format "@var $d iterable|array|\Traversable Lorem ipsum" for variable $d, expected "@var iterable|array|\Traversable $d Lorem ipsum".'
37+
);
38+
}
39+
40+
public function testFixableInvalidInlineDocCommentDeclarations()
41+
{
42+
$report = $this->checkFile(__DIR__ . '/data/invalidInlineDocCommentDeclarations.php', [], [InlineDocCommentDeclarationSniff::CODE_INVALID_FORMAT]);
43+
$this->assertAllFixedInFile($report);
44+
}
45+
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
6+
/** @var string */
7+
private $foo;
8+
9+
public function __construct()
10+
{
11+
/** @var string[] $a */
12+
$a = $this->get();
13+
14+
/** @see https://www.slevomat.cz */
15+
$b = null;
16+
17+
/** @var $c */
18+
$c = [];
19+
20+
/** @var iterable|array|\Traversable $d Lorem ipsum */
21+
$d = [];
22+
}
23+
24+
public function get()
25+
{
26+
$a = [];
27+
return $a;
28+
}
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
6+
/** @var string */
7+
private $foo;
8+
9+
public function __construct()
10+
{
11+
/** @var $a string[] */
12+
$a = $this->get();
13+
14+
/** @see https://www.slevomat.cz */
15+
$b = null;
16+
17+
/** @var $c */
18+
$c = [];
19+
20+
/** @var $d iterable|array|\Traversable Lorem ipsum */
21+
$d = [];
22+
}
23+
24+
public function get()
25+
{
26+
$a = [];
27+
return $a;
28+
}
29+
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
6+
/** @var string */
7+
private $foo;
8+
9+
public function __construct()
10+
{
11+
/** @var string[] $a */
12+
$a = $this->get();
13+
14+
/** @see https://www.slevomat.cz */
15+
$b = null;
16+
17+
/** @var iterable|array|\Traversable $d Lorem ipsum */
18+
$d = [];
19+
}
20+
21+
public function get()
22+
{
23+
$a = [];
24+
return $a;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)