Skip to content

Commit 417e95c

Browse files
committed
Enforce that only useful @param and @return annotations are used
This forbids the use of @param and @return annotations where PHP type declarations provide sufficient information.
1 parent 9067514 commit 417e95c

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

src/LEVIY/ruleset.xml

Lines changed: 1 addition & 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

tests/expected.log

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ 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 13 ERRORS AFFECTING 10 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+
32 | ERROR | [x] There must be no whitespace between parameter type hint nullability symbol and parameter type hint of
3739
| | 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
40+
36 | ERROR | [x] There must be exactly one space between return type hint colon and return type hint.
41+
36 | ERROR | [x] There must be no whitespace between closing parenthesis and return type colon.
42+
40 | ERROR | [ ] Function missingTypeHints() does not have parameter type hint nor @param annotation for its parameter
4143
| | $input.
42-
21 | ERROR | [ ] Function missingTypeHints() does not have return type hint nor @return annotation for its return
44+
40 | ERROR | [ ] Function missingTypeHints() does not have return type hint nor @return annotation for its return
4345
| | value.
44-
26 | ERROR | [ ] Function missingTraversableAnnotations() does not have @param annotation for its traversable
46+
45 | ERROR | [ ] Function missingTraversableAnnotations() does not have @param annotation for its traversable
4547
| | parameter $input.
46-
26 | ERROR | [ ] Function missingTraversableAnnotations() does not have @return annotation for its traversable return
48+
45 | ERROR | [ ] Function missingTraversableAnnotations() does not have @return annotation for its traversable return
4749
| | 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.
50+
49 | ERROR | [x] Parameter $input has null default value, but is not marked as nullable.
51+
54 | ERROR | [x] Expected "int" but found "integer" in @param annotation.
52+
56 | ERROR | [x] Expected "bool" but found "boolean" in @return annotation.
5153
------------------------------------------------------------------------------------------------------------------------
52-
PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
54+
PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY
5355
------------------------------------------------------------------------------------------------------------------------
5456

5557

tests/incorrect/type-hints.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ 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+
1332
function wrongNullabilitySymbolSpacing(? string $input): string
1433
{
1534
}
@@ -34,8 +53,8 @@ function nullableDefaultValue(string $input = null): void
3453
/**
3554
* @param integer[] $input
3655
*
37-
* @return boolean
56+
* @return boolean[]
3857
*/
39-
function longDocBlockTypeHints(array $input): bool
58+
function longDocBlockTypeHints(array $input): array
4059
{
4160
}

0 commit comments

Comments
 (0)