Skip to content

Commit 263ec63

Browse files
authored
Merge pull request #28 from Lassombria/add-array-doc-like-vector-rule
add array doc like vector rule
2 parents 4c4de39 + 60977af commit 263ec63

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

src/PhpCs/FiveLab/ErrorCodes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ final class ErrorCodes
3131
public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed';
3232
public const MISSED_CONSTANT_TYPE = 'MissedConstantType';
3333
public const NAMESPACE_WRONG = 'NamespaceWrong';
34+
public const ARRAYS_DOC_VECTOR = 'ArraysDocVector';
3435
}

src/PhpCs/FiveLab/Sniffs/AbstractFunctionDocCommentSniff.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use PHP_CodeSniffer\Files\File;
1717
use PHP_CodeSniffer\Sniffs\Sniff;
1818

19-
/**
20-
* Abstract sniff for check doc comments.
21-
*/
2219
abstract class AbstractFunctionDocCommentSniff implements Sniff
2320
{
2421
public function register(): array

src/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniff.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
use FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\AbstractFunctionDocCommentSniff;
1818
use PHP_CodeSniffer\Files\File;
1919

20-
/**
21-
* Check annotations in comments.
22-
*/
2320
class AnnotationsSniff extends AbstractFunctionDocCommentSniff
2421
{
2522
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName, int $countCommentLines): void
@@ -60,5 +57,13 @@ private function processAnnotation(File $phpcsFile, int $lineNumber, string $ann
6057
);
6158
}
6259
}
60+
61+
if (\str_contains($value, '[]')) {
62+
$phpcsFile->addErrorOnLine(
63+
'Please use vector type annotation for arrays.',
64+
$lineNumber,
65+
ErrorCodes::ARRAYS_DOC_VECTOR
66+
);
67+
}
6368
}
6469
}

tests/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniffTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public static function provideDataSet(): array
4040
'message' => 'Please import error class in "use" block and use short class name in @throws annotation.',
4141
'source' => 'FiveLab.Commenting.Annotations.Prohibited',
4242
],
43+
[
44+
'message' => 'Please use vector type annotation for arrays.',
45+
'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector',
46+
],
47+
[
48+
'message' => 'Please use vector type annotation for arrays.',
49+
'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector',
50+
],
51+
[
52+
'message' => 'Please use vector type annotation for arrays.',
53+
'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector',
54+
],
4355
],
4456
];
4557
}

tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/success.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ function bar(int $a, string $b): int
2222
{
2323
return 1;
2424
}
25+
26+
/**
27+
* @param array<string> $a
28+
* @param array<SomeObject> $b
29+
* @param array<int, string> $c
30+
*
31+
* @return array<string|SomeObject>
32+
*/
33+
function baz(array $a, array $b, array $c): array
34+
{
35+
}

tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/wrong.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ function bar(): int
2020
{
2121
return 1;
2222
}
23+
24+
/**
25+
* @param SomeObject[] $a
26+
* @param string[] $b
27+
*
28+
* @return int[]
29+
*/
30+
function baz(array $a, array $b, array $c): array
31+
{
32+
}

0 commit comments

Comments
 (0)