Skip to content

Commit 51f8d5f

Browse files
gmponoskukulich
authored andcommitted
Added DeprecatedAnnotationDeclarationSniff
1 parent a960bb4 commit 51f8d5f

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ Sniff provides the following settings:
11001100

11011101
Enforces fully qualified names of classes and interfaces in phpDocs - in annotations. This results in unambiguous phpDocs.
11021102

1103+
#### SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclarationSniff
1104+
1105+
Reports `@deprecated` annotations without description.
1106+
11031107
#### SlevomatCodingStandard.Commenting.DisallowCommentAfterCode 🔧
11041108

11051109
Disallows comments after code at the same line.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Commenting;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\Annotation\GenericAnnotation;
8+
use SlevomatCodingStandard\Helpers\AnnotationHelper;
9+
use function count;
10+
use const T_DOC_COMMENT_OPEN_TAG;
11+
12+
class DeprecatedAnnotationDeclarationSniff implements Sniff
13+
{
14+
15+
public const MISSING_DESCRIPTION = 'MissingDescription';
16+
17+
/** @return array<int, (int|string)> */
18+
public function register(): array
19+
{
20+
return [T_DOC_COMMENT_OPEN_TAG];
21+
}
22+
23+
/**
24+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
25+
* @param File $phpcsFile
26+
* @param int $docCommentStartPointer
27+
*/
28+
public function process(File $phpcsFile, $docCommentStartPointer): void
29+
{
30+
/** @var GenericAnnotation[] $annotations */
31+
$annotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $docCommentStartPointer, '@deprecated');
32+
33+
if (count($annotations) === 0) {
34+
return;
35+
}
36+
37+
foreach ($annotations as $annotation) {
38+
if ($annotation->getContent() !== null) {
39+
continue;
40+
}
41+
42+
$phpcsFile->addError(
43+
'Deprecated annotation must have a description.',
44+
$annotation->getStartPointer(),
45+
self::MISSING_DESCRIPTION
46+
);
47+
}
48+
}
49+
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Commenting;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class DeprecatedAnnotationDeclarationSniffTest extends TestCase
8+
{
9+
10+
public function testNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/deprecatedAnnotationDeclarationNoErrors.php');
13+
self::assertNoSniffErrorInFile($report);
14+
}
15+
16+
public function testErrors(): void
17+
{
18+
$report = self::checkFile(__DIR__ . '/data/deprecatedAnnotationDeclarationErrors.php');
19+
20+
self::assertSame(4, $report->getErrorCount());
21+
22+
foreach ([6, 12, 17, 25] as $line) {
23+
self::assertSniffError($report, $line, DeprecatedAnnotationDeclarationSniff::MISSING_DESCRIPTION);
24+
}
25+
}
26+
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @deprecated
7+
*/
8+
class Whatever
9+
{
10+
11+
/**
12+
* @deprecated
13+
*/
14+
public const DEPRECATED_WITHOUT = 'deprecated';
15+
16+
/**
17+
* @deprecated
18+
*/
19+
public function deprecatedWithoutDescriptionIsReported()
20+
{
21+
}
22+
23+
public function deprecatedVariable()
24+
{
25+
/** @deprecated */
26+
$var = 1;
27+
}
28+
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class Whatever
6+
{
7+
8+
/**
9+
* @deprecated with description
10+
*/
11+
public const DEPRECATED_WITH_DESCRIPTION = 'deprecated';
12+
13+
/**
14+
* @deprecated useSomething else
15+
*/
16+
public function deprecatedMethodWithDescriptionIsNotReported()
17+
{
18+
}
19+
20+
/**
21+
* Should not report the plain word deprecated inside a docblock
22+
*/
23+
public function deprecatedWordShouldNotBeReported()
24+
{
25+
}
26+
27+
/**
28+
* Should not report the @deprecated plain word deprecated inside a docblock
29+
*/
30+
public function deprecatedInDocblock()
31+
{
32+
}
33+
34+
public function deprecatedVariable()
35+
{
36+
/** @deprecated This var is deprecated */
37+
$var = 2;
38+
}
39+
40+
/**
41+
* @see something
42+
* @param string $var
43+
* @return void
44+
*/
45+
public function functionWithNoDeprecatedShouldNotBeReported(string $var): void
46+
{
47+
}
48+
49+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @deprecated
7+
*/
8+
class Whatever
9+
{
10+
/**
11+
* @deprecated
12+
*/
13+
public const DEPRECATED_WITHOUT = 'deprecated';
14+
15+
/**
16+
* @deprecated with description
17+
*/
18+
public const DEPRECATED_WITH = 'deprecated';
19+
20+
/**
21+
* @deprecated useSomething else
22+
*/
23+
public function deprecatedMethodWithDescriptionIsNotReported()
24+
{
25+
}
26+
27+
/**
28+
* @deprecated
29+
*/
30+
public function deprecatedWithoutDescriptionIsReported()
31+
{
32+
}
33+
34+
/**
35+
* Should not report the plain word deprecated inside a docblock
36+
*/
37+
public function deprecatedWordShouldNotBeReported()
38+
{
39+
}
40+
41+
/**
42+
* Should not report the @deprecated plain word deprecated inside a docblock
43+
*/
44+
public function deprecatedInDocblock()
45+
{
46+
}
47+
48+
public function deprecatedVariable()
49+
{
50+
/** @deprecated */
51+
$var1 = 1;
52+
53+
/** @deprecated This var is deprecated */
54+
$var2 = 2;
55+
}
56+
57+
/**
58+
* @see something
59+
* @param string $var
60+
* @return void
61+
*/
62+
public function functionWithNoDeprecatedShouldNotBeReported(string $var): void
63+
{
64+
}
65+
}

0 commit comments

Comments
 (0)