Skip to content

Commit 385a802

Browse files
authored
Merge pull request #17 from leviy/only-allow-useful-type-annotations
Only allow useful @param and @return annotations
2 parents 01a4419 + 2055dd5 commit 385a802

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

src/LEVIY/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration">
6868
<properties>
6969
<property name="allAnnotationsAreUseful" value="true"/>
70+
<property name="enableEachParameterAndReturnInspection" value="true"/>
7071
</properties>
7172
</rule>
7273

@@ -162,6 +163,7 @@
162163
</rule>
163164
<rule ref="Symfony.Commenting.FunctionComment">
164165
<exclude name="Symfony.Commenting.FunctionComment.MissingParamComment"/>
166+
<exclude name="Symfony.Commenting.FunctionComment.ParamNameNoMatch"/>
165167
<exclude name="Symfony.Commenting.FunctionComment.SpacingAfterParamName"/>
166168
</rule>
167169
<rule ref="Symfony.Formatting.BlankLineBeforeReturn"/>

tests/correct/annotations.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@
77
function allUsefulAnnotationsAreAllowed(): void
88
{
99
}
10+
11+
/**
12+
* @param int[] $numbers
13+
*/
14+
function omittingUselessReturnAnnotationsIsAllowed(array $numbers): void
15+
{
16+
}
17+
18+
/**
19+
* @param int[] $numbers
20+
*/
21+
function omittingUselessParamAnnotationsIsAllowed(string $string, int $int, array $numbers, bool $bool): void
22+
{
23+
}

tests/expected.log

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,33 @@ PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
3030

3131
FILE: tests/incorrect/type-hints.php
3232
------------------------------------------------------------------------------------------------------------------------
33-
FOUND 11 ERRORS AFFECTING 8 LINES
33+
FOUND 17 ERRORS AFFECTING 14 LINES
3434
------------------------------------------------------------------------------------------------------------------------
3535
9 | ERROR | [x] Function doesNotNeedDocumentationComment() does not need documentation comment.
36-
13 | ERROR | [x] There must be no whitespace between parameter type hint nullability symbol and parameter type hint of
36+
16 | ERROR | [x] Function uselessReturnAnnotation() has useless @return annotation.
37+
23 | ERROR | [x] Function uselessParamAnnotation() has useless @param annotation for parameter $string.
38+
35 | ERROR | [x] Function uselessAnnotationsWithUsefulComment() has useless @param annotation for parameter $string.
39+
37 | ERROR | [x] Function uselessAnnotationsWithUsefulComment() has useless @return annotation.
40+
46 | ERROR | [x] Function uselessAnnotationsWithUsefulAnnotation() has useless @param annotation for parameter
41+
| | $string.
42+
48 | ERROR | [x] Function uselessAnnotationsWithUsefulAnnotation() has useless @return annotation.
43+
54 | ERROR | [x] There must be no whitespace between parameter type hint nullability symbol and parameter type hint of
3744
| | parameter $input.
38-
17 | ERROR | [x] There must be exactly one space between return type hint colon and return type hint.
39-
17 | ERROR | [x] There must be no whitespace between closing parenthesis and return type colon.
40-
21 | ERROR | [ ] Function missingTypeHints() does not have parameter type hint nor @param annotation for its parameter
45+
58 | ERROR | [x] There must be exactly one space between return type hint colon and return type hint.
46+
58 | ERROR | [x] There must be no whitespace between closing parenthesis and return type colon.
47+
62 | ERROR | [ ] Function missingTypeHints() does not have parameter type hint nor @param annotation for its parameter
4148
| | $input.
42-
21 | ERROR | [ ] Function missingTypeHints() does not have return type hint nor @return annotation for its return
49+
62 | ERROR | [ ] Function missingTypeHints() does not have return type hint nor @return annotation for its return
4350
| | value.
44-
26 | ERROR | [ ] Function missingTraversableAnnotations() does not have @param annotation for its traversable
51+
67 | ERROR | [ ] Function missingTraversableAnnotations() does not have @param annotation for its traversable
4552
| | parameter $input.
46-
26 | ERROR | [ ] Function missingTraversableAnnotations() does not have @return annotation for its traversable return
53+
67 | ERROR | [ ] Function missingTraversableAnnotations() does not have @return annotation for its traversable return
4754
| | value.
48-
30 | ERROR | [x] Parameter $input has null default value, but is not marked as nullable.
49-
35 | ERROR | [x] Expected "int" but found "integer" in @param annotation.
50-
37 | ERROR | [x] Expected "bool" but found "boolean" in @return annotation.
55+
71 | ERROR | [x] Parameter $input has null default value, but is not marked as nullable.
56+
76 | ERROR | [x] Expected "int" but found "integer" in @param annotation.
57+
78 | ERROR | [x] Expected "bool" but found "boolean" in @return annotation.
5158
------------------------------------------------------------------------------------------------------------------------
52-
PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
59+
PHPCBF CAN FIX THE 13 MARKED SNIFF VIOLATIONS AUTOMATICALLY
5360
------------------------------------------------------------------------------------------------------------------------
5461

5562

tests/incorrect/type-hints.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,47 @@ function doesNotNeedDocumentationComment(string $input): string
1010
{
1111
}
1212

13+
/**
14+
* @param int[] $numbers
15+
*
16+
* @return void
17+
*/
18+
function uselessReturnAnnotation(array $numbers): void
19+
{
20+
}
21+
22+
/**
23+
* @param string $string
24+
* @param int[] $numbers
25+
*
26+
* @return int[]
27+
*/
28+
function uselessParamAnnotation(string $string, array $numbers): array
29+
{
30+
}
31+
32+
/**
33+
* This is a useful comment that should be kept
34+
*
35+
* @param string $string
36+
*
37+
* @return void
38+
*/
39+
function uselessAnnotationsWithUsefulComment(string $string): void
40+
{
41+
}
42+
43+
/**
44+
* @whatever
45+
*
46+
* @param string $string
47+
*
48+
* @return void
49+
*/
50+
function uselessAnnotationsWithUsefulAnnotation(string $string): void
51+
{
52+
}
53+
1354
function wrongNullabilitySymbolSpacing(? string $input): string
1455
{
1556
}
@@ -34,8 +75,8 @@ function nullableDefaultValue(string $input = null): void
3475
/**
3576
* @param integer[] $input
3677
*
37-
* @return boolean
78+
* @return boolean[]
3879
*/
39-
function longDocBlockTypeHints(array $input): bool
80+
function longDocBlockTypeHints(array $input): array
4081
{
4182
}

0 commit comments

Comments
 (0)