-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathClassAndInterfacePHPDocFormattingSniff.php
117 lines (101 loc) · 3.21 KB
/
ClassAndInterfacePHPDocFormattingSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Commenting;
use Magento2\Helpers\Commenting\PHPDocFormattingValidator;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Detects PHPDoc formatting for classes and interfaces.
*/
class ClassAndInterfacePHPDocFormattingSniff implements Sniff
{
/**
* @var PHPDocFormattingValidator
*/
private $PHPDocFormattingValidator;
/**
* @var string[] List of tags that can not be used in comments
*/
public $forbiddenTags = [
'@category',
'@package',
'@subpackage'
];
/**
* Helper initialisation
*/
public function __construct()
{
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
}
/**
* @inheritDoc
*/
public function register()
{
return [
T_CLASS,
T_INTERFACE
];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true);
$commentStartPtr = $this->PHPDocFormattingValidator->findPHPDoc($stackPtr, $phpcsFile);
if ($commentStartPtr === -1) {
return;
}
if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
$phpcsFile->addWarning(
sprintf(
'%s description must contain meaningful information beyond what its name provides or be removed.',
ucfirst($tokens[$stackPtr]['content'])
),
$stackPtr,
'InvalidDescription'
);
}
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
$phpcsFile->addWarning(
'Motivation behind the added @deprecated tag MUST be explained. '
. '@see tag MUST be used with reference to new implementation when code is deprecated '
. 'and there is a new alternative.',
$stackPtr,
'InvalidDeprecatedTagUsage'
);
}
$this->validateTags($phpcsFile, $commentStartPtr, $tokens);
}
/**
* Validates that forbidden tags are not used in comment
*
* @param File $phpcsFile
* @param int $commentStartPtr
* @param array $tokens
* @return bool
*/
private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
{
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
continue;
}
if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
$phpcsFile->addWarning(
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
$i,
'ForbiddenTags'
);
}
}
return false;
}
}